/** * @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->_googleCloud->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->_googleCloud->copyObject($sourceBucket, $this->_getPathPrefix($originatingSettings) . $file->getFolder()->path . $file->filename, $bucket, $newServerPath, \GC::ACL_PUBLIC_READ)) { $response = new AssetOperationResponseModel(); return $response->setError(Craft::t("Could not save the file")); } @$this->_googleCloud->deleteObject($sourceBucket, $this->_getGCPath($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); }
/** * Return true if a transform exists at the location for a file. * * @param AssetFileModel $file * @param $location * @return mixed */ public function transformExists(AssetFileModel $file, $location) { $this->_prepareForRequests(); return (bool) @$this->_googleCloud->getObjectInfo($this->getSettings()->bucket, $this->_getPathPrefix() . $file->getFolder()->fullPath . $location . '/' . $file->filename); }