From: Michael Tremer Date: Sun, 30 Mar 2025 17:08:30 +0000 (+0000) Subject: uploads: Compute checksum for locally uploaded files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4df49f92a545e93e15df63eca8d3295bf7495efb;p=pbs.git uploads: Compute checksum for locally uploaded files Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/uploads.py b/src/buildservice/uploads.py index 7e827f4a..e8efd51c 100644 --- a/src/buildservice/uploads.py +++ b/src/buildservice/uploads.py @@ -89,10 +89,14 @@ class Uploads(base.Object): # Fetch the size size = os.path.getsize(path) + # Compute the digest + digest_blake2b512 = self.digest(path, algo="blake2b512") + # Create the new upload object upload = await self.create( - filename = filename, - size = size, + filename = filename, + size = size, + digest_blake2b512 = digest_blake2b512, **kwargs, ) @@ -120,6 +124,25 @@ class Uploads(base.Object): async with await self.db.transaction(): await upload.delete() + def digest(self, path, algo="blake2b512"): + """ + Hashes the given file + """ + # Create a new hash function + h = hashlib.new(algo) + + # Feed the entire file into the hash function + with open(path, "rb") as f: + while True: + chunk = f.read(4096) + if not chunk: + break + + h.update(chunk) + + # Return the digest + return h.digest() + class Upload(database.Base, database.BackendMixin): __tablename__ = "uploads"