Add basic upload image capabilities
authorxray7224 <xray7224@googlemail.com>
Tue, 24 Sep 2013 19:30:51 +0000 (20:30 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 22 Jul 2014 22:13:15 +0000 (23:13 +0100)
mediagoblin/decorators.py
mediagoblin/federation/routing.py
mediagoblin/federation/views.py
mediagoblin/oauth/oauth.py
mediagoblin/tools/request.py

index 5cba6fee8c6a5c4cdf1fa12e7ac8337a67da7cb6..90edf96bdb001304e88347e2155d483675c22061 100644 (file)
@@ -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),
                 )
 
index 123067666c12576dc574cebef12e472e6d2a1703..b9cc4e2e0a8bd596f0bbbbcfc017a2e59cccaed7 100644 (file)
@@ -36,6 +36,12 @@ add_route(
     "mediagoblin.federation.views:feed"
     )
 
+add_route(
+    "mediagoblin.federation.user.uploads",
+    "/api/user/<string:username>/uploads",
+    "mediagoblin.federation.views:uploads"
+    )
+
 add_route(
     "mediagoblin.federation.inbox",
     "/api/user/<string:username>/inbox",
index cff3d499e3a4d32a111c8546497ab079d25c741c..851a3f39ca7e850d8c91f38591ab8dbd21cd83a8 100644 (file)
@@ -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/<username>/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/<username>/feed """
-    print request.user
     user = request.matchdict["username"]
     requested_user = User.query.filter_by(username=user)
 
index 8229c47d885c99b7cd23e4d2ddbd39be66ba21b9..d9defa4b9da56e3ed0ae33b143d1ead6463cc4e7 100644 (file)
@@ -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)
index d47390394430cfe220cd8a053fea75664ff0a347..2de0b32f177196a92a9fe87d7229d7e8c9d8e3eb 100644 (file)
@@ -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)