File tree Expand file tree Collapse file tree 3 files changed +11
-26
lines changed Expand file tree Collapse file tree 3 files changed +11
-26
lines changed Original file line number Diff line number Diff line change 28
28
29
29
from functools import partial
30
30
from mimetypes import guess_type
31
+ import hashlib
31
32
32
33
from flask import abort , current_app , request
33
34
from gridfs import GridFS , NoFile
@@ -163,14 +164,20 @@ def get_upload(filename):
163
164
# mostly copied from flask/helpers.py, with
164
165
# modifications for GridFS
165
166
data = wrap_file (request .environ , fileobj , buffer_size = 1024 * 255 )
167
+ content_type , _ = guess_type (filename )
166
168
response = current_app .response_class (
167
169
data ,
168
- mimetype = fileobj . content_type ,
170
+ mimetype = content_type ,
169
171
direct_passthrough = True ,
170
172
)
171
173
response .content_length = fileobj .length
172
174
response .last_modified = fileobj .upload_date
173
- response .set_etag (fileobj .md5 )
175
+ # Compute the sha1 sum of the file for the etag.
176
+ pos = fileobj .tell ()
177
+ raw_data = fileobj .read ()
178
+ fileobj .seek (pos )
179
+ digest = hashlib .sha1 (raw_data ).hexdigest ()
180
+ response .set_etag (digest )
174
181
response .cache_control .max_age = cache_for
175
182
response .cache_control .public = True
176
183
response .make_conditional (request )
Original file line number Diff line number Diff line change 1
- from hashlib import md5
1
+ from hashlib import sha1
2
2
from io import BytesIO
3
3
4
4
from bson .objectid import ObjectId
@@ -30,15 +30,6 @@ def test_it_saves_files(self):
30
30
gridfs = GridFS (self .mongo .db )
31
31
assert gridfs .exists ({"filename" : "my-file" })
32
32
33
- def test_it_guesses_type_from_filename (self ):
34
- fileobj = BytesIO (b"these are the bytes" )
35
-
36
- self .mongo .save_file ("my-file.txt" , fileobj )
37
-
38
- gridfs = GridFS (self .mongo .db )
39
- gridfile = gridfs .find_one ({"filename" : "my-file.txt" })
40
- assert gridfile .content_type == "text/plain"
41
-
42
33
def test_it_saves_files_with_props (self ):
43
34
fileobj = BytesIO (b"these are the bytes" )
44
35
@@ -82,7 +73,7 @@ def test_it_sets_supports_conditional_gets(self):
82
73
environ_args = {
83
74
"method" : "GET" ,
84
75
"headers" : {
85
- "If-None-Match" : md5 (self .myfile .getvalue ()).hexdigest (),
76
+ "If-None-Match" : sha1 (self .myfile .getvalue ()).hexdigest (),
86
77
},
87
78
}
88
79
Original file line number Diff line number Diff line change @@ -18,16 +18,3 @@ def test_find_one_or_404(self):
18
18
# now it should not raise
19
19
thing = self .mongo .db .things .find_one_or_404 ({"_id" : "thing" })
20
20
assert thing ["val" ] == "foo" , "got wrong thing"
21
-
22
- # also test with dotted-named collections
23
- self .mongo .db .things .morethings .delete_many ({})
24
- try :
25
- self .mongo .db .things .morethings .find_one_or_404 ({"_id" : "thing" })
26
- except HTTPException as notfound :
27
- assert notfound .code == 404 , "raised wrong exception"
28
-
29
- self .mongo .db .things .morethings .insert_one ({"_id" : "thing" , "val" : "foo" })
30
-
31
- # now it should not raise
32
- thing = self .mongo .db .things .morethings .find_one_or_404 ({"_id" : "thing" })
33
- assert thing ["val" ] == "foo" , "got wrong thing"
You can’t perform that action at this time.
0 commit comments