|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: floor12 |
|
5
|
|
|
* Date: 27.06.2016 |
|
6
|
|
|
* Time: 8:32 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace floor12\files\controllers; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use floor12\files\models\File; |
|
13
|
|
|
use floor12\files\models\FileType; |
|
14
|
|
|
use floor12\files\models\VideoStatus; |
|
15
|
|
|
use Throwable; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\console\Controller; |
|
18
|
|
|
use yii\db\ActiveRecord; |
|
19
|
|
|
use yii\db\StaleObjectException; |
|
20
|
|
|
use yii\helpers\Console; |
|
21
|
|
|
|
|
22
|
|
|
class ConsoleController extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Run `./yii files/console/clean` to remove all unlinked files more then 6 hours |
|
27
|
|
|
* |
|
28
|
|
|
* @throws Throwable |
|
29
|
|
|
* @throws StaleObjectException |
|
30
|
|
|
*/ |
|
31
|
|
|
function actionClean() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$time = strtotime('- 6 hours'); |
|
34
|
|
|
$files = File::find()->where(['object_id' => '0'])->andWhere(['<', 'created', $time])->all(); |
|
35
|
|
|
if ($files) foreach ($files as $file) { |
|
|
|
|
|
|
36
|
|
|
$file->delete(); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Run `./yii files/console/clean-cache` to remove all generated images and previews |
|
42
|
|
|
*/ |
|
43
|
|
|
function actionCleanCache() |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$module = Yii::$app->getModule('files'); |
|
46
|
|
|
$commands = []; |
|
47
|
|
|
$commands[] = "find {$module->storageFullPath} -regextype egrep -regex \".+/.{32}_.*\" -exec rm -rf {} \;"; |
|
48
|
|
|
$commands[] = "find {$module->cacheFullPath} -regextype egrep -regex \".+/.{32}_.*\" -exec rm -rf {} \;"; |
|
49
|
|
|
$commands[] = "find {$module->storageFullPath} -regextype egrep -regex \".+/.{32}\..{3,4}\.jpg\" -exec rm -rf {} \;"; |
|
50
|
|
|
$commands[] = "find {$module->cacheFullPath} -regextype egrep -regex \".+/.{32}\..{3,4}\.jpg\" -exec rm -rf {} \;"; |
|
51
|
|
|
|
|
52
|
|
|
array_map(function ($command) { |
|
53
|
|
|
exec($command); |
|
54
|
|
|
}, $commands); |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Run `./yii files/console/convert` to proccess one video file from queue with ffmpeg |
|
60
|
|
|
* @return bool|int |
|
61
|
|
|
*/ |
|
62
|
|
|
function actionConvert() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$ffmpeg = Yii::$app->getModule('files')->ffmpeg; |
|
65
|
|
|
|
|
66
|
|
|
if (!file_exists($ffmpeg)) |
|
|
|
|
|
|
67
|
|
|
return $this->stdout("ffmpeg is not found: {$ffmpeg}" . PHP_EOL, Console::FG_RED); |
|
68
|
|
|
|
|
69
|
|
|
if (!is_executable($ffmpeg)) |
|
|
|
|
|
|
70
|
|
|
return $this->stdout("ffmpeg is not executable: {$ffmpeg}" . PHP_EOL, Console::FG_RED); |
|
71
|
|
|
|
|
72
|
|
|
$file = File::find() |
|
73
|
|
|
->where(['type' => FileType::VIDEO, 'video_status' => VideoStatus::QUEUE]) |
|
74
|
|
|
->andWhere(['!=', 'object_id', 0]) |
|
75
|
|
|
->one(); |
|
76
|
|
|
|
|
77
|
|
|
if (!$file) |
|
78
|
|
|
return $this->stdout("Convert queue is empty" . PHP_EOL, Console::FG_GREEN); |
|
79
|
|
|
|
|
80
|
|
|
if (!file_exists($file->rootPath)) |
|
81
|
|
|
return $this->stdout("Source file is not found: {$file->rootPath}" . PHP_EOL, Console::FG_RED); |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$file->video_status = VideoStatus::CONVERTING; |
|
85
|
|
|
$file->save(); |
|
86
|
|
|
$width = $this->getVideoWidth($file->class, $file->field); |
|
87
|
|
|
$height = $this->getVideoHeight($file->class, $file->field); |
|
88
|
|
|
$newFileName = $file->filename . ".mp4"; |
|
89
|
|
|
$newFilePath = $file->rootPath . ".mp4"; |
|
90
|
|
|
$command = Yii::$app->getModule('files')->ffmpeg . " -i {$file->rootPath} -vf scale={$width}:{$height} -threads 4 {$newFilePath}"; |
|
|
|
|
|
|
91
|
|
|
echo $command . PHP_EOL; |
|
92
|
|
|
exec($command, |
|
93
|
|
|
$outout, $result); |
|
94
|
|
|
if ($result == 0) { |
|
95
|
|
|
@unlink($file->rootPath); |
|
|
|
|
|
|
96
|
|
|
$file->filename = $newFileName; |
|
97
|
|
|
$file->video_status = VideoStatus::READY; |
|
98
|
|
|
} else { |
|
99
|
|
|
$file->video_status = VideoStatus::QUEUE; |
|
100
|
|
|
} |
|
101
|
|
|
$file->save(); |
|
102
|
|
|
|
|
103
|
|
|
return $this->stdout("File converted: {$file->rootPath}" . PHP_EOL, Console::FG_GREEN); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function getVideoWidth($classname, $field) |
|
107
|
|
|
{ |
|
108
|
|
|
/** @var ActiveRecord $ownerClassObject */ |
|
109
|
|
|
$ownerClassObject = new $classname; |
|
110
|
|
|
return $ownerClassObject->getBehavior('files')->attributes[$field]['videoWidth'] ?? 1280; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function getVideoHeight($classname, $field) |
|
114
|
|
|
{ |
|
115
|
|
|
/** @var ActiveRecord $ownerClassObject */ |
|
116
|
|
|
$ownerClassObject = new $classname; |
|
117
|
|
|
return $ownerClassObject->getBehavior('files')->attributes[$field]['videoHeight'] ?? -1; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.