From d4a21d7e746dc1284f44137d1c3e45b7b5ee09c0 Mon Sep 17 00:00:00 2001 From: xray7224 Date: Tue, 24 Sep 2013 20:30:51 +0100 Subject: [PATCH] Add basic upload image capabilities --- mediagoblin/decorators.py | 2 +- mediagoblin/federation/routing.py | 6 ++++ mediagoblin/federation/views.py | 56 ++++++++++++++++++++++++++++++- mediagoblin/oauth/oauth.py | 2 +- mediagoblin/tools/request.py | 2 +- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index 5cba6fee..90edf96b 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -404,7 +404,7 @@ def oauth_required(controller): valid, r = resource_endpoint.validate_protected_resource_request( uri=request.url, http_method=request.method, - body=request.get_data(), + body=request.data, headers=dict(request.headers), ) diff --git a/mediagoblin/federation/routing.py b/mediagoblin/federation/routing.py index 12306766..b9cc4e2e 100644 --- a/mediagoblin/federation/routing.py +++ b/mediagoblin/federation/routing.py @@ -36,6 +36,12 @@ add_route( "mediagoblin.federation.views:feed" ) +add_route( + "mediagoblin.federation.user.uploads", + "/api/user//uploads", + "mediagoblin.federation.views:uploads" + ) + add_route( "mediagoblin.federation.inbox", "/api/user//inbox", diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index cff3d499..851a3f39 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -1,9 +1,16 @@ import json +import io +from werkzeug.datastructures import FileStorage + +from mediagoblin.media_types import sniff_media from mediagoblin.decorators import oauth_required from mediagoblin.db.models import User, MediaEntry, MediaComment from mediagoblin.tools.response import redirect, json_response from mediagoblin.meddleware.csrf import csrf_exempt +from mediagoblin.notifications import add_comment_subscription +from mediagoblin.submit.lib import (new_upload_entry, prepare_queue_task, + run_process_media) #@oauth_required def profile(request, raw=False): @@ -36,11 +43,58 @@ def user(request): return json_response(data) +#@oauth_required +@csrf_exempt +def uploads(request): + """ This is the endpoint which uploads can be sent ot - /api/user//uploads """ + user = request.matchdict["username"] + requested_user = User.query.filter_by(username=user) + + if requested_user is None: + error = "No such 'user' with id '{0}'".format(user) + return json_response({"error": error}, status=404) + + request.user = requested_user[0] + if request.method == "POST": + # Wrap the data in the werkzeug file wrapper + file_data = FileStorage( + stream=io.BytesIO(request.data), + filename=request.form.get("qqfile", "unknown.jpg"), + content_type=request.headers.get("Content-Type", "application/octal-stream") + ) + + # Use the same kind of method from mediagoblin/submit/views:submit_start + media_type, media_manager = sniff_media(file_data) + entry = new_upload_entry(request.user) + entry.media_type = unicode(media_type) + entry.title = u"Hello ^_^" + entry.description = u"" + entry.license = None + + entry.generate_slug() + + queue_file = prepare_queue_task(request.app, entry, file_data.filename) + with queue_file: + queue_file.write(request.data) + + entry.save() + + # run the processing + feed_url = request.urlgen( + 'mediagoblin.user_pages.atom_feed', + qualified=True, user=request.user.username) + + run_process_media(entry, feed_url) + add_comment_subscription(request.user, entry) + + return json_response(entry.serialize(request)) + + return json_response({"error": "Not yet implemented"}, status=400) + @oauth_required @csrf_exempt def feed(request): """ Handles the user's outbox - /api/user//feed """ - print request.user user = request.matchdict["username"] requested_user = User.query.filter_by(username=user) diff --git a/mediagoblin/oauth/oauth.py b/mediagoblin/oauth/oauth.py index 8229c47d..d9defa4b 100644 --- a/mediagoblin/oauth/oauth.py +++ b/mediagoblin/oauth/oauth.py @@ -126,7 +126,7 @@ class GMGRequest(Request): """ kwargs["uri"] = kwargs.get("uri", request.url) kwargs["http_method"] = kwargs.get("http_method", request.method) - kwargs["body"] = kwargs.get("body", request.get_data()) + kwargs["body"] = kwargs.get("body", request.data) kwargs["headers"] = kwargs.get("headers", dict(request.headers)) super(GMGRequest, self).__init__(*args, **kwargs) diff --git a/mediagoblin/tools/request.py b/mediagoblin/tools/request.py index d4739039..2de0b32f 100644 --- a/mediagoblin/tools/request.py +++ b/mediagoblin/tools/request.py @@ -45,7 +45,7 @@ def setup_user_in_request(request): def decode_request(request): """ Decodes a request based on MIME-Type """ - data = request.get_data() + data = request.data if request.content_type == json_encoded: data = json.loads(data) -- 2.25.1