mongodb / flask-pymongo

PyMongo support for Flask applications
BSD 2-Clause "Simplified" License
721 stars 175 forks source link

send_file throwing an error in werkzeug by passing a None-type to function that requires str #153

Open scoofy opened 2 years ago

scoofy commented 2 years ago

I upgraded to python 3.9 and i've run into some problems. When running:

mongo.send_file(filename)

Running tests, when I'm grabbing an image file from my mongo database, it seems to not be returning an MD5 in the fileobj, which causes an error here:

response.set_etag(fileobj.md5)

this seems to throw an error with werkzeug/http.py

line 850, in quote_etag
if '"' in etag:

TypeError: argument of type 'NoneType' is not iterable

I'm not exactly sure if this is the fault of flask-pymongo, or why i'm getting the error. But when i remove the set_etag line, it works just fine. The werkzeug file has a type hint that the etag must be a str, so it seems that you may want to turn a None instance into an empty string before running that function.

scoofy commented 2 years ago

I realized the issue is a PyMongo 3.x -> 4 problem.

shigongqiang commented 2 years ago

GridFS changes disable_md5 parameter is removed Removed the disable_md5 option for GridFSBucket and GridFS. GridFS no longer generates checksums. Applications that desire a file digest should implement it outside GridFS and store it with other file metadata. For example:

import hashlib my_db = MongoClient().test fs = GridFSBucket(my_db) grid_in = fs.open_upload_stream("test_file") file_data = b'...' sha356 = hashlib.sha256(file_data).hexdigest() grid_in.write(file_data) grid_in.sha356 = sha356 # Set the custom 'sha356' field grid_in.close() Note that for large files, the checksum may need to be computed in chunks to avoid the excessive memory needed to load the entire file at once.