From 51466c89215ed54cbca70bcd56b1c1cf85017b2a Mon Sep 17 00:00:00 2001 From: Aleksey Filippov Date: Tue, 24 Feb 2026 17:24:56 +0300 Subject: [PATCH] fix(ERP-236): filter video conversion to files younger than 30 days Files older than 1 month are cleaned by write-offs-attachments anyway. Added --days option (default 30, 0=all). Co-Authored-By: Claude Opus 4.6 --- erp24/commands/ConvertVideoController.php | 48 +++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/erp24/commands/ConvertVideoController.php b/erp24/commands/ConvertVideoController.php index 2724ce07..023a2bac 100644 --- a/erp24/commands/ConvertVideoController.php +++ b/erp24/commands/ConvertVideoController.php @@ -12,8 +12,12 @@ use yii_app\records\Files; /** * Массовая конвертация MOV → MP4 для существующих файлов. * + * По умолчанию конвертирует только файлы младше 1 месяца (старые удаляются через write-offs-attachments). + * * Запуск: - * php yii convert-video/run — конвертация всех MOV + * php yii convert-video/run — конвертация MOV младше 1 месяца + * php yii convert-video/run --days=7 — только за последние 7 дней + * php yii convert-video/run --days=0 — все файлы без фильтра по дате * php yii convert-video/run --limit=50 — конвертация первых 50 * php yii convert-video/run --dry-run — только показать что будет конвертировано * php yii convert-video/status — статистика MOV/MP4 @@ -32,12 +36,16 @@ class ConvertVideoController extends Controller /** @var int Пауза между файлами в секундах (снижает нагрузку) */ public int $sleep = 2; + /** @var int Конвертировать файлы за последние N дней (0 = все, по умолчанию 30) */ + public int $days = 30; + public function options($actionID): array { return array_merge(parent::options($actionID), [ 'limit', 'dryRun', 'sleep', + 'days', ]); } @@ -47,6 +55,7 @@ class ConvertVideoController extends Controller 'l' => 'limit', 'n' => 'dryRun', 's' => 'sleep', + 'd' => 'days', ]; } @@ -55,11 +64,28 @@ class ConvertVideoController extends Controller */ public function actionStatus(): int { - $movCount = Files::find()->where(['like', 'url', '.mov'])->count(); - $mp4Count = Files::find()->where(['like', 'url', '.mp4'])->count(); - $aviCount = Files::find()->where(['like', 'url', '.avi'])->count(); + $borderDate = $this->days > 0 ? date('Y-m-d H:i:s', strtotime("-{$this->days} days")) : null; + + $movQuery = Files::find()->where(['like', 'url', '.mov']); + $mp4Query = Files::find()->where(['like', 'url', '.mp4']); + $aviQuery = Files::find()->where(['like', 'url', '.avi']); + + if ($borderDate) { + $movQuery->andWhere(['>=', 'created_at', $borderDate]); + $mp4Query->andWhere(['>=', 'created_at', $borderDate]); + $aviQuery->andWhere(['>=', 'created_at', $borderDate]); + } + + $movCount = $movQuery->count(); + $mp4Count = $mp4Query->count(); + $aviCount = $aviQuery->count(); $this->stdout("=== Статистика видеофайлов ===\n"); + if ($borderDate) { + $this->stdout("Фильтр: за последние {$this->days} дней (с {$borderDate})\n"); + } else { + $this->stdout("Фильтр: все файлы\n"); + } $this->stdout("MOV: {$movCount}\n"); $this->stdout("MP4: {$mp4Count}\n"); $this->stdout("AVI: {$aviCount}\n"); @@ -80,10 +106,18 @@ class ConvertVideoController extends Controller return ExitCode::UNSPECIFIED_ERROR; } + $borderDate = $this->days > 0 ? date('Y-m-d H:i:s', strtotime("-{$this->days} days")) : null; + $query = Files::find() - ->where(['like', 'url', '.mov']) - ->orWhere(['like', 'url', '.avi']) - ->orderBy(['id' => SORT_ASC]); + ->andWhere(['or', ['like', 'url', '.mov'], ['like', 'url', '.avi']]) + ->orderBy(['id' => SORT_DESC]); + + if ($borderDate) { + $query->andWhere(['>=', 'created_at', $borderDate]); + $this->stdout("Фильтр: за последние {$this->days} дней (с {$borderDate})\n"); + } else { + $this->stdout("Фильтр: все файлы\n"); + } if ($this->limit > 0) { $query->limit($this->limit); -- 2.39.5