I'm having this hash function for hashing an image with a new name:
def get_file_hash(file):
"""
Returns a new filename based on the file content using MD5 hashing.
It uses hashlib.md5() function from Python standard library to get
the hash.
Parameters
----------
file : werkzeug.datastructures.FileStorage
File sent by user.
Returns
-------
str
New filename based in md5 file hash.
"""
hasher = hashlib.md5()
basename = os.path.basename(file.filename)
filename, extension = os.path.splitext(basename)
file_read = file.read()
hasher.update(file_read)
processed_filename = hasher.hexdigest() + extension
return processed_filename
and I'm not passing the last evaluation of this test (the one which says "Check the file content is still readable:"):
def test_get_file_hash(self):
filename = "tests/dog.jpeg"
md5_filename = "0a7c757a80f2c5b13fa7a2a47a683593.jpeg"
with open(filename, "rb") as fp:
file = FileStorage(fp)
# Check the new filename is correct
new_filename = utils.get_file_hash(file)
self.assertEqual(md5_filename, new_filename, new_filename)
# Check the file content is still readable!
self.assertTrue(file.read() != b"")
I don't know how to make that happen after I call the function, in context I implement the hash function and then save the image:
filename = utils.get_file_hash(img_api)
img_api.save("static/uploads/" + filename)
Any thoughts would be very much appreciated, best
Thanks you very much Mathias: this is how the function finally pass the test just in case:
def get_file_hash(file):
"""
Returns a new filename based on the file content using MD5 hashing.
It uses hashlib.md5() function from Python standard library to get
the hash.
Parameters
----------
file : werkzeug.datastructures.FileStorage
File sent by user.
Returns
-------
str
New filename based in md5 file hash.
"""
hasher = hashlib.md5()
basename = os.path.basename(file.filename)
filename, extension = os.path.splitext(basename)
file_read = file.read()
hasher.update(file_read)
processed_filename = hasher.hexdigest() + extension
file.stream.seek(0)
return processed_filename
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.