@@ -32,14 +32,6 @@ class PatchHandler extends BaseHandler {
32
32
throw ERRORS . INVALID_CONTENT_TYPE ;
33
33
}
34
34
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
-
43
35
const file = await this . store . getOffset ( file_id ) ;
44
36
45
37
if ( file . size !== offset ) {
@@ -48,22 +40,29 @@ class PatchHandler extends BaseHandler {
48
40
throw ERRORS . INVALID_OFFSET ;
49
41
}
50
42
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
+ }
59
59
60
- // Declare upload-length if it is not already known
61
- if ( upload_length !== undefined && file . upload_length === undefined ) {
62
60
await this . store . declareUploadLength ( file_id , upload_length ) ;
61
+ file . upload_length = upload_length ;
63
62
}
64
63
65
64
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 ) ) {
67
66
this . emit ( EVENTS . EVENT_UPLOAD_COMPLETE , { file : new File ( file_id , file . upload_length , file . upload_defer_length , file . upload_metadata ) } ) ;
68
67
}
69
68
0 commit comments