@@ -76,11 +76,11 @@ public function generateToken($args, $dirname = '')
76
76
if (!array_key_exists ('filename ' , $ args )) {
77
77
throw new Exception ('Parameter filename is not defined ' , MIDAS_HTTPUPLOAD_FILENAME_PARAM_UNDEFINED );
78
78
}
79
- $ dir = $ dirname == '' ? '' : '/ ' .$ dirname ;
79
+ $ dir = $ dirname === '' ? '' : '/ ' .$ dirname ;
80
80
$ dir = UtilityComponent::getTempDirectory ().$ dir ;
81
81
82
82
if (!file_exists ($ dir )) {
83
- if (!mkdir ($ dir , 0700 , true )) {
83
+ if (!mkdir ($ dir , 0777 , true )) {
84
84
throw new Exception ('Failed to create temporary upload dir ' , MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED );
85
85
}
86
86
}
@@ -90,10 +90,16 @@ public function generateToken($args, $dirname = '')
90
90
if ($ dirname != '' ) {
91
91
$ uniqueIdentifier = $ dirname .'/ ' .$ uniqueIdentifier ;
92
92
}
93
- if (file_exists (UtilityComponent::getTempDirectory ().'/ ' .$ uniqueIdentifier )) {
93
+
94
+ $ path = UtilityComponent::getTempDirectory ().'/ ' .$ uniqueIdentifier ;
95
+ if (file_exists ($ path )) {
94
96
throw new Exception ('Failed to generate upload token ' , MIDAS_HTTPUPLOAD_UPLOAD_TOKEN_GENERATION_FAILED );
95
97
}
96
- touch (UtilityComponent::getTempDirectory ().'/ ' .$ uniqueIdentifier );
98
+
99
+ if (touch ($ path ) === false ) {
100
+ mkdir ($ path , 0777 , true );
101
+ $ uniqueIdentifier .= '/ ' ;
102
+ }
97
103
98
104
return array ('token ' => $ uniqueIdentifier );
99
105
}
@@ -123,48 +129,54 @@ public function process($args)
123
129
if (!array_key_exists ('length ' , $ args )) {
124
130
throw new Exception ('Parameter length is not defined ' , MIDAS_HTTPUPLOAD_PARAM_UNDEFINED );
125
131
}
126
- $ length = (float ) ($ args ['length ' ]);
132
+ $ length = (int ) ($ args ['length ' ]);
127
133
128
134
if ($ this ->testingEnable && array_key_exists ('localinput ' , $ args )) {
129
- $ localinput = array_key_exists ('localinput ' , $ args ) ? $ args ['localinput ' ] : false ;
135
+ $ localInput = array_key_exists ('localinput ' , $ args ) ? $ args ['localinput ' ] : false ;
130
136
}
131
137
132
- // check if the temporary file exists
133
- $ pathTemporaryFilename = UtilityComponent::getTempDirectory ().'/ ' .$ uploadToken ;
134
- if (!file_exists ($ pathTemporaryFilename )) {
138
+ $ temporaryPath = UtilityComponent::getTempDirectory ().'/ ' .$ uploadToken ;
139
+ if (!file_exists ($ temporaryPath )) {
135
140
throw new Exception (
136
- 'Invalid upload token ' .$ pathTemporaryFilename , MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN
141
+ 'Invalid upload token ' .$ uploadToken , MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN
137
142
);
138
- } else {
139
- $ uploadOffset = UtilityComponent::fileSize ($ pathTemporaryFilename );
140
143
}
141
144
142
- // can't do streaming checksum if we have a partial file already.
143
- $ streamChecksum = $ uploadOffset == 0 ;
145
+ if (substr ($ temporaryPath , -1 ) === '/ ' ) {
146
+ @rmdir ($ temporaryPath );
147
+ }
144
148
145
- ignore_user_abort ( true ) ;
149
+ $ uploadOffset = file_exists ( $ temporaryPath ) ? UtilityComponent:: fileSize ( $ temporaryPath ) : 0 ;
146
150
147
- $ inputfile = 'php://input ' ; // Stream (Client -> Server) Mode: Read, Binary
148
- if ($ this ->testingEnable && array_key_exists ('localinput ' , $ args )) {
149
- $ inputfile = $ localinput ; // Stream (LocalServerFile -> Server) Mode: Read, Binary
150
- }
151
+ // can't do streaming checksum if we have a partial file already.
152
+ $ streamChecksum = $ uploadOffset === 0 ;
151
153
152
- $ in = fopen ($ inputfile , 'rb ' ); // Stream (LocalServerFile -> Server) Mode: Read, Binary
153
- if ($ in === false ) {
154
- throw new Exception ('Failed to open [ ' .$ inputfile .'] source ' , MIDAS_HTTPUPLOAD_INPUT_FILE_OPEN_FAILED );
155
- }
154
+ ignore_user_abort (true );
156
155
157
156
// open target output
158
- $ out = fopen ($ pathTemporaryFilename , 'ab ' ); // Stream (Server -> TempFile) Mode: Append, Binary
157
+ $ out = fopen ($ temporaryPath , 'ab ' ); // Stream (Server -> TempFile) Mode: Append, Binary
159
158
if ($ out === false ) {
160
159
throw new Exception (
161
- 'Failed to open output file [ ' .$ pathTemporaryFilename .'] ' ,
160
+ 'Failed to open output file [ ' .$ temporaryPath .'] ' ,
162
161
MIDAS_HTTPUPLOAD_OUTPUT_FILE_OPEN_FAILED
163
162
);
164
163
}
165
164
165
+ $ inputFile = 'php://input ' ; // Stream (Client -> Server) Mode: Read, Binary
166
+ if ($ this ->testingEnable && array_key_exists ('localinput ' , $ args )) {
167
+ $ inputFile = $ localInput ; // Stream (LocalServerFile -> Server) Mode: Read, Binary
168
+ }
169
+
170
+ $ in = fopen ($ inputFile , 'rb ' ); // Stream (LocalServerFile -> Server) Mode: Read, Binary
171
+ if ($ in === false ) {
172
+ fclose ($ out );
173
+ throw new Exception ('Failed to open [ ' .$ inputFile .'] source ' , MIDAS_HTTPUPLOAD_INPUT_FILE_OPEN_FAILED );
174
+ }
175
+
166
176
if ($ streamChecksum ) {
167
- $ hashctx = hash_init ('md5 ' );
177
+ $ hashContext = hash_init ('md5 ' );
178
+ } else {
179
+ $ hashContext = null ;
168
180
}
169
181
170
182
// read from input and write into file
@@ -177,7 +189,7 @@ public function process($args)
177
189
$ bufSize = $ length - $ uploadOffset ;
178
190
}
179
191
if ($ streamChecksum ) {
180
- hash_update ($ hashctx , $ buf );
192
+ hash_update ($ hashContext , $ buf );
181
193
}
182
194
}
183
195
fclose ($ in );
@@ -191,9 +203,9 @@ public function process($args)
191
203
}
192
204
193
205
$ data ['filename ' ] = $ filename ;
194
- $ data ['path ' ] = $ pathTemporaryFilename ;
206
+ $ data ['path ' ] = $ temporaryPath ;
195
207
$ data ['size ' ] = $ uploadOffset ;
196
- $ data ['md5 ' ] = $ streamChecksum ? hash_final ($ hashctx ) : '' ;
208
+ $ data ['md5 ' ] = $ streamChecksum ? hash_final ($ hashContext ) : '' ;
197
209
198
210
return $ data ;
199
211
}
0 commit comments