|
@@ -1,60 +0,0 @@
|
|
-#
|
|
|
|
-# Copyright (c) Daniel Sheffield 2024
|
|
|
|
-# All rights reserved
|
|
|
|
-#
|
|
|
|
-# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
|
-
|
|
|
|
-from io import BufferedRandom, BytesIO
|
|
|
|
-import os
|
|
|
|
-import shutil
|
|
|
|
-from uuid import uuid4
|
|
|
|
-
|
|
|
|
-from .hash_util import blake_file, bytes_to_base32
|
|
|
|
-from .qr import get_qr_code
|
|
|
|
-
|
|
|
|
-def save_qr(qr: bytes, _b32:str , directory: str) -> str:
|
|
|
|
- fd = os.open(f'{directory}/{_b32}.qr', os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o600)
|
|
|
|
- with open(fd, "wb") as f:
|
|
|
|
- f.write(qr)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def save_filename(name: str, _b32: str, directory: str):
|
|
|
|
- fd = os.open(f'{directory}/{_b32}.name', os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o600)
|
|
|
|
- with open(fd, "w") as f:
|
|
|
|
- f.write(name)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def save_upload(name: str, location: str, content: BufferedRandom, root='rest/static/files') -> 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:
|
|
|
|
- done = False
|
|
|
|
- if isinstance(content, BytesIO):
|
|
|
|
- f.write(content.getvalue())
|
|
|
|
- done = True
|
|
|
|
- while not done and content.peek(1):
|
|
|
|
- seg = content.read(1024)
|
|
|
|
- f.write(seg)
|
|
|
|
-
|
|
|
|
- _bytes = blake_file(unique.hex, person='upload'.encode('utf-8'), root=tmpdir)
|
|
|
|
- _b32 = bytes_to_base32(_bytes)
|
|
|
|
-
|
|
|
|
- directory = f'{root}/{_b32}'
|
|
|
|
- try:
|
|
|
|
- os.mkdir(directory, mode=0o700, dir_fd=None)
|
|
|
|
- except FileExistsError:
|
|
|
|
- pass
|
|
|
|
-
|
|
|
|
- shutil.move(f'{tmpdir}/{unique.hex}', f'{directory}/{_b32}.file')
|
|
|
|
- save_filename(name, _b32, directory)
|
|
|
|
- link = f'{location}/upload/{_b32}'
|
|
|
|
- svg = get_qr_code(link, fallback=link)
|
|
|
|
- save_qr(svg, _b32, directory)
|
|
|
|
-
|
|
|
|
- return _b32
|
|
|