@@ -19,27 +19,40 @@ class FilerUploadParser(FileUploadParser):
1919 """
2020 media_type = 'filer/octet-stream'
2121
22+ errors = {
23+ "no_filepath" : (
24+ "Missing filepath. Request should include a Content-Disposition "
25+ "header with a filepath parameter."
26+ ),
27+ }
28+ errors .update (FileUploadParser .errors )
29+
2230 def parse (self , stream , media_type = None , parser_context = None ):
2331 request = parser_context ['request' ]
2432 filename = self .get_filename (stream , media_type , parser_context )
2533 if not filename :
2634 raise ParseError (self .errors ['no_filename' ])
27-
35+ filepath = self .get_filepath (stream , media_type , parser_context )
36+ if not filepath :
37+ raise ParseError (self .errors ['no_filepath' ])
2838 try :
2939 content_length = int (request .META .get ('HTTP_CONTENT_LENGTH' ,
3040 request .META .get ('CONTENT_LENGTH' , 0 )))
3141 except (ValueError , TypeError ):
3242 content_length = None
43+ return DataAndFiles ({}, {'file' : FilerFile (filename , filepath , stream , content_length )})
3344
34- file_meta = self .get_file_meta (request .META )
35- return DataAndFiles ({}, {'file' : FilerFile (
36- file_meta ['filename' ], file_meta ['filepath' ], stream , content_length )})
37-
38- def get_file_meta (self , META ):
45+ def get_filepath (self , stream , media_type , parser_context ):
3946 """
40- Detects the uploaded file name. First searches a 'filename ' url kwarg.
47+ Detects the uploaded file name. First searches a 'filepath ' url kwarg.
4148 Then tries to parse Content-Disposition header.
4249 """
50+ with contextlib .suppress (KeyError ):
51+ return parser_context ['kwargs' ]['filepath' ]
52+
4353 with contextlib .suppress (AttributeError , KeyError , ValueError ):
44- _ , params = parse_header_parameters (META ['HTTP_CONTENT_DISPOSITION' ])
45- return params
54+ meta = parser_context ['request' ].META
55+ _ , params = parse_header_parameters (meta ['HTTP_CONTENT_DISPOSITION' ])
56+ if 'filepath*' in params :
57+ return params ['filepath*' ]
58+ return params ['filepath' ]
0 commit comments