@@ -189,12 +189,29 @@ function resourceSearch($args)
189
189
190
190
/**
191
191
* Generate a unique upload token
192
+ * @param itemid The id of the parent item to upload into
193
+ * @param filename The filename of the bitstream you will upload
192
194
* @return An upload token that can be used to upload a file
193
195
*/
194
196
function uploadGeneratetoken ($ args )
195
197
{
198
+ $ this ->_validateParams ($ args , array ('itemid ' , 'filename ' ));
199
+ $ userDao = $ this ->_getUser ($ args );
200
+ if (!$ userDao )
201
+ {
202
+ throw new Exception ('Anonymous users may not upload ' , MIDAS_INVALID_POLICY );
203
+ }
204
+
205
+ $ modelLoader = new MIDAS_ModelLoader ();
206
+ $ itemModel = $ modelLoader ->loadModel ('Item ' );
207
+ $ item = $ itemModel ->load ($ args ['itemid ' ]);
208
+ if (!$ itemModel ->policyCheck ($ item , $ userDao , MIDAS_POLICY_WRITE ))
209
+ {
210
+ throw new Exception ('Invalid policy or itemid ' , MIDAS_INVALID_POLICY );
211
+ }
212
+
196
213
$ uploadApi = new KwUploadAPI ($ this ->apiSetup );
197
- return $ uploadApi ->generateToken ($ args );
214
+ return $ uploadApi ->generateToken ($ args, $ userDao -> getKey (). ' / ' . $ item -> getKey () );
198
215
}
199
216
200
217
/**
@@ -209,50 +226,79 @@ function uploadGetoffset($args)
209
226
}
210
227
211
228
/**
212
- * Upload a file to the server. PUT or POST is required
213
- * @param token The upload token (see upload.generatetoken)
229
+ * Upload a file to the server. PUT or POST is required. Either the itemid or folderid parameter must be set
230
+ * @param uploadtoken The upload token (see upload.generatetoken)
231
+ * @param filename The upload filename
232
+ * @param length The length in bytes of the file being uploaded
214
233
* @param mode (Optional) Stream or multipart. Default is stream
215
- * @param folder_id The id of the folder to upload into
216
- * @param item_id (Optional) If set, will create a new revision in the existing item
217
- * @param revision (Optional) If set, will add a new file into an existing revision
234
+ * @param folderid (Optional) The id of the folder to upload into
235
+ * @param itemid (Optional) If set, will create a new revision in the existing item
236
+ * @param revision (Optional) If set, will add a new file into an existing revision. Set this to "head" to add to the most recent revision.
218
237
* @param return The item information of the item created or changed
219
238
*/
220
239
function uploadPerform ($ args )
221
240
{
241
+ $ this ->_validateParams ($ args , array ('uploadtoken ' , 'filename ' , 'length ' ));
222
242
if (!$ this ->controller ->getRequest ()->isPost () && !$ this ->controller ->getRequest ()->isPut ())
223
243
{
224
244
throw new Exception ('POST or PUT method required ' , MIDAS_HTTP_ERROR );
225
245
}
226
246
227
- $ userDao = $ this ->_getUser ($ args );
247
+ list ($ userid , $ resourceid , ) = explode ('/ ' , $ args ['uploadtoken ' ]);
248
+ //TODO check if this upload token is valid
228
249
229
- if ($ userDao == false )
250
+ $ modelLoader = new MIDAS_ModelLoader ();
251
+ $ itemModel = $ modelLoader ->loadModel ('Item ' );
252
+ $ userModel = $ modelLoader ->loadModel ('User ' );
253
+ $ userDao = $ userModel ->load ($ userid );
254
+ if (!$ userDao )
230
255
{
231
- throw new Exception ('Please log in ' , MIDAS_INVALID_POLICY );
256
+ throw new Exception ('Invalid user id from upload token ' , MIDAS_INVALID_PARAMETER );
232
257
}
233
258
234
- $ modelLoader = new MIDAS_ModelLoader ();
235
- $ itemModel = $ modelLoader ->loadModel ('Item ' );
236
- if (array_key_exists ('revision ' , $ args ) && array_key_exists ('item_id ' , $ args ))
259
+ if (array_key_exists ('revision ' , $ args ) && array_key_exists ('itemid ' , $ args ))
237
260
{
238
- $ item = $ itemModel ->load ($ args ['item_id ' ]);
261
+ if ($ args ['itemid ' ] != $ resourceid )
262
+ {
263
+ throw new Exception ('Upload token does not match itemid ' , MIDAS_INVALID_PARAMETER );
264
+ }
265
+ $ item = $ itemModel ->load ($ args ['itemid ' ]);
239
266
if ($ item == false )
240
267
{
241
268
throw new Exception ('Unable to find item ' , MIDAS_INVALID_PARAMETER );
242
269
}
243
- if (! $ itemModel -> policyCheck ( $ item , $ userDao , MIDAS_POLICY_WRITE ) )
270
+ if (strtolower ( $ args [ ' revision ' ]) == ' head ' )
244
271
{
245
- throw new Exception ('Permission error ' , MIDAS_INVALID_PARAMETER );
272
+ $ revision = $ itemModel ->getLastRevision ($ item );
273
+
274
+ if ($ revision == false )
275
+ {
276
+ // Create new revision if none exists yet
277
+ Zend_Loader::loadClass ('ItemRevisionDao ' , BASE_PATH .'/core/models/dao ' );
278
+ $ revision = new ItemRevisionDao ();
279
+ $ revision ->setChanges ('Initial revision ' );
280
+ $ revision ->setUser_id ($ userDao ->getKey ());
281
+ $ revision ->setDate (date ('c ' ));
282
+ $ revision ->setLicense (null );
283
+ $ revision = $ itemModel ->addRevision ($ item , $ revision );
284
+ }
246
285
}
247
- $ revision = $ itemModel ->getRevision ($ item , $ args ['revision ' ]);
248
- if ($ revision == false )
286
+ else
249
287
{
250
- throw new Exception ('Unable to find revision ' , MIDAS_INVALID_PARAMETER );
288
+ $ revision = $ itemModel ->getRevision ($ item , $ args ['revision ' ]);
289
+ if ($ revision == false )
290
+ {
291
+ throw new Exception ('Unable to find revision ' , MIDAS_INVALID_PARAMETER );
292
+ }
251
293
}
252
294
}
253
- elseif (array_key_exists ('item_id ' , $ args ))
295
+ elseif (array_key_exists ('itemid ' , $ args ))
254
296
{
255
- $ item = $ itemModel ->load ($ args ['item_id ' ]);
297
+ if ($ args ['itemid ' ] != $ resourceid )
298
+ {
299
+ throw new Exception ('Upload token does not match itemid ' , MIDAS_INVALID_PARAMETER );
300
+ }
301
+ $ item = $ itemModel ->load ($ args ['itemid ' ]);
256
302
if ($ item == false )
257
303
{
258
304
throw new Exception ('Unable to find item ' , MIDAS_INVALID_PARAMETER );
@@ -262,10 +308,14 @@ function uploadPerform($args)
262
308
throw new Exception ('Permission error ' , MIDAS_INVALID_POLICY );
263
309
}
264
310
}
265
- elseif (array_key_exists ('folder_id ' , $ args ))
311
+ elseif (array_key_exists ('folderid ' , $ args ))
266
312
{
313
+ if ($ args ['folderid ' ] != $ resourceid )
314
+ {
315
+ throw new Exception ('Upload token does not match itemid ' , MIDAS_INVALID_PARAMETER );
316
+ }
267
317
$ folderModel = $ modelLoader ->loadModel ('Folder ' );
268
- $ folder = $ folderModel ->load ($ args ['folder_id ' ]);
318
+ $ folder = $ folderModel ->load ($ args ['folderid ' ]);
269
319
if ($ folder == false )
270
320
{
271
321
throw new Exception ('Unable to find folder ' , MIDAS_INVALID_PARAMETER );
@@ -277,18 +327,17 @@ function uploadPerform($args)
277
327
}
278
328
else
279
329
{
280
- throw new Exception ('Parameter itemrevision_id or item_id or folder_id is not defined ' , MIDAS_INVALID_PARAMETER );
330
+ throw new Exception ('You must specify an itemid or folderid to upload into ' , MIDAS_INVALID_PARAMETER );
281
331
}
282
332
283
333
$ mode = array_key_exists ('mode ' , $ args ) ? $ args ['mode ' ] : 'stream ' ;
284
334
$ uploadApi = new KwUploadAPI ($ this ->apiSetup );
285
335
336
+ // Use KWUploadApi to handle the actual file upload
286
337
if ($ mode == 'stream ' )
287
338
{
288
- $ token = $ this ->uploadApi ->generateToken ($ args );
289
- $ args ['uploadtoken ' ] = $ token ['token ' ];
290
- $ args ['length ' ] = $ args ['size ' ];
291
339
$ result = $ uploadApi ->process ($ args );
340
+
292
341
$ filename = $ result ['filename ' ];
293
342
$ filepath = $ result ['path ' ];
294
343
$ filesize = $ result ['size ' ];
@@ -327,6 +376,14 @@ function uploadPerform($args)
327
376
$ item = $ uploadComponent ->createNewRevision ($ userDao , $ filename , $ filepath , $ tmp , '' );
328
377
}
329
378
379
+ if (!$ item )
380
+ {
381
+ throw new Exception ('Upload failed ' , MIDAS_INTERNAL_ERROR );
382
+ }
383
+ if ($ filesize == $ args ['length ' ])
384
+ {
385
+ unlink ($ filepath );
386
+ }
330
387
return $ item ->toArray ();
331
388
}
332
389
0 commit comments