Daniel Sheffield hai 1 ano
pai
achega
39854500df
Modificáronse 2 ficheiros con 62 adicións e 53 borrados
  1. 3 53
      app/rest/pyapi.py
  2. 59 0
      app/rest/save.py

+ 3 - 53
app/rest/pyapi.py

@@ -3,9 +3,7 @@
 # All rights reserved
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
-from hashlib import blake2b
-from io import BufferedRandom
-import os
+
 from threading import Thread
 from typing import Union
 from bottle import (
@@ -16,15 +14,14 @@ from bottle import (
 )
 from psycopg import Cursor, connect
 from psycopg.rows import TupleRow
-from uuid import uuid4
 
 from .validate import validate, validate_file, validate_parameter, validate_url
-from .hash_util import DIGEST_SIZE_BYTES, blake, bytes_to_base32, normalize_base32
+from .hash_util import normalize_base32
 from .route_decorators import normalize, poison, cursor
 from .query_to_xml import get_categories, get_groups, get_products, get_tags
 from .CachedLoadingPage import CachedLoadingPage
 from .Cache import Cache
-from . import BOOLEAN, PARAMS, trend as worker
+from . import trend as worker
 
 host = f"host={os.getenv('HOST')}"
 db = f"dbname={os.getenv('DB', 'grocery')}"
@@ -100,53 +97,6 @@ PORT = ""
 LOCATION = SCHEME + (f"{HOST}." if HOST else "") + DOMAIN + (f":{PORT}" if PORT else "")
 
 
-def save(content: bytes, root='app/rest/static') -> str:
-    _bytes = blake(content, person='clip'.encode('utf-8'))
-    _b32 = bytes_to_base32(_bytes)
-    directory = f'{root}/{_b32}'
-    try:
-        os.mkdir(directory, mode=0o700, dir_fd=None)
-    except FileExistsError:
-        pass
-    fd = os.open(f'{directory}/{_b32}.file', os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o600)
-    with open(fd, "wb") as f:
-        f.write(content)
-    return _b32
-
-def save_upload(content: BufferedRandom, root='app/rest/static') -> str:
-    tmpdir = '/tmp/upload'
-    try:
-        os.mkdir(tmpdir, mode=0o700, dir_fd=None)
-    except FileExistsError:
-        pass
-    unique = uuid4()
-    fd = os.open(f'{tmpdir}/{unique.hex}', os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o600)
-    with open(fd, "wb") as f:
-        while content.peek(1):
-            seg = content.read(1024)
-            f.write(seg)
-    
-    fd = os.open(f'{tmpdir}/{unique.hex}', os.O_RDONLY, 0o600)
-    with open(fd, "rb") as f:
-        f.seek(0)
-        _blake = blake2b(usedforsecurity=False, digest_size=DIGEST_SIZE_BYTES, person='upload'.encode('utf-8'))
-        while f.peek(1):
-            _blake.update(f.read(1024))
-        
-        _bytes = _blake.digest()
-        _b32 = bytes_to_base32(_bytes)
-    
-    directory = f'{root}/{_b32}'
-    try:
-        os.mkdir(directory, mode=0o700, dir_fd=None)
-    except FileExistsError:
-        pass
-
-    os.replace(f'{tmpdir}/{unique.hex}', f'{directory}/{_b32}.file')
-    
-    return _b32
-
-
 @route('/clip', method=['GET', 'POST'])
 def clip():
     if request.method == 'GET':

+ 59 - 0
app/rest/save.py

@@ -0,0 +1,59 @@
+#
+# Copyright (c) Daniel Sheffield 2023
+# All rights reserved
+#
+# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
+
+from hashlib import blake2b
+from io import BufferedRandom
+import os
+from uuid import uuid4
+from .hash_util import DIGEST_SIZE_BYTES, blake, bytes_to_base32
+
+
+def save(content: bytes, root='app/rest/static') -> str:
+    _bytes = blake(content, person='clip'.encode('utf-8'))
+    _b32 = bytes_to_base32(_bytes)
+    directory = f'{root}/{_b32}'
+    try:
+        os.mkdir(directory, mode=0o700, dir_fd=None)
+    except FileExistsError:
+        pass
+    fd = os.open(f'{directory}/{_b32}.file', os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o600)
+    with open(fd, "wb") as f:
+        f.write(content)
+    return _b32
+
+def save_upload(content: BufferedRandom, root='app/rest/static') -> str:
+    tmpdir = '/tmp/upload'
+    try:
+        os.mkdir(tmpdir, mode=0o700, dir_fd=None)
+    except FileExistsError:
+        pass
+    unique = uuid4()
+    fd = os.open(f'{tmpdir}/{unique.hex}', os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o600)
+    with open(fd, "wb") as f:
+        while content.peek(1):
+            seg = content.read(1024)
+            f.write(seg)
+    
+    fd = os.open(f'{tmpdir}/{unique.hex}', os.O_RDONLY, 0o600)
+    with open(fd, "rb") as f:
+        f.seek(0)
+        _blake = blake2b(usedforsecurity=False, digest_size=DIGEST_SIZE_BYTES, person='upload'.encode('utf-8'))
+        while f.peek(1):
+            _blake.update(f.read(1024))
+        
+        _bytes = _blake.digest()
+        _b32 = bytes_to_base32(_bytes)
+    
+    directory = f'{root}/{_b32}'
+    try:
+        os.mkdir(directory, mode=0o700, dir_fd=None)
+    except FileExistsError:
+        pass
+
+    os.replace(f'{tmpdir}/{unique.hex}', f'{directory}/{_b32}.file')
+    
+    return _b32
+