@@ -32,14 +32,6 @@ class PatchHandler extends BaseHandler {
3232 throw ERRORS . INVALID_CONTENT_TYPE ;
3333 }
3434
35- // The request MUST validate upload-length related headers
36- const upload_length = req . headers [ 'upload-length' ] ;
37- const upload_defer_length = req . headers [ 'upload-defer-length' ] ;
38-
39- if ( ( upload_length === undefined ) === ( upload_defer_length === undefined ) ) {
40- throw ERRORS . INVALID_LENGTH ;
41- }
42-
4335 const file = await this . store . getOffset ( file_id ) ;
4436
4537 if ( file . size !== offset ) {
@@ -48,22 +40,29 @@ class PatchHandler extends BaseHandler {
4840 throw ERRORS . INVALID_OFFSET ;
4941 }
5042
51- // Throw error is upload-length header does not match current upload-length if it is set.
52- if ( upload_length !== undefined && file . upload_length !== undefined && upload_length !== file . upload_length ) {
53- throw ERRORS . INVALID_LENGTH ;
54- }
55- // Throw error is upload-defer-length header is present while upload-length is already known
56- else if ( upload_defer_length !== undefined && file . upload_length !== undefined ) {
57- throw ERRORS . INVALID_LENGTH ;
58- }
43+ // The request MUST validate upload-length related headers
44+ const upload_length = req . headers [ 'upload-length' ] ;
45+ if ( upload_length !== undefined ) {
46+ // Throw error if extension is not supported
47+ if ( ! this . store . hasExtension ( 'creation-defer-length' ) ) {
48+ throw ERRORS . UNSUPPORTED_CREATION_DEFER_LENGTH_EXTENSION ;
49+ }
50+
51+ // Throw error if upload-length is already set.
52+ if ( file . upload_length !== undefined ) {
53+ throw ERRORS . INVALID_LENGTH ;
54+ }
55+
56+ if ( parseInt ( upload_length , 10 ) < file . size ) {
57+ throw ERRORS . INVALID_LENGTH ;
58+ }
5959
60- // Declare upload-length if it is not already known
61- if ( upload_length !== undefined && file . upload_length === undefined ) {
6260 await this . store . declareUploadLength ( file_id , upload_length ) ;
61+ file . upload_length = upload_length ;
6362 }
6463
6564 const new_offset = await this . store . write ( req , file_id , offset ) ;
66- if ( new_offset === parseInt ( upload_length , 10 ) ) {
65+ if ( new_offset === parseInt ( file . upload_length , 10 ) ) {
6766 this . emit ( EVENTS . EVENT_UPLOAD_COMPLETE , { file : new File ( file_id , file . upload_length , file . upload_defer_length , file . upload_metadata ) } ) ;
6867 }
6968
0 commit comments