Change API filename upload using custom header
authorRomain Porte <microjoe@microjoe.org>
Sat, 11 Nov 2017 17:29:54 +0000 (18:29 +0100)
committerBoris Bobrov <breton@cynicmansion.ru>
Sat, 11 Nov 2017 17:41:01 +0000 (20:41 +0300)
When uploading a file using the API its same is always going to be
'unknown.<extension>', which is not good looking for file urls. This
patch proposes a custom header named 'X-File-Name' that allows to
specify a file name using an API client.

This changeset was tested with a local OAuth1 client.

mediagoblin/api/views.py

index 74181fdeefacb5a7f08bf77785572f94dc6bda74..b25300f0854dca95cb1a94558d260e6686efd849 100644 (file)
@@ -115,8 +115,13 @@ def uploads_endpoint(request):
             )
 
         mimetype = request.headers["Content-Type"]
-        filename = mimetypes.guess_all_extensions(mimetype)
-        filename = 'unknown' + filename[0] if filename else filename
+
+        if "X-File-Name" in request.headers:
+            filename = request.headers["X-File-Name"]
+        else:
+            filename = mimetypes.guess_all_extensions(mimetype)
+            filename = 'unknown' + filename[0] if filename else filename
+
         file_data = FileStorage(
             stream=io.BytesIO(request.data),
             filename=filename,