Beispiel #1
0
 /**
  * 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);
 }