From 8a8a2d37e4c74303909a3383080058bd0bafe8f3 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 12 Mar 2023 17:55:41 +0200 Subject: [PATCH] subtitle lan code --- app/controllers/api/videos.php | 22 +++++++++++++- app/workers/transcoding.php | 1 + composer.lock | 12 ++++---- src/Appwrite/Extend/Exception.php | 1 + .../Videos/VideosCustomClientTest.php | 14 +++++++++ .../Videos/VideosCustomServerTest.php | 29 +++++++++++++++++-- 6 files changed, 70 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/videos.php b/app/controllers/api/videos.php index 4141e7aa51..32a01a7339 100644 --- a/app/controllers/api/videos.php +++ b/app/controllers/api/videos.php @@ -84,6 +84,15 @@ App::post('/v1/videos') ->action(action: function (string $bucketId, string $fileId, Request $request, Response $response, Database $dbForProject, string $mode) { $file = validateFilePermissions($dbForProject, $bucketId, $fileId, $mode); + + if ( + !str_starts_with($file->getAttribute('mimeType'), 'video/') && + !str_starts_with($file->getAttribute('mimeType'), 'audio/') && + $file->getAttribute('mimeType') !== 'application/ogg' + ) { + throw new Exception(Exception::VIDEO_NOT_VALID); + } + $video = Authorization::skip(function () use ($dbForProject, $bucketId, $file) { return $dbForProject->createDocument('videos', new Document([ 'bucketId' => $bucketId, @@ -275,8 +284,10 @@ App::post('/v1/videos/:videoId/subtitles') ->inject('mode') ->action(action: function (string $videoId, string $bucketId, string $fileId, string $name, string $code, bool $default, Request $request, Response $response, Database $dbForProject, string $mode) { + $code = strtolower($code); $languages = Config::getParam('locale-languages'); $found = array_search($code, array_column($languages, 'code2')); + if (!$found) { throw new Exception(Exception::VIDEO_LANGUAGE_CODE_NOT_FOUND); } @@ -288,7 +299,16 @@ App::post('/v1/videos/:videoId/subtitles') } validateFilePermissions($dbForProject, $video['bucketId'], $video['fileId'], $mode); - validateFilePermissions($dbForProject, $bucketId, $fileId, $mode); + $file = validateFilePermissions($dbForProject, $bucketId, $fileId, $mode); + + if ( + $file->getAttribute('mimeType') !== 'text/vtt' && + $file->getAttribute('mimeType') !== 'text/plain' + ) { + throw new Exception(Exception::VIDEO_SUBTITLE_NOT_VALID); + } + + $subtitle = Authorization::skip(fn() => $dbForProject->createDocument('videos_subtitles', new Document([ diff --git a/app/workers/transcoding.php b/app/workers/transcoding.php index c316cb100e..cbc64461bd 100644 --- a/app/workers/transcoding.php +++ b/app/workers/transcoding.php @@ -307,6 +307,7 @@ class TranscodingV1 extends Worker } console::info('Rendition ' . $query->getId() . ' conversion, done'); + /** Upload & cleanup **/ $start = 0; $fileNames = scandir($this->outDir); diff --git a/composer.lock b/composer.lock index e2e198da3b..3fea71c0eb 100644 --- a/composer.lock +++ b/composer.lock @@ -943,16 +943,16 @@ }, { "name": "guzzlehttp/psr7", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "67c26b443f348a51926030c83481b85718457d3d" + "reference": "3cf1b6d4f0c820a2cf8bcaec39fc698f3443b5cf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/67c26b443f348a51926030c83481b85718457d3d", - "reference": "67c26b443f348a51926030c83481b85718457d3d", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/3cf1b6d4f0c820a2cf8bcaec39fc698f3443b5cf", + "reference": "3cf1b6d4f0c820a2cf8bcaec39fc698f3443b5cf", "shasum": "" }, "require": { @@ -1042,7 +1042,7 @@ ], "support": { "issues": "https://github.com/guzzle/psr7/issues", - "source": "https://github.com/guzzle/psr7/tree/2.4.3" + "source": "https://github.com/guzzle/psr7/tree/2.4.4" }, "funding": [ { @@ -1058,7 +1058,7 @@ "type": "tidelift" } ], - "time": "2022-10-26T14:07:24+00:00" + "time": "2023-03-09T13:19:02+00:00" }, { "name": "influxdb/influxdb-php", diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 59dbdedf93..9237be6442 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -188,6 +188,7 @@ class Exception extends \Exception public const VIDEO_RENDITION_SEGMENT_NOT_FOUND = 'video_rendition_segment_not_found'; public const VIDEO_LANGUAGE_CODE_NOT_FOUND = 'video_language_code_not_found'; public const VIDEO_SUBTITLE_SEGMENT_NOT_FOUND = 'video_subtitle_segment_not_found'; + public const VIDEO_SUBTITLE_NOT_VALID = 'video_subtitle_not_valid'; /** GraphqQL */ diff --git a/tests/e2e/Services/Videos/VideosCustomClientTest.php b/tests/e2e/Services/Videos/VideosCustomClientTest.php index e2131cbd86..eac361627c 100644 --- a/tests/e2e/Services/Videos/VideosCustomClientTest.php +++ b/tests/e2e/Services/Videos/VideosCustomClientTest.php @@ -116,6 +116,20 @@ class VideosCustomClientTest extends Scope 'code' => 'It', ]); + $this->assertEquals(404, $response['headers']['status-code']); + + + $response = $this->client->call(Client::METHOD_POST, '/videos/' . $videoId . '/subtitles', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'bucketId' => $this->getBucket()['$id'], + 'fileId' => $this->getSubtitle()['$id'], + 'name' => 'Italian', + 'code' => 'Ita', + ]); + $this->assertEquals(201, $response['headers']['status-code']); $this->assertNotEmpty($response['body']); $this->assertNotEmpty($response['body']['$id']); diff --git a/tests/e2e/Services/Videos/VideosCustomServerTest.php b/tests/e2e/Services/Videos/VideosCustomServerTest.php index aa4d6e5c0b..2a9577a6dd 100644 --- a/tests/e2e/Services/Videos/VideosCustomServerTest.php +++ b/tests/e2e/Services/Videos/VideosCustomServerTest.php @@ -191,6 +191,17 @@ class VideosCustomServerTest extends Scope public function testCreateVideo(): string { + $response = $this->client->call(Client::METHOD_POST, '/videos', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'bucketId' => $this->getBucket()['$id'], + 'fileId' => $this->getSubtitle()['$id'] + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + $response = $this->client->call(Client::METHOD_POST, '/videos', [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -212,6 +223,20 @@ class VideosCustomServerTest extends Scope */ public function testCreateSubtitles($videoId) { + + $response = $this->client->call(Client::METHOD_POST, '/videos/' . $videoId . '/subtitles', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'bucketId' => $this->getBucket()['$id'], + 'fileId' => $this->getSubtitle()['$id'], + 'name' => 'English', + 'code' => 'xxx', + 'default' => true, + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $response = $this->client->call(Client::METHOD_POST, '/videos/' . $videoId . '/subtitles', [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -236,7 +261,7 @@ class VideosCustomServerTest extends Scope 'bucketId' => $this->getBucket()['$id'], 'fileId' => $this->getSubtitle()['$id'], 'name' => 'Italian', - 'code' => 'It', + 'code' => 'ita', ]); $this->assertEquals(201, $response['headers']['status-code']); @@ -398,7 +423,7 @@ class VideosCustomServerTest extends Scope 'bucketId' => $this->getBucket()['$id'], 'fileId' => $this->getSubtitle()['$id'], 'name' => 'Italian', - 'code' => 'It', + 'code' => 'Ita', ]); $this->assertEquals(201, $response['headers']['status-code']);