3
3
- Would originally be in Flask's request.file
4
4
- Value will be a FileStorage object
5
5
"""
6
+ import io
7
+
8
+ from werkzeug .datastructures import FileStorage
9
+
6
10
from .parameter import Parameter
7
11
8
12
@@ -21,7 +25,7 @@ def __init__(
21
25
self .min_length = min_length
22
26
self .max_length = max_length
23
27
24
- def validate (self , value ):
28
+ def validate (self , value : FileStorage ):
25
29
# Content type validation
26
30
if self .content_types is not None :
27
31
# We check mimetype, as it strips charset etc.
@@ -31,15 +35,19 @@ def validate(self, value):
31
35
32
36
# Min content length validation
33
37
if self .min_length is not None :
34
- if value .content_length < self .min_length :
38
+ origin = value .stream .tell ()
39
+ if value .stream .seek (0 , io .SEEK_END ) < self .min_length :
35
40
raise ValueError (
36
41
f"must have a content-length at least { self .min_length } ."
37
42
)
43
+ value .stream .seek (origin )
38
44
39
45
# Max content length validation
40
46
if self .max_length is not None :
41
- if value .content_length > self .max_length :
47
+ origin = value .stream .tell ()
48
+ if value .stream .seek (0 , io .SEEK_END ) > self .max_length :
42
49
raise ValueError (
43
50
f"must have a content-length at most { self .max_length } ."
44
51
)
52
+ value .stream .seek (origin )
45
53
return True
0 commit comments