/**
  * Delete a folder.
  *
  * @param AssetFolderModel $folder The assetFolderModel representing the folder to be deleted.
  *
  * @return AssetOperationResponseModel
  */
 public function deleteFolder(AssetFolderModel $folder)
 {
     // Get rid of children files
     $criteria = craft()->elements->getCriteria(ElementType::Asset);
     $criteria->folderId = $folder->id;
     $files = $criteria->find();
     foreach ($files as $file) {
         $this->deleteFile($file);
     }
     // Delete children folders
     $childFolders = craft()->assets->findFolders(array('parentId' => $folder->id));
     foreach ($childFolders as $childFolder) {
         $this->deleteFolder($childFolder);
     }
     $parentFolder = craft()->assets->getFolderById($folder->parentId);
     $this->deleteSourceFolder($parentFolder, $folder->name);
     craft()->assets->deleteFolderRecord($folder->id);
     $response = new AssetOperationResponseModel();
     return $response->setSuccess();
 }
 /**
  * @inheritDoc BaseAssetSourceType::moveSourceFile()
  *
  * @param AssetFileModel   $file
  * @param AssetFolderModel $targetFolder
  * @param string           $fileName
  * @param bool             $overwrite
  *
  * @return mixed
  */
 protected function moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->_getPathPrefix() . $targetFolder->path . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $this->_prepareForRequests();
     $settings = $this->getSettings();
     $fileInfo = $this->_s3->getObjectInfo($settings->bucket, $newServerPath);
     $conflict = !$overwrite && ($fileInfo || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     $bucket = $this->getSettings()->bucket;
     // Just in case we're moving from another bucket with the same access credentials.
     $originatingSourceType = craft()->assetSources->getSourceTypeById($file->sourceId);
     $originatingSettings = $originatingSourceType->getSettings();
     $sourceBucket = $originatingSettings->bucket;
     $this->_prepareForRequests($originatingSettings);
     if (!$this->_s3->copyObject($sourceBucket, $this->_getPathPrefix($originatingSettings) . $file->getFolder()->path . $file->filename, $bucket, $newServerPath, \S3::ACL_PUBLIC_READ)) {
         $response = new AssetOperationResponseModel();
         return $response->setError(Craft::t("Could not save the file"));
     }
     @$this->_s3->deleteObject($sourceBucket, $this->_getS3Path($file, $originatingSettings));
     if ($file->kind == 'image') {
         if ($targetFolder->sourceId == $file->sourceId) {
             $transforms = craft()->assetTransforms->getAllCreatedTransformsForFile($file);
             $destination = clone $file;
             $destination->filename = $fileName;
             // Move transforms
             foreach ($transforms as $index) {
                 // For each file, we have to have both the source and destination
                 // for both files and transforms, so we can reliably move them
                 $destinationIndex = clone $index;
                 if (!empty($index->filename)) {
                     $destinationIndex->filename = $fileName;
                     craft()->assetTransforms->storeTransformIndexData($destinationIndex);
                 }
                 $from = $file->getFolder()->path . craft()->assetTransforms->getTransformSubpath($file, $index);
                 $to = $targetFolder->path . craft()->assetTransforms->getTransformSubpath($destination, $destinationIndex);
                 $this->copySourceFile($from, $to);
                 $this->deleteSourceFile($from);
             }
         } else {
             craft()->assetTransforms->deleteAllTransformData($file);
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }
 /**
  * @inheritDoc BaseAssetSourceType::moveSourceFile()
  *
  * @param AssetFileModel   $file
  * @param AssetFolderModel $targetFolder
  * @param string           $fileName
  * @param bool             $overwrite
  *
  * @return mixed
  */
 protected function moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->getSourceFileSystemPath() . $targetFolder->path . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $conflict = !$overwrite && (IOHelper::fileExists($newServerPath) || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     if (!IOHelper::move($this->_getFileSystemPath($file), $newServerPath)) {
         $response = new AssetOperationResponseModel();
         return $response->setError(Craft::t('Could not move the file “{filename}”.', array('filename' => $fileName)));
     }
     if ($file->kind == 'image') {
         if ($targetFolder->sourceId == $file->sourceId) {
             $transforms = craft()->assetTransforms->getAllCreatedTransformsForFile($file);
             $destination = clone $file;
             $destination->filename = $fileName;
             // Move transforms
             foreach ($transforms as $index) {
                 // For each file, we have to have both the source and destination
                 // for both files and transforms, so we can reliably move them
                 $destinationIndex = clone $index;
                 if (!empty($index->filename)) {
                     $destinationIndex->filename = $fileName;
                     craft()->assetTransforms->storeTransformIndexData($destinationIndex);
                 }
                 $from = $file->getFolder()->path . craft()->assetTransforms->getTransformSubpath($file, $index);
                 $to = $targetFolder->path . craft()->assetTransforms->getTransformSubpath($destination, $destinationIndex);
                 $this->copySourceFile($from, $to);
                 $this->deleteSourceFile($from);
             }
         } else {
             craft()->assetTransforms->deleteAllTransformData($file);
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }
Ejemplo n.º 4
0
 /**
  * Merge a conflicting uploaded file.
  *
  * @param string $conflictResolution  User response to conflict.
  * @param int    $theNewFileId        The id of the new file that is conflicting.
  * @param string $fileName            The filename that is in the conflict.
  *
  * @return AssetOperationResponseModel
  */
 private function _mergeUploadedFiles($conflictResolution, $theNewFileId, $fileName)
 {
     $theNewFile = $this->getFileById($theNewFileId);
     $folder = $theNewFile->getFolder();
     $source = craft()->assetSources->getSourceTypeById($folder->sourceId);
     $fileId = null;
     switch ($conflictResolution) {
         case AssetConflictResolution::Replace:
             // Replace the actual file
             $targetFile = $this->findFile(array('folderId' => $folder->id, 'filename' => $fileName));
             // If the file doesn't exist in the index, but just in the source,
             // quick-index it, so we have a File Model to work with.
             if (!$targetFile) {
                 $targetFile = new AssetFileModel();
                 $targetFile->sourceId = $folder->sourceId;
                 $targetFile->folderId = $folder->id;
                 $targetFile->filename = $fileName;
                 $targetFile->kind = IOHelper::getFileKind(IOHelper::getExtension($fileName));
                 $this->storeFile($targetFile);
             }
             $source->replaceFile($targetFile, $theNewFile);
             $fileId = $targetFile->id;
             // Falling through to delete the file
         // Falling through to delete the file
         case AssetConflictResolution::Cancel:
             $this->deleteFiles($theNewFileId);
             break;
         default:
             $fileId = $theNewFileId;
             break;
     }
     $response = new AssetOperationResponseModel();
     $response->setSuccess();
     if ($fileId) {
         $response->setDataItem('fileId', $fileId);
     }
     return $response;
 }
Ejemplo n.º 5
0
 /**
  * @inheritDoc BaseAssetSourceType::moveSourceFile()
  *
  * @param AssetFileModel   $file
  * @param AssetFolderModel $targetFolder
  * @param string           $fileName
  * @param bool             $overwrite
  *
  * @return mixed
  */
 protected function moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->_getPathPrefix() . $targetFolder->path . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $fileInfo = $this->_getObjectInfo($newServerPath);
     $conflict = !$overwrite && ($fileInfo || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     // Get the originating source object.
     $originatingSourceType = craft()->assetSources->getSourceTypeById($file->sourceId);
     $originatingSettings = $originatingSourceType->getSettings();
     $sourceUri = $this->_prepareRequestURI($originatingSettings->container, $this->_getPathPrefix($originatingSettings) . $file->getPath());
     $targetUri = $this->_prepareRequestURI($this->getSettings()->container, $newServerPath);
     $this->_copyFile($sourceUri, $targetUri);
     $this->_deleteObject($sourceUri);
     if ($file->kind == 'image') {
         if ($targetFolder->sourceId == $file->sourceId) {
             $transforms = craft()->assetTransforms->getAllCreatedTransformsForFile($file);
             $destination = clone $file;
             $destination->filename = $fileName;
             // Move transforms
             foreach ($transforms as $index) {
                 // For each file, we have to have both the source and destination
                 // for both files and transforms, so we can reliably move them
                 $destinationIndex = clone $index;
                 if (!empty($index->filename)) {
                     $destinationIndex->filename = $fileName;
                     craft()->assetTransforms->storeTransformIndexData($destinationIndex);
                 }
                 // Since Rackspace needs it's paths prepared, we deviate a little from the usual pattern.
                 $sourceTransformPath = $file->folderPath . craft()->assetTransforms->getTransformSubpath($file, $index);
                 $sourceTransformPath = $this->_prepareRequestURI($originatingSettings->container, $this->_getPathPrefix($originatingSettings) . $sourceTransformPath);
                 $targetTransformPath = $this->_getPathPrefix() . $targetFolder->path . craft()->assetTransforms->getTransformSubpath($destination, $destinationIndex);
                 $targetTransformPath = $this->_prepareRequestURI($this->getSettings()->container, $targetTransformPath);
                 $this->_copyFile($sourceTransformPath, $targetTransformPath);
                 $this->deleteSourceFile($file->folderPath . craft()->assetTransforms->getTransformSubpath($file, $index));
             }
         } else {
             craft()->assetTransforms->deleteAllTransformData($file);
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }
Ejemplo n.º 6
0
 /**
  * Move a file in source.
  *
  * @param AssetFileModel $file
  * @param AssetFolderModel $targetFolder
  * @param string $fileName
  * @param bool $overwrite if True, will overwrite target destination
  * @return mixed
  */
 protected function _moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->_getPathPrefix() . $targetFolder->fullPath . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $this->_prepareForRequests();
     $settings = $this->getSettings();
     $fileInfo = $this->_s3->getObjectInfo($settings->bucket, $newServerPath);
     $conflict = !$overwrite && ($fileInfo || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->_getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     $bucket = $this->getSettings()->bucket;
     // Just in case we're moving from another bucket with the same access credentials.
     $originatingSourceType = craft()->assetSources->getSourceTypeById($file->sourceId);
     $originatingSettings = $originatingSourceType->getSettings();
     $sourceBucket = $originatingSettings->bucket;
     $this->_prepareForRequests($originatingSettings);
     if (!$this->_s3->copyObject($sourceBucket, $this->_getPathPrefix($originatingSettings) . $file->getFolder()->fullPath . $file->filename, $bucket, $newServerPath, \S3::ACL_PUBLIC_READ)) {
         $response = new AssetOperationResponseModel();
         return $response->setError(Craft::t("Could not save the file"));
     }
     @$this->_s3->deleteObject($sourceBucket, $this->_getS3Path($file));
     if ($file->kind == 'image') {
         $this->_deleteGeneratedThumbnails($file);
         // Move transforms
         $transforms = craft()->assetTransforms->getGeneratedTransformLocationsForFile($file);
         $baseFromPath = $this->_getPathPrefix() . $file->getFolder()->fullPath;
         $baseToPath = $this->_getPathPrefix() . $targetFolder->fullPath;
         foreach ($transforms as $location) {
             // Surpress errors when trying to move image transforms. Maybe the user hasn't updated them yet.
             $copyResult = @$this->_s3->copyObject($sourceBucket, $baseFromPath . $location . '/' . $file->filename, $bucket, $baseToPath . $location . '/' . $fileName, \S3::ACL_PUBLIC_READ);
             // If we failed to copy, that's because source wasn't there. Skip delete and save time - everyone's a winner!
             if ($copyResult) {
                 @$this->_s3->deleteObject($sourceBucket, $baseFromPath . $location . '/' . $file->filename);
             }
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }
Ejemplo n.º 7
0
 /**
  * Merge a conflicting uploaded file.
  *
  * @param string $conflictResolution  User response to conflict.
  * @param int    $theNewFileId        The id of the new file that is conflicting.
  * @param string $fileName            The filename that is in the conflict.
  *
  * @return AssetOperationResponseModel
  */
 private function _mergeUploadedFiles($conflictResolution, $theNewFileId, $fileName)
 {
     $theNewFile = $this->getFileById($theNewFileId);
     $folder = $theNewFile->getFolder();
     $source = craft()->assetSources->getSourceTypeById($folder->sourceId);
     $fileId = null;
     switch ($conflictResolution) {
         case AssetConflictResolution::Replace:
             // Replace the actual file
             $targetFile = $this->findFile(array('folderId' => $folder->id, 'filename' => $fileName));
             $source->replaceFile($targetFile, $theNewFile);
             $fileId = $targetFile->id;
             // Falling through to delete the file
         // Falling through to delete the file
         case AssetConflictResolution::Cancel:
             $this->deleteFiles($theNewFileId);
             break;
         default:
             $fileId = $theNewFileId;
             break;
     }
     $response = new AssetOperationResponseModel();
     $response->setSuccess();
     if ($fileId) {
         $response->setDataItem('fileId', $fileId);
     }
     return $response;
 }
 /**
  * Move a file in source.
  *
  * @param AssetFileModel   $file         The file to move.
  * @param AssetFolderModel $targetFolder The folder where to move the file.
  * @param string           $fileName     The filename to use.
  * @param bool             $overwrite    If true, will overwrite target
  *
  * @return mixed
  */
 protected function moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->getSourceFileSystemPath() . $targetFolder->path . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $conflict = !$overwrite && (IOHelper::fileExists($newServerPath) || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     if (!IOHelper::move($this->_getFileSystemPath($file), $newServerPath)) {
         $response = new AssetOperationResponseModel();
         return $response->setError(Craft::t('Could not move the file “{filename}”.', array('filename' => $fileName)));
     }
     if ($file->kind == 'image') {
         if ($targetFolder->sourceId == $file->sourceId) {
             $transforms = craft()->assetTransforms->getAllCreatedTransformsForFile($file);
             // Move transforms
             foreach ($transforms as $index) {
                 $this->copyTransform($file, $targetFolder, $index, $index);
                 $this->deleteSourceFile($file->getFolder()->path . craft()->assetTransforms->getTransformSubpath($file, $index));
             }
         } else {
             craft()->assetTransforms->deleteCreatedTransformsForFile($file);
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }
Ejemplo n.º 9
0
 /**
  * Move or rename files.
  *
  * @param $fileIds
  * @param $folderId
  * @param string $filename if this is a rename operation
  * @param array $actions actions to take in case of a conflict.
  */
 public function moveFiles($fileIds, $folderId, $filename = '', $actions = array())
 {
     if (!is_array($fileIds)) {
         $fileIds = array($fileIds);
     }
     if (!is_array($actions)) {
         $actions = array($actions);
     }
     $results = array();
     $response = new AssetOperationResponseModel();
     foreach ($fileIds as $i => $fileId) {
         $file = $this->getFileById($fileId);
         // If this is not a rename operation, then the filename remains the original
         if (empty($filename)) {
             $filename = $file->filename;
         }
         $filename = IOHelper::cleanFilename($filename);
         if ($folderId == $file->folderId && $filename == $file->filename) {
             $response = new AssetOperationResponseModel();
             $response->setSuccess();
             $results[] = $response;
         }
         $originalSourceType = craft()->assetSources->getSourceTypeById($file->sourceId);
         $folder = $this->getFolderById($folderId);
         $newSourceType = craft()->assetSources->getSourceTypeById($folder->sourceId);
         if ($originalSourceType && $newSourceType) {
             if (!($response = $newSourceType->moveFileInsideSource($originalSourceType, $file, $folder, $filename, $actions[$i]))) {
                 $response = $this->_moveFileBetweenSources($originalSourceType, $newSourceType, $file, $folder, $actions[$i]);
             }
         } else {
             $response->setError(Craft::t("There was an error moving the file {file}.", array('file' => $file->filename)));
         }
     }
     return $response;
 }
 /**
  * Move a file in source.
  *
  * @param AssetFileModel $file
  * @param AssetFolderModel $targetFolder
  * @param string $fileName
  * @param bool $overwrite if True, will overwrite target destination
  * @return mixed
  */
 protected function _moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->_getPathPrefix() . $targetFolder->fullPath . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $fileInfo = $this->_getObjectInfo($newServerPath);
     $conflict = !$overwrite && ($fileInfo || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->_getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     $sourceFolder = $file->getFolder();
     // Get the originating source object.
     $originatingSourceType = craft()->assetSources->getSourceTypeById($file->sourceId);
     $originatingSettings = $originatingSourceType->getSettings();
     $sourceUri = $this->_prepareRequestURI($originatingSettings->container, $originatingSettings->subfolder . $sourceFolder->fullPath . $file);
     $targetUri = $this->_prepareRequestURI($this->getSettings()->container, $newServerPath);
     $this->_copyFile($sourceUri, $targetUri);
     $this->_deleteObject($sourceUri);
     if ($file->kind == 'image') {
         $this->_deleteGeneratedThumbnails($file);
         // Move transforms
         $transforms = craft()->assetTransforms->getGeneratedTransformLocationsForFile($file);
         $baseFromPath = $originatingSettings->subfolder . $sourceFolder->fullPath;
         $baseToPath = $this->_getPathPrefix() . $targetFolder->fullPath;
         foreach ($transforms as $location) {
             $sourceUri = $this->_prepareRequestURI($originatingSettings->container, $baseFromPath . $location . '/' . $file->filename);
             $targetUri = $this->_prepareRequestURI($this->getSettings()->container, $baseToPath . $location . '/' . $fileName);
             $this->_copyFile($sourceUri, $targetUri);
             $this->_deleteObject($sourceUri);
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }
 /**
  * Move a file in source.
  *
  * @param AssetFileModel $file
  * @param AssetFolderModel $targetFolder
  * @param string $fileName
  * @param bool $overwrite if True, will overwrite target destination
  * @return mixed
  */
 protected function _moveSourceFile(AssetFileModel $file, AssetFolderModel $targetFolder, $fileName = '', $overwrite = false)
 {
     if (empty($fileName)) {
         $fileName = $file->filename;
     }
     $newServerPath = $this->_getSourceFileSystemPath() . $targetFolder->fullPath . $fileName;
     $conflictingRecord = craft()->assets->findFile(array('folderId' => $targetFolder->id, 'filename' => $fileName));
     $conflict = !$overwrite && (IOHelper::fileExists($newServerPath) || !craft()->assets->isMergeInProgress() && is_object($conflictingRecord));
     if ($conflict) {
         $response = new AssetOperationResponseModel();
         return $response->setPrompt($this->_getUserPromptOptions($fileName))->setDataItem('fileName', $fileName);
     }
     if (!IOHelper::move($this->_getFileSystemPath($file), $newServerPath)) {
         $response = new AssetOperationResponseModel();
         return $response->setError(Craft::t("Could not save the file"));
     }
     if ($file->kind == 'image') {
         $this->_deleteGeneratedThumbnails($file);
         // Move transforms
         $transforms = craft()->assetTransforms->getGeneratedTransformLocationsForFile($file);
         $baseFromPath = $this->_getSourceFileSystemPath() . $file->getFolder()->fullPath;
         $baseToPath = $this->_getSourceFileSystemPath() . $targetFolder->fullPath;
         foreach ($transforms as $location) {
             if (IOHelper::fileExists($baseFromPath . $location . '/' . $file->filename)) {
                 IOHelper::ensureFolderExists($baseToPath . $location);
                 IOHelper::move($baseFromPath . $location . '/' . $file->filename, $baseToPath . $location . '/' . $fileName);
             }
         }
     }
     $response = new AssetOperationResponseModel();
     return $response->setSuccess()->setDataItem('newId', $file->id)->setDataItem('newFileName', $fileName);
 }