From 4cd273e9768e2fc0b12503c3596fee365a3cc060 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 9 Apr 2023 12:58:21 +0300 Subject: [PATCH] updates --- app/config/collections.php | 11 ++++++ app/controllers/api/videos.php | 5 ++- app/workers/deletes.php | 19 +++++----- app/workers/videos.php | 63 +++++++++++++++++++--------------- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 63cb5cd8a9..081153cac2 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -3800,6 +3800,17 @@ $collections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => 'second', + 'type' => Database::VAR_INTEGER, + 'format' => '', + 'size' => 0, + 'signed' => false, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ diff --git a/app/controllers/api/videos.php b/app/controllers/api/videos.php index f86ed50e94..5ae6d95823 100644 --- a/app/controllers/api/videos.php +++ b/app/controllers/api/videos.php @@ -335,7 +335,7 @@ App::post('/v1/videos/:videoId/preview') ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) ->label('sdk.namespace', 'videos') ->label('sdk.method', 'createPreview') - ->label('sdk.description', '/docs/references/videos/create.md') // TODO: Create markdown + ->label('sdk.description', '/docs/references/videos/create-preview.md') // TODO: Create markdown ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_VIDEO) @@ -354,8 +354,7 @@ App::post('/v1/videos/:videoId/preview') } validateFilePermissions($dbForProject, $video['bucketId'], $video['fileId'], $mode); - var_dump($second); - var_dump($video['duration']); + $range = new Range(1, ($video['duration'] / 1000), Validator::TYPE_INTEGER); if (!$range->isValid($second)) { throw new Exception(Exception::VIDEO_SECOND_OUT_OF_RANGE); diff --git a/app/workers/deletes.php b/app/workers/deletes.php index dbf7614750..980b948f57 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -722,20 +722,9 @@ class DeletesV1 extends Worker protected function deleteBucket(Document $document, string $projectId) { $dbForProject = $this->getProjectDB($projectId); - $dbForProject->deleteCollection('bucket_' . $document->getInternalId()); - $dbForProject->deleteCollection('videos_' . $document->getInternalId()); - $dbForProject->deleteCollection('videos_' . $document->getInternalId() . '_renditions'); - $dbForProject->deleteCollection('videos_' . $document->getInternalId() . '_renditions_segments'); - $dbForProject->deleteCollection('videos_' . $document->getInternalId() . '_subtitles'); - $dbForProject->deleteCollection('videos_' . $document->getInternalId() . '_subtitles_segments'); - $dbForProject->deleteCollection('videos_' . $document->getInternalId() . '_profiles'); - $device = $this->getFilesDevice($projectId); $device->deletePath($document->getId()); - - $device = $this->getVideoDevice($projectId); - $device->deletePath($document->getId()); } /** @@ -747,6 +736,14 @@ class DeletesV1 extends Worker $videoId = $document->getId(); $dbForProject = $this->getProjectDB($projectId); + + $previews = $dbForProject->find('videos_previews', [ + new Query('videoId', Query::TYPE_EQUAL, [$videoId]) + ]); + foreach ($previews as $preview) { + $dbForProject->deleteDocument('videos_previews', $preview->getId()); + } + $renditions = $dbForProject->find('videos_renditions', [ new Query('videoId', Query::TYPE_EQUAL, [$videoId]) ]); diff --git a/app/workers/videos.php b/app/workers/videos.php index fe28a2297e..03c5ec7ae1 100644 --- a/app/workers/videos.php +++ b/app/workers/videos.php @@ -6,7 +6,6 @@ use Appwrite\Extend\Exception; use Appwrite\Messaging\Adapter\Realtime; use Appwrite\OpenSSL\OpenSSL; use Appwrite\Resque\Worker; -use Appwrite\Utopia\Response\Model\Execution; use Streaming\FFMpeg; use FFMpeg\FFProbe; use Streaming\Format\StreamFormat; @@ -19,7 +18,6 @@ use Utopia\CLI\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; -use Utopia\Database\Exception\Duplicate; use Utopia\Database\Query; use Utopia\Storage\Compression\Algorithms\GZIP; use Utopia\Storage\Compression\Algorithms\Zstd; @@ -191,33 +189,42 @@ class VideosV1 extends Worker (new Local('/'))->read($this->outDir . $name) ); - try { + $preview = $this->database->findOne('videos_previews', [ + Query::equal('videoId', [$this->video->getId()]), + Query::equal('type', [$this->action]), + Query::equal('name', [$name]), + ]); + + if (empty($preview)) { $preview = $this->database->createDocument('videos_previews', new Document([ 'videoId' => $this->video->getId(), - 'type' => 'preview', + 'type' => $this->action, 'name' => $name, 'path' => $path, + 'second' => $this->args['second'], ])); - } catch (Duplicate $th) { - ; + + $this->video->setAttribute('previewId', $preview->getId()); + $this->database->updateDocument( + 'videos', + $this->video->getId(), + new document( + array_filter((array)$this->video, fn ($value) => !is_null($value)) + ), + ); + } else { + $this->database->updateDocument( + 'videos_previews', + $preview->getId(), + $preview->setAttribute('second', $this->args['second']) + ); + + (new Delete()) + ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) + ->setResource('preview/' . $preview->getId()) + ->trigger(); } - $this->video->setAttribute('previewId', $preview->getId()); - - (new Delete()) - ->setType(DELETE_TYPE_CACHE_BY_RESOURCE) - ->setResource('preview/' . $preview->getId()) - ->trigger(); - - - $this->database->updateDocument( - 'videos', - $this->video->getId(), - new document( - array_filter((array)$this->video, fn ($value) => !is_null($value)) - ), - ); - return; } @@ -367,9 +374,11 @@ class VideosV1 extends Worker 'audioBitRate' => $this->profile->getAttribute('audioBitRate'), 'output' => $this->args['output'], ])); + $this->send($query); + $renditionRootPath = $this->getVideoDevice($this->project->getId())->getPath($this->video->getId()) . '/'; - $renditionPath = $renditionRootPath . $this->renditionName . '-' . $query->getId() . '/'; + $renditionPath = $renditionRootPath . $this->renditionName . '-' . $query->getId() . '/'; try { $representation = (new Representation()) @@ -451,8 +460,8 @@ class VideosV1 extends Worker foreach ($m3u8['segments'] ?? [] as $segment) { $this->database->createDocument('videos_subtitles_segments', new Document([ 'subtitleId' => $subtitle->getId(), - 'fileName' => $segment['fileName'], - 'path' => $renditionRootPath , + 'fileName' => $segment['fileName'], + 'path' => $renditionRootPath . 'subtitles/', 'duration' => $segment['duration'], ])); } @@ -462,7 +471,7 @@ class VideosV1 extends Worker } $subtitle->setAttribute('status', self::STATUS_READY); - $subtitle->setAttribute('path', $renditionRootPath); + $subtitle->setAttribute('path', $renditionRootPath . 'subtitles/'); $this->database->updateDocument('videos_subtitles', $subtitle->getId(), $subtitle); } @@ -477,7 +486,7 @@ class VideosV1 extends Worker $data = (new Local('/'))->read($this->outDir . $fileinfo->getFilename()); $to = $renditionPath; if (str_contains($fileinfo->getFilename(), "_subtitles_") || str_contains($fileinfo->getFilename(), ".vtt")) { - $to = $renditionRootPath; + $to = $renditionRootPath . 'subtitles/'; } $this->getVideoDevice($this->project->getId())->write($to . $fileinfo->getFilename(), $data);