/**
  * Remove files from the storage component.
  *
  * @todo make permission check.
  * @return boolean
  */
 public function actionFilemanagerRemoveFiles()
 {
     foreach (Yii::$app->request->post('ids', []) as $id) {
         if (!Storage::removeFile($id)) {
             return false;
         }
     }
     return true;
 }
示例#2
0
 public function testErrorUploadFromFiles()
 {
     $files[] = ['tmp_name' => 'not/found.jpg', 'name' => 'image.jpg', 'type' => 'image/jpg', 'error' => 1, 'size' => 123];
     $response = Storage::uploadFromFiles($files);
     $this->assertFalse($response['upload']);
 }
 /**
  * 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;
 }
 public function callbackRemove($imageId)
 {
     $image = Yii::$app->storage->getImage($imageId);
     if ($image) {
         $this->modelItem->flowDeleteImage($image);
         if (Storage::removeImage($image->id, true)) {
             return $this->sendSuccess('image has been removed');
         }
     }
     return $this->sendError('Unable to remove this image');
 }