Skip to content

Commit

Permalink
Added command d3files/clean-files/unused-files
Browse files Browse the repository at this point in the history
  • Loading branch information
uldisn committed Feb 10, 2022
1 parent a3752a9 commit fa8496a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,26 @@ D3filesModel::createCopy($fileModelId, Users::class, $model->id);

### Maintenance commands

Soft deletes files (sets deleted=0)

Soft deletes files (sets deleted=1)
For model dektrium\user\models\User remove from database model dektrium\user\models\User oldest as 12 months files
```bash
d3files/remove-older-than full/model/name months
yii d3files/remove-older-than 'dektrium\user\models\User' 12
```

Deletes files and corresponding records in database which have deleted=1
```bash
yii d3files/remove-files 'dektrium\user\models\User'
```

Deletes files and corresponding records in database which have deleted=0
Deletes model files from filesystem with out refence in database
```bash
yii d3files/clean-files/unused-files 'poker\poker\models\PkPlaygroundFixes'

```bash
d3files/remove-files full/model/name
```

### Change log
- 0.9.0 (Feb 26, 2017) - added RU translation
- 0.9.3 (May 29, 2017) - auto creating upload directories
- 0.9.4 (Nov 16, 2017) - added parameter controllerRoute
- 0.9.13 (Jul 2, 2018) - added action column
- 0.9.93 (febr 10, 2022) - added maint
2 changes: 1 addition & 1 deletion components/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function validateFileExtentsion(): void
* @return string
* @return string
*/
protected static function getUploadDirPath($model_name): string
public static function getUploadDirPath($model_name): string
{
$pos = strrpos($model_name, '\\');
$modelShortName = false === $pos ? $model_name : substr($model_name, $pos + 1);
Expand Down
55 changes: 49 additions & 6 deletions controllers/CleanFilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

namespace d3yii2\d3files\controllers;

use yii\console\Controller;
use d3yii2\d3files\models\D3files;
use d3yii2\d3files\models\D3filesModel;
use d3yii2\d3files\components\FileHandler;
use d3system\commands\D3CommandController;
use yii\helpers\VarDumper;

class CleanFilesController extends D3CommandController
{
Expand All @@ -17,13 +18,11 @@ class CleanFilesController extends D3CommandController
*
* @param $modelName
* @param $months
* @throws \ReflectionException
* @throws \Throwable
* @throws \yii\db\StaleObjectException
*
* @return int
*/
public function actionRemoveOlderThan($modelName, $months)
public function actionRemoveOlderThan($modelName, $months): int
{
$date = date('Y-m-d', strtotime('-'.$months.' month'));

Expand Down Expand Up @@ -52,9 +51,10 @@ public function actionRemoveOlderThan($modelName, $months)
* with value "deleted = 1"
*
* @param $modelName
* @throws \ReflectionException
*
* @return int
* @throws \ReflectionException
* @throws \Throwable
* @throws \yii\db\StaleObjectException
*/
public function actionRemoveFiles($modelName)
{
Expand All @@ -66,6 +66,7 @@ public function actionRemoveFiles($modelName)

foreach ($deletedFiles as $fileModel) {

/** @var D3files $file */
$file = $fileModel->getD3files()->one();

$fileHandler = new FileHandler(
Expand Down Expand Up @@ -94,5 +95,47 @@ public function actionRemoveFiles($modelName)

return 0;
}

/**
* check all model files in upload directory and check it in db. if not used, delete
* @param string $modelName
* @return int
*/
public function actionUnusedFiles(string $modelName): int
{
$dirPath = FileHandler::getUploadDirPath($modelName);
$handle = opendir($dirPath);

if ($handle) {
$i = 1;
while (($entry = readdir($handle)) !== FALSE) {
if ($entry ==='.') {
continue;
}
if ($entry ==='..') {
continue;
}
$i++;
$this->out($entry);
$fileName = pathinfo($entry, PATHINFO_FILENAME);
if (!preg_match('#^\d+$#',$fileName)) {
$this->out(' ilegal file name');
continue;
}
if (D3files::findOne($fileName)) {
//$this->out(' Izmanto');
continue;
}
$this->out(' unused');
if (!unlink($dirPath . '/' . $entry)){
$this->out(' Error: can not unlink');
continue;
}
$this->out(' unlinked');
}
}

return 0;
}

}

0 comments on commit fa8496a

Please sign in to comment.