|
@@ -3,9 +3,7 @@
|
|
# All rights reserved
|
|
# All rights reserved
|
|
#
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
-from hashlib import blake2b
|
|
|
|
-from io import BufferedRandom
|
|
|
|
-import os
|
|
|
|
|
|
+
|
|
from threading import Thread
|
|
from threading import Thread
|
|
from typing import Union
|
|
from typing import Union
|
|
from bottle import (
|
|
from bottle import (
|
|
@@ -16,15 +14,14 @@ from bottle import (
|
|
)
|
|
)
|
|
from psycopg import Cursor, connect
|
|
from psycopg import Cursor, connect
|
|
from psycopg.rows import TupleRow
|
|
from psycopg.rows import TupleRow
|
|
-from uuid import uuid4
|
|
|
|
|
|
|
|
from .validate import validate, validate_file, validate_parameter, validate_url
|
|
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 .route_decorators import normalize, poison, cursor
|
|
from .query_to_xml import get_categories, get_groups, get_products, get_tags
|
|
from .query_to_xml import get_categories, get_groups, get_products, get_tags
|
|
from .CachedLoadingPage import CachedLoadingPage
|
|
from .CachedLoadingPage import CachedLoadingPage
|
|
from .Cache import Cache
|
|
from .Cache import Cache
|
|
-from . import BOOLEAN, PARAMS, trend as worker
|
|
|
|
|
|
+from . import trend as worker
|
|
|
|
|
|
host = f"host={os.getenv('HOST')}"
|
|
host = f"host={os.getenv('HOST')}"
|
|
db = f"dbname={os.getenv('DB', 'grocery')}"
|
|
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 "")
|
|
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'])
|
|
@route('/clip', method=['GET', 'POST'])
|
|
def clip():
|
|
def clip():
|
|
if request.method == 'GET':
|
|
if request.method == 'GET':
|