/** * @inheritDoc BaseAssetSourceType::insertFileInFolder() * * @param AssetFolderModel $folder * @param $filePath * @param $fileName * * @throws Exception * @return AssetFileModel */ protected function insertFileInFolder(AssetFolderModel $folder, $filePath, $fileName) { $fileName = AssetsHelper::cleanAssetName($fileName); $extension = IOHelper::getExtension($fileName); if (!IOHelper::isExtensionAllowed($extension)) { throw new Exception(Craft::t('This file type is not allowed')); } $uriPath = $this->_getPathPrefix() . $folder->path . $fileName; $this->_prepareForRequests(); $settings = $this->getSettings(); $fileInfo = $this->_s3->getObjectInfo($settings->bucket, $uriPath); if ($fileInfo) { $response = new AssetOperationResponseModel(); return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName); } clearstatcache(); $this->_prepareForRequests(); if (!$this->putObject($filePath, $this->getSettings()->bucket, $uriPath, \S3::ACL_PUBLIC_READ)) { throw new Exception(Craft::t('Could not copy file to target destination')); } $response = new AssetOperationResponseModel(); return $response->setSuccess()->setDataItem('filePath', $uriPath); }
/** * Indexes a file. * * @param string $uriPath The URI path fo the file to index. * * @return AssetFileModel|bool|null */ protected function indexFile($uriPath) { $extension = IOHelper::getExtension($uriPath); if (IOHelper::isExtensionAllowed($extension)) { $parts = explode('/', $uriPath); $fileName = array_pop($parts); $searchFullPath = join('/', $parts) . (empty($parts) ? '' : '/'); if (empty($searchFullPath)) { $parentId = ':empty:'; } else { $parentId = false; } $parentFolder = craft()->assets->findFolder(array('sourceId' => $this->model->id, 'path' => $searchFullPath, 'parentId' => $parentId)); if (empty($parentFolder)) { return false; } $folderId = $parentFolder->id; $fileModel = craft()->assets->findFile(array('folderId' => $folderId, 'filename' => $fileName)); if (is_null($fileModel)) { $fileModel = new AssetFileModel(); $fileModel->sourceId = $this->model->id; $fileModel->folderId = $folderId; $fileModel->filename = $fileName; $fileModel->kind = IOHelper::getFileKind($extension); craft()->assets->storeFile($fileModel); } return $fileModel; } return false; }
/** * Move or rename files. * * @param $fileIds * @param $folderId * @param string $filename If this is a rename operation or not. * @param array $actions Actions to take in case of a conflict. * * @throws Exception * @return bool|AssetOperationResponseModel */ public function moveFiles($fileIds, $folderId, $filename = '', $actions = array()) { if ($filename && is_array($fileIds) && count($fileIds) > 1) { throw new Exception(Craft::t("It’s not possible to rename multiple files!")); } if (!is_array($fileIds)) { $fileIds = array($fileIds); } if (!is_array($actions)) { $actions = array($actions); } $results = array(); $response = new AssetOperationResponseModel(); // Make sure the filename is allowed if ($filename) { $extension = IOHelper::getExtension($filename); if (!IOHelper::isExtensionAllowed($extension)) { $response->setError(Craft::t('This file type is not allowed')); return $response; } } $folder = $this->getFolderById($folderId); $newSourceType = craft()->assetSources->getSourceTypeById($folder->sourceId); // Does the source folder exist? $parent = $folder->getParent(); if ($parent && $folder->parentId && !$newSourceType->folderExists($parent ? $parent->path : '', $folder->name)) { $response->setError(Craft::t("The target folder does not exist!")); } else { foreach ($fileIds as $i => $fileId) { $file = $this->getFileById($fileId); // If this is not a rename operation, then the filename remains the original if (count($fileIds) > 1 || empty($filename)) { $filename = $file->filename; } // If the new file does not have an extension, give it the old file extension. if (!IOHelper::getExtension($filename)) { $filename .= '.' . $file->getExtension(); } $filename = AssetsHelper::cleanAssetName($filename); if ($folderId == $file->folderId && $filename == $file->filename) { $response = new AssetOperationResponseModel(); $response->setSuccess(); $results[] = $response; } $originalSourceType = craft()->assetSources->getSourceTypeById($file->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; }
/** * @inheritDoc BaseAssetSourceType::insertFileInFolder() * * @param AssetFolderModel $folder * @param string $filePath * @param string $fileName * * @throws Exception * @return AssetOperationResponseModel */ protected function insertFileInFolder(AssetFolderModel $folder, $filePath, $fileName) { // Check if the set file system path exists $basePath = $this->getSourceFileSystemPath(); if (empty($basePath)) { $basePath = $this->getBasePath(); if (!empty($basePath)) { throw new Exception(Craft::t('The file system path “{folder}” set for this source does not exist.', array('folder' => $this->getBasePath()))); } } $targetFolder = $this->getSourceFileSystemPath() . $folder->path; // Make sure the folder exists. if (!IOHelper::folderExists($targetFolder)) { throw new Exception(Craft::t('The folder “{folder}” does not exist.', array('folder' => $targetFolder))); } // Make sure the folder is writable if (!IOHelper::isWritable($targetFolder)) { throw new Exception(Craft::t('The folder “{folder}” is not writable.', array('folder' => $targetFolder))); } $fileName = AssetsHelper::cleanAssetName($fileName); $targetPath = $targetFolder . $fileName; $extension = IOHelper::getExtension($fileName); if (!IOHelper::isExtensionAllowed($extension)) { throw new Exception(Craft::t('This file type is not allowed')); } if (IOHelper::fileExists($targetPath)) { $response = new AssetOperationResponseModel(); return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName); } if (!IOHelper::copyFile($filePath, $targetPath)) { throw new Exception(Craft::t('Could not copy file to target destination')); } IOHelper::changePermissions($targetPath, craft()->config->get('defaultFilePermissions')); $response = new AssetOperationResponseModel(); return $response->setSuccess()->setDataItem('filePath', $targetPath); }
/** * @inheritDoc BaseAssetSourceType::insertFileInFolder() * * @param AssetFolderModel $folder * @param $filePath * @param $fileName * * @throws Exception * @return AssetFileModel */ protected function insertFileInFolder(AssetFolderModel $folder, $filePath, $fileName) { $fileName = AssetsHelper::cleanAssetName($fileName); $extension = IOHelper::getExtension($fileName); if (!IOHelper::isExtensionAllowed($extension)) { throw new Exception(Craft::t('This file type is not allowed')); } $uriPath = $this->_getPathPrefix() . $folder->path . $fileName; $fileInfo = $this->_getObjectInfo($uriPath); if ($fileInfo) { $response = new AssetOperationResponseModel(); return $response->setPrompt($this->getUserPromptOptions($fileName))->setDataItem('fileName', $fileName); } clearstatcache(); // Upload file try { $this->_uploadFile($uriPath, $filePath); } catch (\Exception $exception) { throw new Exception(Craft::t('Could not copy file to target destination')); } $response = new AssetOperationResponseModel(); return $response->setSuccess()->setDataItem('filePath', $uriPath); }
/** * Insert a file from path in folder. * * @param AssetFolderModel $folder * @param $filePath * @param $fileName * @return AssetOperationResponseModel * @throws Exception */ protected function _insertFileInFolder(AssetFolderModel $folder, $filePath, $fileName) { $targetFolder = $this->_getSourceFileSystemPath() . $folder->fullPath; // Make sure the folder exists. if (!IOHelper::folderExists($targetFolder)) { throw new Exception(Craft::t('The “File System Path” specified for this asset source does not appear to exist.')); } // Make sure the folder is writable if (!IOHelper::isWritable($targetFolder)) { throw new Exception(Craft::t('Craft is not able to write to the “File System Path” specified for this asset source.')); } $fileName = IOHelper::cleanFilename($fileName); $targetPath = $targetFolder . $fileName; $extension = IOHelper::getExtension($fileName); if (!IOHelper::isExtensionAllowed($extension)) { throw new Exception(Craft::t('This file type is not allowed')); } if (IOHelper::fileExists($targetPath)) { $response = new AssetOperationResponseModel(); return $response->setPrompt($this->_getUserPromptOptions($fileName))->setDataItem('fileName', $fileName); } if (!IOHelper::copyFile($filePath, $targetPath)) { throw new Exception(Craft::t('Could not copy file to target destination')); } IOHelper::changePermissions($targetPath, IOHelper::getWritableFilePermissions()); $response = new AssetOperationResponseModel(); return $response->setSuccess()->setDataItem('filePath', $targetPath); }