Exemple #1
0
 /**
  * @param $newPath
  * @return bool
  */
 public function move($newPath)
 {
     if (!IOHelper::move($this->getRealPath(), $newPath)) {
         return false;
     }
     return true;
 }
 /**
  * Start import task.
  */
 public function actionImport()
 {
     // Get import post
     $settings = craft()->request->getRequiredPost('import');
     // Get file
     $file = craft()->request->getParam('file');
     // Get mapping fields
     $map = craft()->request->getParam('fields');
     $unique = craft()->request->getParam('unique');
     // Get rows/steps from file
     $rows = count(craft()->import->data($file));
     // Proceed when atleast one row
     if ($rows) {
         // Set more settings
         $settings = array_merge(array('file' => $file, 'rows' => $rows, 'map' => $map, 'unique' => $unique), $settings);
         // Create history
         $history = craft()->import_history->start($settings);
         // Add history to settings
         $settings['history'] = $history;
         // UNCOMMENT FOR DEBUGGING
         //craft()->import->debug($settings, $history, 1);
         // Determine new folder to save original importfile
         $folder = dirname($file) . '/' . $history . '/';
         IOHelper::ensureFolderExists($folder);
         // Move the file to its history folder
         IOHelper::move($file, $folder . basename($file));
         // Update the settings with the new file location
         $settings['file'] = $folder . basename($file);
         // Create the import task
         $task = craft()->tasks->createTask('Import', Craft::t('Importing') . ' ' . basename($file), $settings);
         // Notify user
         craft()->userSession->setNotice(Craft::t('Import process started.'));
         // Redirect to history
         $this->redirect('import/history?task=' . $task->id);
     } else {
         // Redirect to history
         $this->redirect('import/history');
     }
 }
 /**
  * Saves the image to the target path.
  *
  * @param string $targetPath
  *
  * @throws \Imagine\Exception\RuntimeException
  * @return null
  */
 public function saveAs($targetPath, $autoQuality = false)
 {
     $extension = StringHelper::toLowerCase(IOHelper::getExtension($targetPath));
     $options = $this->_getSaveOptions(false, $extension);
     $targetPath = IOHelper::getFolderName($targetPath) . IOHelper::getFileName($targetPath, false) . '.' . IOHelper::getExtension($targetPath);
     if ($autoQuality && in_array($extension, array('jpeg', 'jpg', 'png'))) {
         clearstatcache();
         $originalSize = IOHelper::getFileSize($this->_imageSourcePath);
         $tempFile = $this->_autoGuessImageQuality($targetPath, $originalSize, $extension, 0, 200);
         IOHelper::move($tempFile, $targetPath, true);
     } else {
         $this->_image->save($targetPath, $options);
     }
     return true;
 }
 /**
  * @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);
 }
 /**
  * Store a local image copy to a destination path.
  *
  * @param $localCopy
  * @param $destination
  *
  * @return null
  */
 public function storeLocalSource($localCopy, $destination)
 {
     $maxCachedImageSize = $this->getCachedCloudImageSize();
     // Resize if constrained by maxCachedImageSizes setting
     if ($maxCachedImageSize > 0 && ImageHelper::isImageManipulatable($localCopy)) {
         craft()->images->loadImage($localCopy)->scaleToFit($maxCachedImageSize, $maxCachedImageSize)->setQuality(100)->saveAs($destination);
         if ($localCopy != $destination) {
             IOHelper::deleteFile($localCopy);
         }
     } else {
         if ($localCopy != $destination) {
             IOHelper::move($localCopy, $destination);
         }
     }
 }
 /**
  * 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->_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);
 }