Răsfoiți Sursa

reuse blake_file method

Daniel Sheffield 1 an în urmă
părinte
comite
6d2de97926
1 a modificat fișierele cu 4 adăugiri și 10 ștergeri
  1. 4 10
      rest/save.py

+ 4 - 10
rest/save.py

@@ -4,13 +4,12 @@
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
 
-from hashlib import blake2b
 from io import BufferedRandom, BytesIO
 import os
 import shutil
 from uuid import uuid4
 
-from .hash_util import DIGEST_SIZE_BYTES, blake, bytes_to_base32
+from .hash_util import DIGEST_SIZE_BYTES, blake, blake_file, bytes_to_base32
 from .qr import get_qr_code
 
 def save_qr(qr: bytes, _b32:str , directory: str) -> str:
@@ -61,14 +60,9 @@ def save_upload(name: str, location: str, content: BufferedRandom, root='rest/st
             seg = content.read(1024)
             f.write(seg)
     
-    with open(f'{tmpdir}/{unique.hex}', "rb") as f:
-        _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)
-    
+    _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)