From: xray7224 Date: Thu, 10 Oct 2013 19:19:58 +0000 (+0100) Subject: Refactors api uploading to media managers X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7810817caf73bcc0dcdfe1cec249c86e3e77c148;p=mediagoblin.git Refactors api uploading to media managers --- diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index 215e7552..cc5d0afa 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -467,18 +467,16 @@ class MediaEntry(Base, MediaEntryMixin): if show_comments: comments = [comment.serialize(request) for comment in self.get_comments()] total = len(comments) - if total > 0: - # we only want to include replies if there are any. - context["replies"] = { - "totalItems": total, - "items": comments, - "url": request.urlgen( - "mediagoblin.federation.object.comments", - objectType=self.objectType, - uuid=self.slug, - qualified=True - ), - } + context["replies"] = { + "totalItems": total, + "items": comments, + "url": request.urlgen( + "mediagoblin.federation.object.comments", + objectType=self.objectType, + uuid=self.slug, + qualified=True + ), + } return context diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index 4add17d9..bdc93d9b 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -46,7 +46,7 @@ def user(request): #@oauth_required @csrf_exempt def uploads(request): - """ This is the endpoint which uploads can be sent ot - /api/user//uploads """ + """ This is the endpoint which uploads can be sent to - /api/user//uploads """ user = request.matchdict["username"] requested_user = User.query.filter_by(username=user) @@ -58,40 +58,22 @@ def uploads(request): if request.method == "POST": # Wrap the data in the werkzeug file wrapper file_data = FileStorage( - stream=io.BytesIO(request.data), - filename=request.args.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 + stream=io.BytesIO(request.data), + filename=request.args.get("qqfile", "unknown.jpg"), + content_type=request.headers.get("Content-Type", "application/octal-stream") + ) + + # Find media manager media_type, media_manager = sniff_media(file_data) entry = new_upload_entry(request.user) - entry.media_type = unicode(media_type) - entry.title = unicode(request.args.get("title", "Hello ^_^")) - entry.description = unicode(request.args.get("description", "")) - 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)) + if hasattr(media_manager, "api_upload_request"): + return media_manager.api_upload_request(request, file_data, entry) + else: + return json_response({"error": "Not yet implemented"}, status=400) return json_response({"error": "Not yet implemented"}, status=400) -@oauth_required +#@oauth_required @csrf_exempt def feed(request): """ Handles the user's outbox - /api/user//feed """ @@ -124,6 +106,7 @@ def feed(request): comment.save() data = {"verb": "post", "object": comment.serialize(request)} return json_response(data) + elif obj.get("objectType", None) == "image": # Posting an image to the feed # NB: This is currently just handing the image back until we have an @@ -146,6 +129,7 @@ def feed(request): error = {"error": "Unknown object type '{0}'.".format(obj.get("objectType", None))} return json_response(error, status=400) + feed_url = request.urlgen( "mediagoblin.federation.feed", username=request.user.username, diff --git a/mediagoblin/media_types/image/__init__.py b/mediagoblin/media_types/image/__init__.py index 06e0f08f..0a77b0ce 100644 --- a/mediagoblin/media_types/image/__init__.py +++ b/mediagoblin/media_types/image/__init__.py @@ -19,7 +19,9 @@ import logging from mediagoblin.media_types import MediaManagerBase from mediagoblin.media_types.image.processing import sniff_handler, \ ImageProcessingManager - +from mediagoblin.tools.response import json_response +from mediagoblin.submit.lib import prepare_queue_task, run_process_media +from mediagoblin.notifications import add_comment_subscription _log = logging.getLogger(__name__) @@ -56,6 +58,30 @@ class ImageMediaManager(MediaManagerBase): except (KeyError, ValueError): return None + @staticmethod + def api_upload_request(request, file_data, entry): + """ This handles a image upload request """ + # Use the same kind of method from mediagoblin/submit/views:submit_start + entry.media_type = unicode(MEDIA_TYPE) + entry.title = unicode(request.args.get("title", file_data.filename)) + entry.description = unicode(request.args.get("description", "")) + entry.license = request.args.get("license", "") # not part of the standard API + + entry.generate_slug() + + queue_file = prepare_queue_task(request.app, entry, file_data.filename) + with queue_file: + queue_file.write(request.data) + + entry.save() + + 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)) def get_media_type_and_manager(ext): if ext in ACCEPTED_EXTENSIONS: