/** * @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); }
/** * Moves a folder. * * @param AssetFolderModel $folder The assetFolderModel representing the existing folder. * @param AssetFolderModel $newParentFolder The assetFolderModel representing the new parent folder. * @param bool $overwriteTarget If true, will overwrite folder, if needed. * * @return AssetOperationResponseModel */ public function moveFolder(AssetFolderModel $folder, AssetFolderModel $newParentFolder, $overwriteTarget = false) { $response = new AssetOperationResponseModel(); if ($folder->id == $newParentFolder->id) { return $response->setSuccess(); } $removeFromTree = ''; if ($this->folderExists($newParentFolder, $folder->name)) { if ($overwriteTarget) { $existingFolder = craft()->assets->findFolder(array('parentId' => $newParentFolder->id, 'name' => $folder->name)); if ($existingFolder) { $removeFromTree = $existingFolder->id; $this->deleteFolder($existingFolder); } else { $this->deleteSourceFolder($newParentFolder, $folder->name); } } else { return $response->setPrompt($this->getUserFolderPromptOptions($folder->name, $folder->id))->setDataItem('folderId', $folder->id); } } $response->setSuccess()->setDataItem('deleteList', array($folder->id))->setDataItem('removeFromTree', $removeFromTree); $mirroringData = array('changedFolderIds' => array()); $this->_mirrorStructure($newParentFolder, $folder, $mirroringData); $response->setDataItem('changedFolderIds', $mirroringData['changedFolderIds']); $criteria = craft()->elements->getCriteria(ElementType::Asset); $criteria->folderId = array_keys(craft()->assets->getAllDescendantFolders($folder)); $files = $criteria->find(); $transferList = array(); foreach ($files as $file) { $transferList[] = array('fileId' => $file->id, 'folderId' => $mirroringData['changedFolderIds'][$file->folderId]['newId'], 'fileName' => $file->filename); } return $response->setDataItem('transferList', $transferList); }
/** * 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); }
/** * @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); }
/** * 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); }
/** * 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); }