/** * Remove a file from the storage system. * * @param integer $fileId The file id to delete * @param boolean $cleanup If cleanup is enabled, also all images will be deleted, this is by default turned off because * casual you want to remove the large source file but not the images where used in several tables and situations. * @return boolean */ public static function removeFile($fileId, $cleanup = false) { $model = StorageFile::find()->where(['id' => $fileId, 'is_deleted' => 0])->one(); if ($model) { if ($cleanup) { foreach (Yii::$app->storage->findImages(['file_id' => $fileId]) as $imageItem) { StorageImage::findOne($imageItem->id)->delete(); } } $response = $model->delete(); Yii::$app->storage->flushArrays(); return $response; } return true; }
/** * Mark not found files as deleted. * * 1. get all files from storage folder * 2. check each db entry if not available in file list and set is_deleted = 1 * * @return int count of flagged 'is_deleted' entries */ public static function removeMissingStorageFiles() { $storageFileList = static::getFindFilesDirectory(); if (!$storageFileList) { return 0; } // check storage files $allStorageFileEntries = StorageFile::find()->where(['is_deleted' => 0])->indexBy('id')->all(); $count = 0; // build storagefilelist index $storageFileIndex = []; foreach ($storageFileList as $key => $file) { $storageFileIndex[] = pathinfo($file, PATHINFO_BASENAME); } foreach ($allStorageFileEntries as $dbfile) { if (!in_array($dbfile['name_new_compound'], $storageFileIndex)) { $dbfile->updateAttributes(['is_deleted' => 1]); $count++; } } return $count; }
/** * delete folder, all subfolders and all included files. * * 1. search another folders with matching parentIds and call deleteFolder on them * 2. get all included files and delete them * 3. delete folder * * @param integer $folderId The folder to delete. * @todo move to storage helpers? * @return boolean */ public function actionFolderDelete($folderId) { // find all subfolders $matchingChildFolders = StorageFolder::find()->where(['parent_id' => $folderId])->asArray()->all(); foreach ($matchingChildFolders as $matchingChildFolder) { $this->actionFolderDelete($matchingChildFolder['id']); } // find all attached files and delete them $folderFiles = StorageFile::find()->where(['folder_id' => $folderId])->all(); foreach ($folderFiles as $folderFile) { $folderFile->delete(); } // delete folder $model = StorageFolder::findOne($folderId); if (!$model) { return false; } $model->is_deleted = true; $this->flushApiCache(); return $model->update(); }