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

Commit de0deb3

Browse files
committed
ENH: refs #236. Don't allow duplicate bitstream names in a single revision
when uploading via the web API
1 parent fb3e9f3 commit de0deb3

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ function uploadGeneratetoken($args)
230230
$revision->setLicense(null);
231231
$revision = $itemModel->addRevision($item, $revision);
232232
}
233+
234+
$siblings = $revision->getBitstreams();
235+
foreach($siblings as $sibling)
236+
{
237+
if($sibling->getName() == $args['filename'])
238+
{
239+
// already have a file with this name. don't add new record.
240+
return array('token' => '');
241+
}
242+
}
233243
Zend_Loader::loadClass('BitstreamDao', BASE_PATH.'/core/models/dao');
234244
$bitstream = new BitstreamDao();
235245
$bitstream->setChecksum($args['checksum']);

modules/api/tests/controllers/ApiCallMethodsTest.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public function testUpload()
262262
$this->params['token'] = $this->_loginUsingApiKey();
263263
$this->params['method'] = 'midas.upload.generatetoken';
264264
$this->params['filename'] = 'test.txt';
265+
$this->params['checksum'] = 'foo';
265266
// call should fail for the first item since we don't have write permission
266267
$this->params['itemid'] = $itemsFile[0]->getKey();
267268
$this->request->setMethod('POST');
@@ -274,9 +275,25 @@ public function testUpload()
274275
$usersFile = $this->loadData('User', 'default');
275276
$itemsFile = $this->loadData('Item', 'default');
276277

278+
//now upload using our token
279+
$this->resetAll();
280+
$string = '';
281+
$length = 100;
282+
for($i = 0; $i < $length; $i++)
283+
{
284+
$string .= 'a';
285+
}
286+
$fh = fopen(BASE_PATH.'/tmp/misc/test.txt', 'w');
287+
fwrite($fh, $string);
288+
fclose($fh);
289+
$md5 = md5($string);
290+
$assetstoreFile = BASE_PATH.'/data/assetstore/'.substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.$md5;
291+
unlink($assetstoreFile);
292+
277293
$this->params['token'] = $this->_loginUsingApiKey();
278294
$this->params['method'] = 'midas.upload.generatetoken';
279295
$this->params['filename'] = 'test.txt';
296+
$this->params['checksum'] = $md5;
280297
// use the second item since it has write permission set for our user
281298
$this->params['itemid'] = $itemsFile[1]->getKey();
282299
$this->request->setMethod('POST');
@@ -290,22 +307,6 @@ public function testUpload()
290307
$this->assertTrue(file_exists(BASE_PATH.'/tmp/misc/'.$token),
291308
'Token placeholder file '.$token.' was not created in the temp dir');
292309

293-
//now upload using our token
294-
$this->resetAll();
295-
296-
$string = '';
297-
$length = 100;
298-
for($i = 0; $i < $length; $i++)
299-
{
300-
$string .= 'a';
301-
}
302-
$fh = fopen(BASE_PATH.'/tmp/misc/test.txt', 'w');
303-
fwrite($fh, $string);
304-
fclose($fh);
305-
$md5 = md5($string);
306-
$assetstoreFile = BASE_PATH.'/data/assetstore/'.substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.$md5;
307-
unlink($assetstoreFile);
308-
309310
$this->params['method'] = 'midas.upload.perform';
310311
$this->params['uploadtoken'] = $token;
311312
$this->params['filename'] = 'test.txt';

0 commit comments

Comments
 (0)