-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't load images / documents into memory when calculating their hash
This can be done in chunks, which prevents crashes when uploading large files.
- Loading branch information
1 parent
dab2422
commit 4b38388
Showing
6 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from hashlib import sha1 | ||
from io import UnsupportedOperation | ||
|
||
from django.utils.encoding import force_bytes | ||
|
||
HASH_READ_SIZE = 65536 # 64k | ||
|
||
|
||
def hash_filelike(filelike): | ||
""" | ||
Compute the hash of a file-like object, without loading it all into memory. | ||
""" | ||
file_pos = 0 | ||
if hasattr(filelike, "tell"): | ||
file_pos = filelike.tell() | ||
|
||
try: | ||
# Reset file handler to the start of the file so we hash it all | ||
filelike.seek(0) | ||
except (AttributeError, UnsupportedOperation): | ||
pass | ||
|
||
hasher = sha1() | ||
while True: | ||
data = filelike.read(HASH_READ_SIZE) | ||
if not data: | ||
break | ||
# Use `force_bytes` to account for files opened as text | ||
hasher.update(force_bytes(data)) | ||
|
||
if hasattr(filelike, "seek"): | ||
# Reset the file handler to where it was before | ||
filelike.seek(file_pos) | ||
|
||
return hasher.hexdigest() |