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

uploadGeneratetoken: Add support to optionally create new revision #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions core/controllers/components/ApisystemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ public function linkCreate($args)
* When passing the <b>folderid</b> param, the name of the newly created item,
* if not supplied, the item will have the same name as <b>filename</b>.
* @param checksum (Optional) The md5 checksum of the file to be uploaded.
* @param create_additional_revision (Optional) When a <b>checksum</b> is passed and
* the server already has the file, by default a reference to the existing
* bitstream will be added to the latest revision. By setting
* <b>create_additional_revision</b> to true, a new revision will be created.
* @return An upload token that can be used to upload a file.
* If <b>folderid</b> is passed instead of <b>itemid</b>, a new item will be created
* in that folder, but the id of the newly created item will not be
Expand Down Expand Up @@ -404,13 +408,25 @@ public function uploadGeneratetoken($args)
MIDAS_POLICY_READ
)
) {
$createAdditionalRevision = isset($args['create_additional_revision']) ? $args['create_additional_revision'] : false;
$revision = $itemModel->getLastRevision($item);

if ($revision === false) {
// Create new revision if none exists yet
// Do not create an additional revision if last revision has one bitstream and checksum matches.
if ($createAdditionalRevision && $revision) {
$bitstreams = $revision->getBitstreams();
if (count($bitstreams) === 1 && $args['checksum'] == $bitstreams[0]->getChecksum()) {
return array('token' => '');
}
}

if ($revision === false || $createAdditionalRevision) {
// Create a new revision if none exists yet or if the user explicitly asks to create a new revision when
// a bitstream with the same checksum was found.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a new revision if none exists yet or if the user explicitly asks to create a new revision when a bitstream with the same checksum was found.

Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH.'/core/models/dao');
$revision = new ItemRevisionDao();
$revision->setChanges('Initial revision');
if ($createAdditionalRevision === false) {
$revision->setChanges('Initial revision');
}
$revision->setUser_id($userDao->getKey());
$revision->setDate(date('Y-m-d H:i:s'));
$revision->setLicenseId(null);
Expand Down
4 changes: 4 additions & 0 deletions modules/api/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ public function linkCreate($args)
* When passing the <b>folderid</b> param, the name of the newly created item,
* if not supplied, the item will have the same name as <b>filename</b>.
* @param checksum (Optional) The md5 checksum of the file to be uploaded.
* @param create_additional_revision (Optional) When a <b>checksum</b> is passed and
* the server already has the file, by default a reference to the existing
* bitstream will be added to the latest revision. By setting
* <b>create_additional_revision</b> to true, a new revision will be created.
* @return An upload token that can be used to upload a file.
* If <b>folderid</b> is passed instead of <b>itemid</b>, a new item will be created
* in that folder, but the id of the newly created item will not be
Expand Down