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

Commit fb3e9f3

Browse files
committed
ENH: refs #236. Forbid duplicate content uploads via web API
midas.upload.generatetoken now requires a checksum parameter. If we already have a bitstream with the provided checksum, we don't create an upload token; instead we create a pointer to the existing content within the specified item and return an empty token to let the user know that they don't need to call the upload.process function.
1 parent 7430b69 commit fb3e9f3

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

core/models/base/ItemRevisionModelBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ function addBitstream($itemRevisionDao, $bitstreamDao)
6060
$item->setSizebytes($this->getSize($itemRevisionDao));
6161
$item->setDateCreation(date('c'));
6262

63-
$modulesThumbnail = Zend_Registry::get('notifier')->notifyEvent("EVENT_CORE_CREATE_THUMBNAIL", array($item));
63+
$modulesThumbnail = Zend_Registry::get('notifier')->notifyEvent('EVENT_CORE_CREATE_THUMBNAIL', array($item));
6464
$notifications = Zend_Registry::get('notifier')->getNotifications();
6565

6666
$createThumb = false;
67-
if(!isset($notifications["EVENT_CORE_CREATE_THUMBNAIL"]) || empty($notifications["EVENT_CORE_CREATE_THUMBNAIL"]))
67+
if(!isset($notifications['EVENT_CORE_CREATE_THUMBNAIL']) || empty($notifications['EVENT_CORE_CREATE_THUMBNAIL']))
6868
{
6969
$mime = $bitstreamDao->getMimetype();
7070
$tmpfile = $bitstreamDao->getPath();

modules/api/controllers/components/ApiComponent.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,12 @@ function resourceSearch($args)
192192
* @param token Authentication token
193193
* @param itemid The id of the parent item to upload into
194194
* @param filename The filename of the bitstream you will upload
195-
* @return An upload token that can be used to upload a file
195+
* @param checksum The md5 checksum of the file to be uploaded
196+
* @return An upload token that can be used to upload a file. If the token returned is blank, the server already has this file and there is no need to upload.
196197
*/
197198
function uploadGeneratetoken($args)
198199
{
199-
$this->_validateParams($args, array('itemid', 'filename'));
200+
$this->_validateParams($args, array('itemid', 'filename', 'checksum'));
200201
$userDao = $this->_getUser($args);
201202
if(!$userDao)
202203
{
@@ -211,8 +212,41 @@ function uploadGeneratetoken($args)
211212
throw new Exception('Invalid policy or itemid', MIDAS_INVALID_POLICY);
212213
}
213214

214-
$uploadApi = new KwUploadAPI($this->apiSetup);
215-
return $uploadApi->generateToken($args, $userDao->getKey().'/'.$item->getKey());
215+
// If we already have a bitstream with this checksum, create a reference and return blank token
216+
$bitstreamModel = $modelLoader->loadModel('Bitstream');
217+
$existingBitstream = $bitstreamModel->getByChecksum($args['checksum']);
218+
if($existingBitstream)
219+
{
220+
$revision = $itemModel->getLastRevision($item);
221+
222+
if($revision == false)
223+
{
224+
// Create new revision if none exists yet
225+
Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH.'/core/models/dao');
226+
$revision = new ItemRevisionDao();
227+
$revision->setChanges('Initial revision');
228+
$revision->setUser_id($userDao->getKey());
229+
$revision->setDate(date('c'));
230+
$revision->setLicense(null);
231+
$revision = $itemModel->addRevision($item, $revision);
232+
}
233+
Zend_Loader::loadClass('BitstreamDao', BASE_PATH.'/core/models/dao');
234+
$bitstream = new BitstreamDao();
235+
$bitstream->setChecksum($args['checksum']);
236+
$bitstream->setName($args['filename']);
237+
$bitstream->setSizebytes($existingBitstream->getSizebytes());
238+
$bitstream->setPath($existingBitstream->getPath());
239+
$bitstream->setAssetstoreId($existingBitstream->getAssetstoreId());
240+
$bitstream->setMimetype($existingBitstream->getMimetype());
241+
$revisionModel = $modelLoader->loadModel('Revision');
242+
$revisionModel->addBitstream($revision, $bitstream);
243+
return array('token' => '');
244+
}
245+
else //we don't already have this content, so create the token
246+
{
247+
$uploadApi = new KwUploadAPI($this->apiSetup);
248+
return $uploadApi->generateToken($args, $userDao->getKey().'/'.$item->getKey());
249+
}
216250
}
217251

218252
/**

0 commit comments

Comments
 (0)