/** * * 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() { $storageFileList = static::getFindFilesDirectory(); if ($storageFileList === false) { return false; } // check storage files which are not flagged as deleted $allStorageFileEntries = StorageFile::find()->where(['is_deleted' => 0])->indexBy('id')->asArray()->all(); foreach ($storageFileList as $key => $file) { foreach ($allStorageFileEntries as $dbfile) { if ($dbfile['name_new_compound'] == pathinfo($file, PATHINFO_BASENAME)) { unset($storageFileList[$key]); break; } } } // 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']; foreach ($storageFileList as $key => $file) { if ($filterImage == pathinfo($file, PATHINFO_BASENAME)) { unset($storageFileList[$key]); break; } } } } return $storageFileList; }
/** * * @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) { 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(); } return static::removeFile($fileId); } return false; }
/** * @todo this is a copy of the old code, clean up! * @param unknown $fileId * @param unknown $filterId */ public function addImage($fileId, $filterId = 0, $throwException = false) { try { $query = (new \admin\image\Query())->where(['file_id' => $fileId, 'filter_id' => $filterId])->one(); if ($query && $query->fileExists) { return $query; } $fileQuery = $this->getFile($fileId); if (!$fileQuery) { throw new Exception("Unable to create image, cause the base file does not exist."); } $imagine = new Imagine(); $image = $imagine->open($fileQuery->serverSource); $fileName = $filterId . '_' . $fileQuery->systemFileName; $fileSavePath = $this->serverPath . '/' . $fileName; if (empty($filterId)) { $save = $image->save($fileSavePath); } else { $model = StorageFilter::find()->where(['id' => $filterId])->one(); if (!$model) { throw new Exception("Could not find the provided filter id '{$filterId}'."); } $newimage = $model->applyFilter($image, $imagine); $save = $newimage->save($fileSavePath); } if (!$save) { throw new Exception("unable to store file {$fileSavePath}"); } $resolution = Storage::getImageResolution($fileSavePath); $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($err->getMessage(), $err->getCode(), $err); } } return false; }
/** * * 1. get all files from storage folder * 2. check each image/filter db entry if not available in file list and remove them from 'admin_storage_image' * * @return int count of removed entries */ public static function removeMissingImageFiles() { $storageFileList = static::getFindFilesDirectory(); if (!$storageFileList) { return false; } // check storage files $allStorageFileEntries = StorageFile::find()->indexBy('id')->all(); $count = 0; // check image filter files $imageList = StorageImage::find()->all(); foreach ($imageList as $image) { if (array_key_exists($image['file_id'], $allStorageFileEntries)) { $filterImage = $image['filter_id'] . '_' . $allStorageFileEntries[$image['file_id']]['name_new_compound']; $found = false; foreach ($storageFileList as $key => $file) { if ($filterImage == pathinfo($file, PATHINFO_BASENAME)) { $found = true; break; } } if (!$found) { $image->delete(); $count++; } } } return $count; }
public function get($imageId) { // get the real full image path to display this file. $data = StorageImage::find()->where(['id' => $imageId])->with('file')->one(); if (!$data || !isset($data->file)) { return false; } $fileName = implode([$data->filter_id, $data->file->name_new_compound], '_'); return ArrayHelper::toObject(['filter_id' => $data->filter_id, 'file_id' => $data->file_id, 'file_is_deleted' => $data->file->is_deleted, 'image_id' => $data->id, 'file_source' => $data->file->name_new_compound, 'image_source' => $fileName, 'source' => \yii::$app->storage->httpDir . $fileName]); }