Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 19668e5

Browse files
committed
ENH: refs #236. Implement bitstream upload via the web api
1 parent 9456850 commit 19668e5

File tree

2 files changed

+151
-82
lines changed

2 files changed

+151
-82
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,29 @@ function resourceSearch($args)
189189

190190
/**
191191
* 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
192194
* @return An upload token that can be used to upload a file
193195
*/
194196
function uploadGeneratetoken($args)
195197
{
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+
196213
$uploadApi = new KwUploadAPI($this->apiSetup);
197-
return $uploadApi->generateToken($args);
214+
return $uploadApi->generateToken($args, $userDao->getKey().'/'.$item->getKey());
198215
}
199216

200217
/**
@@ -209,50 +226,79 @@ function uploadGetoffset($args)
209226
}
210227

211228
/**
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
214233
* @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.
218237
* @param return The item information of the item created or changed
219238
*/
220239
function uploadPerform($args)
221240
{
241+
$this->_validateParams($args, array('uploadtoken', 'filename', 'length'));
222242
if(!$this->controller->getRequest()->isPost() && !$this->controller->getRequest()->isPut())
223243
{
224244
throw new Exception('POST or PUT method required', MIDAS_HTTP_ERROR);
225245
}
226246

227-
$userDao = $this->_getUser($args);
247+
list($userid, $resourceid, ) = explode('/', $args['uploadtoken']);
248+
//TODO check if this upload token is valid
228249

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)
230255
{
231-
throw new Exception('Please log in', MIDAS_INVALID_POLICY);
256+
throw new Exception('Invalid user id from upload token', MIDAS_INVALID_PARAMETER);
232257
}
233258

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))
237260
{
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']);
239266
if($item == false)
240267
{
241268
throw new Exception('Unable to find item', MIDAS_INVALID_PARAMETER);
242269
}
243-
if(!$itemModel->policyCheck($item, $userDao, MIDAS_POLICY_WRITE))
270+
if(strtolower($args['revision']) == 'head')
244271
{
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+
}
246285
}
247-
$revision = $itemModel->getRevision($item, $args['revision']);
248-
if($revision == false)
286+
else
249287
{
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+
}
251293
}
252294
}
253-
elseif(array_key_exists('item_id', $args))
295+
elseif(array_key_exists('itemid', $args))
254296
{
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']);
256302
if($item == false)
257303
{
258304
throw new Exception('Unable to find item', MIDAS_INVALID_PARAMETER);
@@ -262,10 +308,14 @@ function uploadPerform($args)
262308
throw new Exception('Permission error', MIDAS_INVALID_POLICY);
263309
}
264310
}
265-
elseif(array_key_exists('folder_id', $args))
311+
elseif(array_key_exists('folderid', $args))
266312
{
313+
if($args['folderid'] != $resourceid)
314+
{
315+
throw new Exception('Upload token does not match itemid', MIDAS_INVALID_PARAMETER);
316+
}
267317
$folderModel = $modelLoader->loadModel('Folder');
268-
$folder = $folderModel->load($args['folder_id']);
318+
$folder = $folderModel->load($args['folderid']);
269319
if($folder == false)
270320
{
271321
throw new Exception('Unable to find folder', MIDAS_INVALID_PARAMETER);
@@ -277,18 +327,17 @@ function uploadPerform($args)
277327
}
278328
else
279329
{
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);
281331
}
282332

283333
$mode = array_key_exists('mode', $args) ? $args['mode'] : 'stream';
284334
$uploadApi = new KwUploadAPI($this->apiSetup);
285335

336+
// Use KWUploadApi to handle the actual file upload
286337
if($mode == 'stream')
287338
{
288-
$token = $this->uploadApi->generateToken($args);
289-
$args['uploadtoken'] = $token['token'];
290-
$args['length'] = $args['size'];
291339
$result = $uploadApi->process($args);
340+
292341
$filename = $result['filename'];
293342
$filepath = $result['path'];
294343
$filesize = $result['size'];
@@ -327,6 +376,14 @@ function uploadPerform($args)
327376
$item = $uploadComponent->createNewRevision($userDao, $filename, $filepath, $tmp, '');
328377
}
329378

379+
if(!$item)
380+
{
381+
throw new Exception('Upload failed', MIDAS_INTERNAL_ERROR);
382+
}
383+
if($filesize == $args['length'])
384+
{
385+
unlink($filepath);
386+
}
330387
return $item->toArray();
331388
}
332389

0 commit comments

Comments
 (0)