Пример #1
0
 /**
  * Get orphaned files array.
  *
  * 1. get all files from storage folder
  * 2. check each file if available in db tables ('admin_storage_file' and 'admin_storage_image')
  * 3. remove each found entry and return list with all remaining orphaned files
  *
  * @return array list of orphaned files
  */
 public static function getOrphanedFileList()
 {
     $diskFiles = static::getFindFilesDirectory();
     if ($diskFiles === false) {
         return false;
     }
     //build storagefilelist index
     foreach ($diskFiles as $key => $file) {
         $diskFiles[pathinfo($file, PATHINFO_BASENAME)] = $file;
         unset($diskFiles[$key]);
     }
     // check storage files which are not flagged as deleted
     foreach (StorageFile::find()->where(['is_deleted' => 0])->indexBy('id')->asArray()->all() as $dbfile) {
         if (isset($diskFiles[$dbfile['name_new_compound']])) {
             unset($diskFiles[$dbfile['name_new_compound']]);
         }
         unset($dbfile);
     }
     // check image filter files
     $imageList = StorageImage::find()->asArray()->all();
     // check all storage files including is_deleted entries
     $allStorageFileEntries = StorageFile::find()->indexBy('id')->asArray()->all();
     foreach ($imageList as $image) {
         if (array_key_exists($image['file_id'], $allStorageFileEntries)) {
             $filterImage = $image['filter_id'] . '_' . $allStorageFileEntries[$image['file_id']]['name_new_compound'];
             if (isset($diskFiles[$filterImage])) {
                 unset($diskFiles[$filterImage]);
             }
         }
         unset($image);
     }
     return $diskFiles;
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function beforeDelete()
 {
     if (parent::beforeDelete()) {
         foreach (StorageImage::find()->where(['filter_id' => $this->id])->all() as $img) {
             $img->delete();
         }
         return true;
     }
     return false;
 }
Пример #3
0
 /**
  * Remove an image from the storage system.
  *
  * @param integer $imageId
  * @param boolean $cleanup If cleanup is enabled, all other images will be deleted, the source file will be deleted to
  * if clean is disabled, only the provided $imageId will be removed.
  * @since 1.0.0-beta3
  */
 public static function removeImage($imageId, $cleanup = true)
 {
     if (!$cleanup) {
         Yii::$app->storage->flushArrays();
         return StorageImage::findOne($imageId)->delete();
     }
     $image = Yii::$app->storage->getImage($imageId);
     if ($image) {
         $fileId = $image->fileId;
         foreach (Yii::$app->storage->findImages(['file_id' => $fileId]) as $imageItem) {
             StorageImage::findOne($imageItem->id)->delete();
         }
         Yii::$app->storage->flushArrays();
         return static::removeFile($fileId);
     }
     return false;
 }
 /**
  * Add a new image based an existing file Id.
  *
  * The storage system uses the same file base, for images and files. The difference between a file and an image is the filter which is applied.
  *
  * Only files of the type image can be used (or added) as an image.
  *
  * An image object is always based on the {{\luya\admin\file\Item}} object and a {{luya\admin\base\Filter}}.
  *
  * ```php
  * Yii::$app->storage->addImage(123, 0); // create an image from file object id 123 without filter.
  * ```
  *
  * @param integer $fileId The id of the file where image should be created from.
  * @param integer $filterId The id of the filter which should be applied to, if filter is 0, no filter will be added. Filter can new also be the string name of the filter like `tiny-crop`.
  * @param boolean $throwException Whether the addImage should throw an exception or just return boolean
  * @return \luya\admin\image\Item|\luya\Exception|boolean Returns the item object, if an error happens and $throwException is off `false` is returned otherwhise an exception is thrown.
  */
 public function addImage($fileId, $filterId = 0, $throwException = false)
 {
     try {
         // if the filterId is provded as a string the filter will be looked up by its name in the get filters array list.
         if (is_string($filterId) && !is_numeric($filterId)) {
             $filterLookup = $this->getFiltersArrayItem($filterId);
             if (!$filterLookup) {
                 throw new Exception("The provided filter name " . $filterId . " does not exist.");
             }
             $filterId = $filterLookup['id'];
         }
         $query = (new \luya\admin\image\Query())->where(['file_id' => $fileId, 'filter_id' => $filterId])->one();
         if ($query && $query->fileExists) {
             return $query;
         }
         $fileQuery = $this->getFile($fileId);
         if (!$fileQuery || !$fileQuery->fileExists) {
             throw new Exception("Unable to create image, cause the base file does not exist.");
         }
         $fileName = $filterId . '_' . $fileQuery->systemFileName;
         $fileSavePath = $this->serverPath . '/' . $fileName;
         if (empty($filterId)) {
             $save = @copy($fileQuery->serverSource, $fileSavePath);
         } else {
             $model = StorageFilter::find()->where(['id' => $filterId])->one();
             if (!$model) {
                 throw new Exception("Could not find the provided filter id '{$filterId}'.");
             }
             if (!$model->applyFilterChain($fileQuery, $fileSavePath)) {
                 throw new Exception("Unable to create and save image '" . $fileSavePath . "'.");
             }
         }
         $resolution = Storage::getImageResolution($fileSavePath);
         // ensure the existing of the model
         $model = StorageImage::find()->where(['file_id' => $fileId, 'filter_id' => $filterId])->one();
         if ($model) {
             $model->updateAttributes(['resolution_width' => $resolution['width'], 'resolution_height' => $resolution['height']]);
         } else {
             $model = new StorageImage();
             $model->setAttributes(['file_id' => $fileId, 'filter_id' => $filterId, 'resolution_width' => $resolution['width'], 'resolution_height' => $resolution['height']]);
             if (!$model->save()) {
                 throw new Exception("Unable to save storage image, fatal database exception.");
             }
         }
         $this->_imagesArray[$model->id] = $model->toArray();
         $this->deleteHasCache($this->_imageCacheKey);
         return $this->getImage($model->id);
     } catch (\Exception $err) {
         if ($throwException) {
             throw new Exception("add image exception: " . $err->getMessage(), 0, $err);
         }
     }
     return false;
 }