Browse Source

Create a basic but functional micropub media endpoint :o

tags/v1.9.8
Danielle McLean 1 year ago
parent
commit
4d974a5364
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240
8 changed files with 103 additions and 38 deletions
  1. 1
    0
      Pipfile
  2. 36
    36
      Pipfile.lock
  3. BIN
      lemoncurry.paw
  4. 1
    0
      lemoncurry/settings/base.py
  5. 4
    0
      lemoncurry/utils.py
  6. 4
    2
      micropub/urls.py
  7. 54
    0
      micropub/views/media.py
  8. 3
    0
      micropub/views/query.py

+ 1
- 0
Pipfile View File

@@ -46,5 +46,6 @@ django-super-favicon = "*"
46 46
 django-redis = "*"
47 47
 gevent = "*"
48 48
 django-extensions = "*"
49
+python-magic = "*"
49 50
 
50 51
 [dev-packages]

+ 36
- 36
Pipfile.lock View File

@@ -1,7 +1,7 @@
1 1
 {
2 2
     "_meta": {
3 3
         "hash": {
4
-            "sha256": "d62130799f04a1649c0e6bd978dca2816500d94e27af5d21d1ec328dc795a2f8"
4
+            "sha256": "0347a58bdf44bc71022adb2ced6ae592ff1f7fd44520caee906f3e8e0e3730e4"
5 5
         },
6 6
         "pipfile-spec": 6,
7 7
         "requires": {
@@ -571,44 +571,29 @@
571 571
             ],
572 572
             "version": "==1.5.3"
573 573
         },
574
+        "pyasn1": {
575
+            "hashes": [
576
+                "sha256:0d7f6e959fe53f3960a23d73f35e1fce61348b30915b6664309ca756de7c1f89",
577
+                "sha256:5a0db897b311d265cde49615cf783f1c78613138605cdd0f907ecfa5b2aba3ee",
578
+                "sha256:758cb50abddc03e4563fd9e7f03db56e3e87b58c0bd01247360326e5c0c7ffa5",
579
+                "sha256:7d626683e3d792cccc608da02498aff37ab4f3dafd8905d6bf755d11f9b26b43",
580
+                "sha256:a7efe807c4b83a859e2735c692b92ed7b567cfddc4163763412920041d876c2b",
581
+                "sha256:b5a9ca48055b9a20f6d1b3d68e38692e5431c86a0f99ea602e61294e891fee5b",
582
+                "sha256:c07d6e587b2f928366b1f67c09bda026a3e6fcc99e80a744dc67f8fca3895626",
583
+                "sha256:d258b0a71994f7770599835249cece1caef3c70def868c4915e6e5ca49b67d15",
584
+                "sha256:d5cd6ed995dba16fad0c521cfe31cd2d68400b53fcc2bce93326829be73ab6d1",
585
+                "sha256:d84c2aea3cf43780e9e6a19f4e4dddee9f6976519020e64e47c57e5c7a8c3dd2",
586
+                "sha256:e85895087905c65b5b594eb91f7522664c85545b147d5f4d4e7b1b07da8dcbdc",
587
+                "sha256:f81c96761fca60d64b1c9b79ec2e40cf9495a745cf570613079ef324aeb9672b"
588
+            ],
589
+            "version": "==0.4.2"
590
+        },
574 591
         "pycparser": {
575 592
             "hashes": [
576 593
                 "sha256:99a8ca03e29851d96616ad0404b4aad7d9ee16f25c9f9708a11faf2810f7b226"
577 594
             ],
578 595
             "version": "==2.18"
579 596
         },
580
-        "pycryptodome": {
581
-            "hashes": [
582
-                "sha256:043c82cd3dd3120286a1b325ace93000cf52abb13a067c3ecb6220f874fe4c30",
583
-                "sha256:0cdd73492859d853f60b8185715312dbca465879661e28d354d1cf5ea11860e7",
584
-                "sha256:15013007e393d0cc0e69f4329a47c4c8597b7f3d02c12c03f805405542f70c71",
585
-                "sha256:19d81b92bff837cdade735b9023808556bb4868e1ce194dad4d5ec4e2b2851f3",
586
-                "sha256:1ceb3e87605c4f0080115a8a00abf45f5df27b0166a37fd669fbff4523273cfc",
587
-                "sha256:2354a77051ed4a2959ce2aac508071eb3e42fc348ea39228b2eac335990bf508",
588
-                "sha256:27bd2878200690b050dca34f505b5c623532324b3de40267c1484784063134df",
589
-                "sha256:322f239e51fda80233762400a8975ab728639b571fa58545b95b9c44042af010",
590
-                "sha256:49a71eb990af30ff6276cfe201eb83ed3640ae989c1b5973f7b55a46c94232d1",
591
-                "sha256:4b5a2680008da3ac0cef2d3661597e0cbf8a3eb19eed35b859fd67e2de63eb85",
592
-                "sha256:6d34fe5134eb5d62368e21e6f203ac1770bc7273e9536c4a280121312c2de53a",
593
-                "sha256:733d5eb7e5ceed8b9d0b3c24c81f52c04cb5de6786461388204fceefe4456aa5",
594
-                "sha256:7c73d3798fe2946953768b788ce554c0d4b390780f5e73d63bd833241af27bfe",
595
-                "sha256:97af76f5200f15e97cac58d77f319dec40b4bada98de697c91a9517e63b41d1a",
596
-                "sha256:97cc46ff02b99dafdc2e0385b325cec0f8a15bf8b285d6ed1d7e4a3bc2067ce1",
597
-                "sha256:a561b59e0c3548eb649af381b7c38c6fd8392bbd4d0a8214794b2b761f405af4",
598
-                "sha256:ab2c633bfc23cf41be9281228517cb6f87879f4f1aeb154ed72bd53ab7cc83e9",
599
-                "sha256:adb54316998337f315520bbd8ef4d8bbd940b4ddfaef8ba1db3c137c5e499399",
600
-                "sha256:b4a3b710287eb1fc3e2cc1af018063f003530dff00c9ea4c55ae19bc1f3923cc",
601
-                "sha256:bcfdb66d6604882c3f96eea922552c2487cc0aec4b883cd217b9d341d2f8fad0",
602
-                "sha256:c08c053eb8716bbbd5e13e38f453b9e46a063e68df8659f3c421dcb7519fd381",
603
-                "sha256:e51da4ef9d9e2695a04044152f380c2db17adc9fc6fad8e24d863ead9cd548ed",
604
-                "sha256:e850e07f54dc3de9a1efdd59d227fcd1cb30cdd307dafdc647c79e8f30cf5032",
605
-                "sha256:ebc579c41fe26748dc1bad4f9105f08740ee28826293a28103b3875968695a5e",
606
-                "sha256:ed94cb1b4bf24be734f2bf2db3e8ea75f3914d2f8e684291bee54bbe4a5a9151",
607
-                "sha256:f5e19802295e63bdf83bb92849285c01f7167840efb1c1e08507a50b10ba7efa",
608
-                "sha256:fc569682f012b1f62f8d28d8f9bc71f1de67648cd1bc124ef8ccf8db4edfc28a"
609
-            ],
610
-            "version": "==3.6.1"
611
-        },
612 597
         "pytest": {
613 598
             "hashes": [
614 599
                 "sha256:54713b26c97538db6ff0703a12b19aeaeb60b5e599de542e7fca0ec83b9038e8",
@@ -633,11 +618,19 @@
633 618
         },
634 619
         "python-jose": {
635 620
             "hashes": [
636
-                "sha256:391f860dbe274223d73dd87de25e4117bf09e8fe5f93a417663b1f2d7b591165",
637
-                "sha256:3b35cdb0e55a88581ff6d3f12de753aa459e940b50fe7ca5aa25149bc94cb37b"
621
+                "sha256:e06dd2e5e9125da79b519ff2652b8c666d64a5ea228fcd9862e0b29a534ccc53",
622
+                "sha256:e8255fb3cc524c04f4c790547a6215468f2a32d3a866424175523359e69f3aeb"
638 623
             ],
639 624
             "index": "pypi",
640
-            "version": "==2.0.2"
625
+            "version": "==3.0.0"
626
+        },
627
+        "python-magic": {
628
+            "hashes": [
629
+                "sha256:f2674dcfad52ae6c49d4803fa027809540b130db1dec928cfbb9240316831375",
630
+                "sha256:f3765c0f582d2dfc72c15f3b5a82aecfae9498bd29ca840d72f37d7bd38bfcd5"
631
+            ],
632
+            "index": "pypi",
633
+            "version": "==0.4.15"
641 634
         },
642 635
         "python-memcached": {
643 636
             "hashes": [
@@ -730,6 +723,13 @@
730 723
             ],
731 724
             "version": "==0.10.0"
732 725
         },
726
+        "rsa": {
727
+            "hashes": [
728
+                "sha256:25df4e10c263fb88b5ace923dd84bf9aa7f5019687b5e55382ffcdb8bede9db5",
729
+                "sha256:43f682fea81c452c98d09fc316aae12de6d30c4b5c84226642cf8f8fd1c93abd"
730
+            ],
731
+            "version": "==3.4.2"
732
+        },
733 733
         "six": {
734 734
             "hashes": [
735 735
                 "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",

BIN
lemoncurry.paw View File


+ 1
- 0
lemoncurry/settings/base.py View File

@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/1.11/ref/settings/
12 12
 
13 13
 from os import path
14 14
 
15
+APPEND_SLASH = False
15 16
 
16 17
 ADMINS = [
17 18
     ('dani', 'dani@00dani.me'),

+ 4
- 0
lemoncurry/utils.py View File

@@ -26,6 +26,10 @@ def origin(request):
26 26
     return '{0}://{1}'.format(request.scheme, request.site.domain)
27 27
 
28 28
 
29
+def absolute_url(request, url):
30
+    return urljoin(origin(request), url)
31
+
32
+
29 33
 def uri(request):
30 34
     return origin(request) + request.path
31 35
 

+ 4
- 2
micropub/urls.py View File

@@ -1,7 +1,9 @@
1 1
 from django.urls import path
2
-from . import views
2
+from .views import micropub
3
+from .views.media import media
3 4
 
4 5
 app_name = 'micropub'
5 6
 urlpatterns = (
6
-    path('', views.micropub, name='micropub'),
7
+    path('', micropub, name='micropub'),
8
+    path('/media', media, name='media'),
7 9
 )

+ 54
- 0
micropub/views/media.py View File

@@ -0,0 +1,54 @@
1
+import hashlib
2
+
3
+from django.core.files.storage import default_storage as store
4
+from django.http import HttpResponse
5
+from django.views.decorators.csrf import csrf_exempt
6
+from django.views.decorators.http import require_POST
7
+import magic
8
+
9
+from lemonauth import tokens
10
+from lemoncurry.utils import absolute_url
11
+from . import error
12
+
13
+ACCEPTED_MEDIA_TYPES = (
14
+    'image/jpeg',
15
+    'image/png',
16
+)
17
+
18
+
19
+@csrf_exempt
20
+@require_POST
21
+def media(request):
22
+    token = tokens.auth(request)
23
+    if hasattr(token, 'content'):
24
+        return token
25
+    if 'file' not in request.FILES:
26
+        return error.bad_req(
27
+            "a file named 'file' must be provided to the media endpoint"
28
+        )
29
+    file = request.FILES['file']
30
+    if file.content_type not in ACCEPTED_MEDIA_TYPES:
31
+        return error.bad_req(
32
+            'unacceptable file type {0}'.format(file.content_type)
33
+        )
34
+
35
+    mime = None
36
+    sha = hashlib.sha256()
37
+    for chunk in file.chunks():
38
+        if mime is None:
39
+            mime = magic.from_buffer(chunk, mime=True)
40
+        sha.update(chunk)
41
+
42
+    if mime != file.content_type:
43
+        return error.bad_req(
44
+            'detected file type {0} did not match specified file type {1}'
45
+            .format(mime, file.content_type)
46
+        )
47
+
48
+    path = 'mp/{0[0]}/{2}.{1}'.format(*mime.split('/'), sha.hexdigest())
49
+    path = store.save(path, file)
50
+    url = absolute_url(request, store.url(path))
51
+
52
+    res = HttpResponse(status=201)
53
+    res['Location'] = url
54
+    return res

+ 3
- 0
micropub/views/query.py View File

@@ -1,10 +1,13 @@
1 1
 from django.http import JsonResponse
2
+from django.urls import reverse
2 3
 from lemoncurry import requests
4
+from lemoncurry.utils import absolute_url
3 5
 from . import error
4 6
 
5 7
 
6 8
 def config(request):
7 9
     config = syndicate_to(request)
10
+    config['media-endpoint'] = absolute_url(request, reverse('micropub:media'))
8 11
     return config
9 12
 
10 13
 

Loading…
Cancel
Save