/** * Upload local bitstream. * * @param BitstreamDao $bitstreamdao * @param AssetstoreDao $assetstoredao * @param bool $copy * @throws Zend_Exception */ private function _uploadLocalBitstream($bitstreamdao, $assetstoredao, $copy = false) { // Check if the type of the assetstore is suitable if ($assetstoredao->getType() != MIDAS_ASSETSTORE_LOCAL) { throw new Zend_Exception('The assetstore type should be local to upload.'); } // Check if the path of the assetstore exists on the server if (!is_dir($assetstoredao->getPath())) { throw new Zend_Exception("The assetstore path doesn't exist."); } // Check if the MD5 exists for the bitstream $checksum = $bitstreamdao->getChecksum(); if (empty($checksum)) { throw new Zend_Exception('Checksum is not set.'); } // If we already have a file of this checksum in any assetstore, we point to it /** @var BitstreamModel $bitstreamModel */ $bitstreamModel = MidasLoader::loadModel('Bitstream'); $existing = $bitstreamModel->getByChecksum($checksum); if ($existing) { if ($copy === false) { unlink($bitstreamdao->getPath()); // Remove the temporary uploaded file } $bitstreamdao->setPath($existing->getPath()); $bitstreamdao->setAssetstoreId($existing->getAssetstoreId()); return; } // Two-level hierarchy. $path = substr($checksum, 0, 2) . '/' . substr($checksum, 2, 2) . '/' . $checksum; $fullpath = $assetstoredao->getPath() . '/' . $path; // Create the directories $currentdir = $assetstoredao->getPath() . '/' . substr($checksum, 0, 2); $this->_createAssetstoreDirectory($currentdir); $currentdir .= '/' . substr($checksum, 2, 2); $this->_createAssetstoreDirectory($currentdir); if ($copy) { copy($bitstreamdao->getPath(), $fullpath); } else { rename($bitstreamdao->getPath(), $fullpath); } // Set the new path $bitstreamdao->setPath($path); }
/** * Move all bitstreams from one asset store to another. * * @param AssetstoreDao $srcAssetstore The source asset store * @param AssetstoreDao $dstAssetstore The destination asset store * @param null|ProgressDao $progressDao Progress dao for asynchronous updating * @throws Zend_Exception */ public function moveBitstreams($srcAssetstore, $dstAssetstore, $progressDao = null) { $current = 0; /** @var ProgressModel $progressModel */ $progressModel = MidasLoader::loadModel('Progress'); /** @var BitstreamModel $bitstreamModel */ $bitstreamModel = MidasLoader::loadModel('Bitstream'); $sql = $this->database->select()->setIntegrityCheck(false)->from('bitstream')->where('assetstore_id = ?', $srcAssetstore->getKey()); $rows = $this->database->fetchAll($sql); $srcPath = $srcAssetstore->getPath(); $dstPath = $dstAssetstore->getPath(); foreach ($rows as $row) { $bitstream = $this->initDao('Bitstream', $row); if ($progressDao) { ++$current; $message = $current . ' / ' . $progressDao->getMaximum() . ': Moving ' . $bitstream->getName() . ' (' . UtilityComponent::formatSize($bitstream->getSizebytes()) . ')'; $progressModel->updateProgress($progressDao, $current, $message); } // Move the file on disk to its new location $dir1 = substr($bitstream->getChecksum(), 0, 2); $dir2 = substr($bitstream->getChecksum(), 2, 2); if (!is_dir($dstPath . '/' . $dir1)) { if (!mkdir($dstPath . '/' . $dir1)) { throw new Zend_Exception('Failed to mkdir ' . $dstPath . '/' . $dir1); } } if (!is_dir($dstPath . '/' . $dir1 . '/' . $dir2)) { if (!mkdir($dstPath . '/' . $dir1 . '/' . $dir2)) { throw new Zend_Exception('Failed to mkdir ' . $dstPath . '/' . $dir1 . '/' . $dir2); } } if (is_file($dstPath . '/' . $bitstream->getPath())) { if (is_file($srcPath . '/' . $bitstream->getPath())) { unlink($srcPath . '/' . $bitstream->getPath()); } } else { if (!rename($srcPath . '/' . $bitstream->getPath(), $dstPath . '/' . $bitstream->getPath())) { throw new Zend_Exception('Error moving ' . $bitstream->getPath()); } } // Update the asset store id on the bitstream record once it has been moved $bitstream->setAssetstoreId($dstAssetstore->getKey()); $bitstreamModel->save($bitstream); } }
/** * called from ajax. */ public function addAction() { $this->requireAdminPrivileges(); $this->disableLayout(); $this->disableView(); $form = $this->Form->Assetstore->createAssetstoreForm(); if ($this->getRequest()->isPost() && !$form->isValid($_POST)) { echo json_encode(array('error' => 'Missing or invalid form values.')); return false; } if ($this->getRequest()->isPost() && $form->isValid($_POST)) { // Save the assetstore in the database $assetstoreDao = new AssetstoreDao(); $assetstoreDao->setName($form->name->getValue()); $assetstoreDao->setPath($form->basedirectory->getValue()); $assetstoreDao->setType($form->assetstoretype->getValue()); if (!is_dir($assetstoreDao->getPath())) { echo JsonComponent::encode(array('error' => 'The path provided is not a valid directory')); return false; } if (!is_writable($assetstoreDao->getPath())) { echo JsonComponent::encode(array('error' => 'The specified directory is not writable')); return false; } try { $this->Assetstore->save($assetstoreDao); } catch (Zend_Exception $ze) { echo JsonComponent::encode(array('error' => $ze->getMessage())); return false; } $totalSpace = UtilityComponent::diskTotalSpace($assetstoreDao->getPath()); $freeSpace = UtilityComponent::diskFreeSpace($assetstoreDao->getPath()); echo JsonComponent::encode(array('msg' => 'The assetstore has been added.', 'assetstore_id' => $assetstoreDao->getAssetstoreId(), 'assetstore_name' => $assetstoreDao->getName(), 'assetstore_type' => $assetstoreDao->getType(), 'totalSpace' => $totalSpace, 'totalSpaceText' => $this->Component->Utility->formatSize($totalSpace), 'freeSpace' => $freeSpace, 'freeSpaceText' => $this->Component->Utility->formatSize($freeSpace))); return true; } echo json_encode(array('error' => 'Bad request.')); return false; }