/** * Generate a unique upload token. Either <b>itemid</b> or <b>folderid</b> is required, * but both are not allowed. * * @path /system/uploadtoken * @http GET * @param useSession (Optional) Authenticate using the current Midas session * @param token (Optional) Authentication token * @param itemid (Optional) * The id of the item to upload into. * @param folderid (Optional) * The id of the folder to create a new item in and then upload to. * The new item will have the same name as <b>filename</b> unless <b>itemname</b> * is supplied. * @param filename The filename of the file you will upload, will be used as the * bitstream's name and the item's name (unless <b>itemname</b> is supplied). * @param itemdescription (Optional) * When passing the <b>folderid</b> param, the description of the item, * if not supplied the item's description will be blank. * @param itemname (Optional) * 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. * @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 * returned. If the id of the newly created item is needed, * then call the <b>/item (POST)</b> api instead. * If <b>checksum</b> is passed and the token returned is blank, the * server already has this file and there is no need to follow this * call with a call to <b>/system/upload</b>, as the passed in * file will have been added as a bitstream to the item's latest * revision, creating a new revision if one doesn't exist. * * @param array $args parameters * @throws Exception */ public function uploadGeneratetoken($args) { /** @var ApihelperComponent $apihelperComponent */ $apihelperComponent = MidasLoader::loadComponent('Apihelper'); $apihelperComponent->validateParams($args, array('filename')); if (!array_key_exists('itemid', $args) && !array_key_exists('folderid', $args)) { throw new Exception('Parameter itemid or folderid must be defined', MIDAS_INVALID_PARAMETER); } if (array_key_exists('itemid', $args) && array_key_exists('folderid', $args)) { throw new Exception('Parameter itemid or folderid must be defined, but not both', MIDAS_INVALID_PARAMETER); } $apihelperComponent->requirePolicyScopes(array(MIDAS_API_PERMISSION_SCOPE_WRITE_DATA)); $userDao = $apihelperComponent->getUser($args); if (!$userDao) { throw new Exception('Anonymous users may not upload', MIDAS_INVALID_POLICY); } /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); if (array_key_exists('itemid', $args)) { $item = $itemModel->load($args['itemid']); if (!$itemModel->policyCheck($item, $userDao, MIDAS_POLICY_WRITE)) { throw new Exception('Invalid policy or itemid', MIDAS_INVALID_POLICY); } } elseif (array_key_exists('folderid', $args)) { /** @var FolderModel $folderModel */ $folderModel = MidasLoader::loadModel('Folder'); $folder = $folderModel->load($args['folderid']); if ($folder == false) { throw new Exception('Parent folder corresponding to folderid doesn\'t exist', MIDAS_INVALID_PARAMETER); } if (!$folderModel->policyCheck($folder, $userDao, MIDAS_POLICY_WRITE)) { throw new Exception('Invalid policy or folderid', MIDAS_INVALID_POLICY); } // create a new item in this folder $itemname = isset($args['itemname']) ? $args['itemname'] : $args['filename']; $description = isset($args['itemdescription']) ? $args['itemdescription'] : ''; $item = $itemModel->createItem($itemname, $description, $folder); if ($item === false) { throw new Exception('Create new item failed', MIDAS_INTERNAL_ERROR); } $itemModel->copyParentPolicies($item, $folder); /** @var ItempolicyuserModel $itempolicyuserModel */ $itempolicyuserModel = MidasLoader::loadModel('Itempolicyuser'); $itempolicyuserModel->createPolicy($userDao, $item, MIDAS_POLICY_ADMIN); } if (array_key_exists('checksum', $args)) { // If we already have a bitstream with this checksum, create a reference and return blank token /** @var BitstreamModel $bitstreamModel */ $bitstreamModel = MidasLoader::loadModel('Bitstream'); $existingBitstream = $bitstreamModel->getByChecksum($args['checksum']); if ($existingBitstream) { // User must have read access to the existing bitstream if they are circumventing the upload. // Otherwise an attacker could spoof the checksum and read a private bitstream with a known checksum. if ($itemModel->policyCheck($existingBitstream->getItemrevision()->getItem(), $userDao, MIDAS_POLICY_READ)) { $revision = $itemModel->getLastRevision($item); if ($revision === false) { // Create new revision if none exists yet Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH . '/core/models/dao'); $revision = new ItemRevisionDao(); $revision->setChanges('Initial revision'); $revision->setUser_id($userDao->getKey()); $revision->setDate(date('Y-m-d H:i:s')); $revision->setLicenseId(null); $itemModel->addRevision($item, $revision); } $siblings = $revision->getBitstreams(); foreach ($siblings as $sibling) { if ($sibling->getName() == $args['filename']) { // already have a file with this name. don't add new record. return array('token' => ''); } } Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao'); $bitstream = new BitstreamDao(); $bitstream->setChecksum($args['checksum']); $bitstream->setName($args['filename']); $bitstream->setSizebytes($existingBitstream->getSizebytes()); $bitstream->setPath($existingBitstream->getPath()); $bitstream->setAssetstoreId($existingBitstream->getAssetstoreId()); $bitstream->setMimetype($existingBitstream->getMimetype()); /** @var ItemRevisionModel $revisionModel */ $revisionModel = MidasLoader::loadModel('ItemRevision'); $revisionModel->addBitstream($revision, $bitstream); return array('token' => ''); } } } // we don't already have this content, so create the token /** @var HttpuploadComponent $uploadComponent */ $uploadComponent = MidasLoader::loadComponent('Httpupload'); $apiSetup = $apihelperComponent->getApiSetup(); $uploadComponent->setTestingMode(Zend_Registry::get('configGlobal')->environment === 'testing'); return $uploadComponent->generateToken($args, $userDao->getKey() . '/' . $item->getKey()); }
/** * Duplicate an item in destination folder/community. * * Create a new item (same as old one) in destination folder/community. The new item * have the same metadata and revisions with the old one, but its owner is set as the * input userDao parameter (who run this operation) and access policy is based on * the input folderDao parameter (destination folder) * * @param ItemDao $itemDao the item to be duplicated * @param UserDao $userDao the user who run this operation * @param FolderDao $folderDao destination folder * @return ItemDao * @throws Zend_Exception on invalid input parameters (itemDao, userDao and folderDao) */ public function duplicateItem($itemDao, $userDao, $folderDao) { if (!$itemDao instanceof ItemDao || !$folderDao instanceof FolderDao) { throw new Zend_Exception('Error in ItemDao or FolderDao when duplicating item'); } if (!$userDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } /** @var BitstreamModel $BitstreamModel */ $BitstreamModel = MidasLoader::loadModel('Bitstream'); $name = $itemDao->getName(); $description = $itemDao->getDescription(); $newItem = $this->createItem($name, $description, $folderDao); $newItem->setType($itemDao->getType()); $newItem->setSizebytes($itemDao->getSizebytes()); $newItem->setDateCreation(date('Y-m-d H:i:s')); $newItem->setDateUpdate(date('Y-m-d H:i:s')); $thumbnailId = $itemDao->getThumbnailId(); if ($thumbnailId !== null) { $oldThumb = $BitstreamModel->load($thumbnailId); $newThumb = new BitstreamDao(); $newThumb->setItemrevisionId(-1); $newThumb->setName($oldThumb->getName()); $newThumb->setMimetype($oldThumb->getMimetype()); $newThumb->setSizebytes($oldThumb->getSizebytes()); $newThumb->setChecksum($oldThumb->getChecksum()); $newThumb->setPath($oldThumb->getPath()); $newThumb->setAssetstoreId($oldThumb->getAssetstoreId()); $newThumb->setDate($oldThumb->getDate()); $BitstreamModel->save($newThumb); $newItem->setThumbnailId($newThumb->getKey()); } /** @var ItemRevisionModel $ItemRevisionModel */ $ItemRevisionModel = MidasLoader::loadModel('ItemRevision'); /** @var BitstreamModel $BitstreamModel */ $BitstreamModel = MidasLoader::loadModel('Bitstream'); /** @var MetadataModel $MetadataModel */ $MetadataModel = MidasLoader::loadModel('Metadata'); /** @var ItempolicygroupModel $ItemPolicyGroupModel */ $ItemPolicyGroupModel = MidasLoader::loadModel('Itempolicygroup'); $ItemPolicyGroupModel->computePolicyStatus($newItem); foreach ($itemDao->getRevisions() as $revision) { $dupItemRevision = new ItemRevisionDao(); $dupItemRevision->setItemId($newItem->getItemId()); $dupItemRevision->setRevision($revision->getRevision()); $dupItemRevision->setDate($revision->getDate()); $dupItemRevision->setChanges($revision->getChanges()); $dupItemRevision->setUserId($userDao->getUserId()); $dupItemRevision->setLicenseId($revision->getLicenseId()); $ItemRevisionModel->save($dupItemRevision); // duplicate metadata value $metadatavalues = $ItemRevisionModel->getMetadata($revision); foreach ($metadatavalues as $metadata) { $MetadataModel->addMetadataValue($dupItemRevision, $metadata->getMetadatatype(), $metadata->getElement(), $metadata->getQualifier(), $metadata->getValue(), false); } // duplicate bitstream foreach ($revision->getBitstreams() as $bitstream) { $dupBitstream = new BitstreamDao(); $dupBitstream->setItemrevisionId($dupItemRevision->getItemrevisionId()); $dupBitstream->setName($bitstream->getName()); $dupBitstream->setMimetype($bitstream->getMimetype()); $dupBitstream->setSizebytes($bitstream->getSizebytes()); $dupBitstream->setChecksum($bitstream->getChecksum()); $dupBitstream->setPath($bitstream->getPath()); $dupBitstream->setAssetstoreId($bitstream->getAssetstoreId()); $dupBitstream->setDate($bitstream->getDate()); $BitstreamModel->save($dupBitstream); } } $this->save($newItem, true); // call save with metadata changed flag return $newItem; }
/** * Save upload item in the database. * * @param UserDao $userDao * @param string $name * @param string $url * @param null|int $parent * @param int $sizebytes * @param string $checksum * @return ItemDao * @throws Zend_Exception */ public function createLinkItem($userDao, $name, $url, $parent = null, $sizebytes = 0, $checksum = ' ') { /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); /** @var FolderModel $folderModel */ $folderModel = MidasLoader::loadModel('Folder'); /** @var AssetstoreModel $assetstoreModel */ $assetstoreModel = MidasLoader::loadModel('Assetstore'); /** @var ItemRevisionModel $itemRevisionModel */ $itemRevisionModel = MidasLoader::loadModel('ItemRevision'); if ($userDao == null) { throw new Zend_Exception('Please log in'); } if (is_numeric($parent)) { $parent = $folderModel->load($parent); } if ($parent == false || !$folderModel->policyCheck($parent, $userDao, MIDAS_POLICY_WRITE)) { throw new Zend_Exception('Parent permissions errors'); } Zend_Loader::loadClass('ItemDao', BASE_PATH . '/core/models/dao'); $item = new ItemDao(); $item->setName($name); $item->setDescription(''); $item->setSizebytes($sizebytes); $item->setType(0); $item->setPrivacyStatus(MIDAS_PRIVACY_PRIVATE); // Must set this flag private initially $itemModel->save($item, false); $folderModel->addItem($parent, $item); $itemModel->copyParentPolicies($item, $parent); Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH . '/core/models/dao'); $itemRevisionDao = new ItemRevisionDao(); $itemRevisionDao->setChanges('Initial revision'); $itemRevisionDao->setUser_id($userDao->getKey()); $itemRevisionDao->setDate(date('Y-m-d H:i:s')); $itemRevisionDao->setLicenseId(null); $itemModel->addRevision($item, $itemRevisionDao); // Add bitstreams to the revision Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao'); $bitstreamDao = new BitstreamDao(); $bitstreamDao->setName($url); $bitstreamDao->setPath($url); $bitstreamDao->setMimetype('url'); $bitstreamDao->setSizebytes($sizebytes); $bitstreamDao->setChecksum($checksum); $assetstoreDao = $assetstoreModel->getDefault(); $bitstreamDao->setAssetstoreId($assetstoreDao->getKey()); $itemRevisionModel->addBitstream($itemRevisionDao, $bitstreamDao); $this->getLogger()->debug('Link item created (' . $item->getName() . ', id=' . $item->getKey() . ')'); return $item; }
/** convert to threejs */ public function convertToThreejs($revision) { /** @var SettingModel $settingModel */ $settingModel = MidasLoader::loadModel('Setting'); $useWebGL = $settingModel->getValueByName(VISUALIZE_USE_WEB_GL_KEY, $this->moduleName); if (!isset($useWebGL) || !$useWebGL) { return false; } /** @var ItemRevisionModel $itemRevisionModel */ $itemRevisionModel = MidasLoader::loadModel('ItemRevision'); /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); if (is_array($revision)) { $revision = $itemRevisionModel->load($revision['itemrevision_id']); } $bitstreams = $revision->getBitstreams(); $item = $revision->getItem(); $parents = $item->getFolders(); $userDao = $revision->getUser(); if (count($parents) == 0) { return; } $parent = $parents[0]; if (count($bitstreams) != 1) { return; } $bitstream = $bitstreams[0]; $filenameArray = explode('.', $bitstream->getName()); $ext = end($filenameArray); if ($ext != 'obj') { return; } if (file_exists(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js')) { unlink(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js'); } exec('python ' . dirname(__FILE__) . '/scripts/convert_obj_three.py -i ' . $bitstream->GetFullPath() . ' -o ' . UtilityComponent::getTempDirectory() . '/tmpThreeJs.js -t binary', $output); if (file_exists(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js') && file_exists(UtilityComponent::getTempDirectory() . '/tmpThreeJs.bin')) { /** @var AssetstoreModel $assetstoreModel */ $assetstoreModel = MidasLoader::loadModel('Assetstore'); $assetstoreDao = $assetstoreModel->getDefault(); /** @var UploadComponent $uploadComponent */ $uploadComponent = MidasLoader::loadComponent('Upload'); $newItem = $uploadComponent->createUploadedItem($userDao, $item->getName() . '.threejs.bin', UtilityComponent::getTempDirectory() . '/tmpThreeJs.bin', $parent); $itemModel->copyParentPolicies($newItem, $parent); $newRevision = $itemModel->getLastRevision($newItem); if ($newRevision === false) { throw new Zend_Exception('The item has no revisions', MIDAS_INVALID_POLICY); } $bitstreams = $newRevision->getBitstreams(); if (count($bitstreams) === 0) { throw new Zend_Exception('The item has no bitstreams', MIDAS_INVALID_POLICY); } $bitstreamDao = $bitstreams[0]; Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao'); $content = file_get_contents(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js'); $fc = Zend_Controller_Front::getInstance(); $content = str_replace('tmpThreeJs.bin', $fc->getBaseUrl() . '/download/?bitstream=' . $bitstreamDao->getKey(), $content); file_put_contents(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js', $content); $bitstreamDao = new BitstreamDao(); $bitstreamDao->setName($item->getName() . '.threejs.js'); $bitstreamDao->setPath(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js'); $bitstreamDao->setChecksum(''); $bitstreamDao->fillPropertiesFromPath(); $bitstreamDao->setAssetstoreId($assetstoreDao->getKey()); // Upload the bitstream if necessary (based on the assetstore type) $uploadComponent->uploadBitstream($bitstreamDao, $assetstoreDao); $itemRevisionModel->addBitstream($newRevision, $bitstreamDao); } }