diff --git a/README.md b/README.md index 0b8b7d2..44f799a 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,12 @@ query MyQuery { } ``` +8) You can also mass re-upload everything via Craft's cli + +```sh +./craft cloudflare-stream/reupload +``` + This extension uses Craft's Queue system, so make sure it works properly. Please make sure that Craft's max upload limit is also properly set. diff --git a/src/Plugin.php b/src/Plugin.php index 6c3a6b9..8284948 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -2,10 +2,13 @@ namespace deuxhuithuit\cfstream; +use craft\console\Controller; +use craft\events\DefineConsoleActionsEvent; use craft\events\RegisterComponentTypesEvent; use craft\events\RegisterTemplateRootsEvent; use craft\services\Fields; use craft\web\View; +use deuxhuithuit\cfstream\controllers\ReuploadController; use deuxhuithuit\cfstream\fields\CloudflareVideoStreamField; use deuxhuithuit\cfstream\jobs\DeleteVideoJob; use deuxhuithuit\cfstream\jobs\UploadVideoJob; @@ -199,6 +202,23 @@ function (\craft\events\AssetPreviewEvent $event) { [$this, 'autoDelete'] ); + Event::on( + ReuploadController::class, + Controller::EVENT_DEFINE_ACTIONS, + function (DefineConsoleActionsEvent $event) { + $event->actions['reupload'] = [ + 'options' => [], + 'helpSummary' => 'Re-uploads all Cloudflare stream assets.', + 'action' => function (): int { + /** @var CliController $controller */ + $controller = \Craft::$app->controller; + + return $controller->actionReupload(); + }, + ]; + } + ); + parent::init(); } diff --git a/src/controllers/ReuploadController.php b/src/controllers/ReuploadController.php new file mode 100644 index 0000000..4f04b6e --- /dev/null +++ b/src/controllers/ReuploadController.php @@ -0,0 +1,59 @@ +getFields()->getFieldsByType(CloudflareVideoStreamField::class); + $fieldsCount = is_array($fields) ? count($fields) : 0; + + $this->stdout("Found {$fieldsCount} fields", Console::FG_GREEN); + $this->stdout(PHP_EOL); + + $volumes = \Craft::$app->getVolumes()->getAllVolumes(); + $volumeCount = is_array($volumes) ? count($volumes) : 0; + + $this->stdout("Found {$volumeCount} volumes", Console::FG_GREEN); + $this->stdout(PHP_EOL); + + $uploadCount = 0; + foreach ($volumes as $volume) { + $this->stdout("Volume: {$volume->name}"); + $this->stdout(PHP_EOL); + foreach ($fields as $field) { + $this->stdout(" Field: {$field->handle}"); + $this->stdout(PHP_EOL); + + $entries = Asset::find()->volumeId($volume->id)->all(); + foreach ($entries as $asset) { + if (isset($asset->videoStream)) { + $uploadJob = new UploadVideoJob([ + 'fieldHandle' => $field->handle, + 'elementId' => $asset->id, + 'videoUrl' => $asset->getUrl(), + 'videoName' => $asset->title ?? $asset->filename, + ]); + \Craft::$app->getQueue()->push($uploadJob); + ++$uploadCount; + } + } + } + } + + $this->stdout("Created {$uploadCount} new upload jobs!", Console::FG_GREEN); + $this->stdout(PHP_EOL); + + return ExitCode::OK; + } +}