]> git.ipfire.org Git - pbs.git/commitdiff
uploads: Compute checksum for locally uploaded files
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 30 Mar 2025 17:08:30 +0000 (17:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 30 Mar 2025 17:08:30 +0000 (17:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/uploads.py

index 7e827f4a62ff31e87126dd7f5f3a15c94111ce17..e8efd51c6a5a861fac5255b7e9261bb0320e93b2 100644 (file)
@@ -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"