/** * * 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; }
/** * * 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]); }