From cea3d90d43035d8750dd52eb762a45daceadb4d2 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 28 Oct 2026 15:26:16 +0100 Subject: [PATCH] add file format detector tests, use FileShaped in audio2text:subtitles provider Signed-off-by: Julien Veyssier --- .../AudioToTextSubtitlesProvider.php | 3 +- tests/unit/Service/FileFormatDetectorTest.php | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Service/FileFormatDetectorTest.php diff --git a/lib/TaskProcessing/AudioToTextSubtitlesProvider.php b/lib/TaskProcessing/AudioToTextSubtitlesProvider.php index 7a90e72e..353092e5 100644 --- a/lib/TaskProcessing/AudioToTextSubtitlesProvider.php +++ b/lib/TaskProcessing/AudioToTextSubtitlesProvider.php @@ -17,6 +17,7 @@ use OCP\TaskProcessing\EShapeType; use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\Exception\UserFacingProcessingException; +use OCP\TaskProcessing\FileShaped; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\ShapeEnumValue; @@ -162,7 +163,7 @@ public function process(?string $userId, array $input, callable $reportProgress) try { $transcription = $this->openAiAPIService->transcribeFile($userId, $this->service, $inputFile, false, $model, $language, $format); - return ['output' => $transcription]; + return ['output' => new FileShaped(EShapeType::File, $transcription, $format)]; } catch (UserFacingProcessingException $e) { throw $e; } catch (\Throwable $e) { diff --git a/tests/unit/Service/FileFormatDetectorTest.php b/tests/unit/Service/FileFormatDetectorTest.php new file mode 100644 index 00000000..eb20dc03 --- /dev/null +++ b/tests/unit/Service/FileFormatDetectorTest.php @@ -0,0 +1,59 @@ + ["\xFF\xD8\xFF\xE0" . str_repeat("\0", 32), 'jpeg'], + 'png' => ["\x89PNG\r\n\x1A\n" . str_repeat("\0", 32), 'png'], + 'gif' => ['GIF89a' . str_repeat("\0", 32), 'gif'], + 'webp' => ["RIFF\x00\x00\x00\x00WEBPVP8 ", 'webp'], + 'unknown' => ['garbage', ''], + 'too short' => ['x', ''], + ]; + } + + /** + * @dataProvider imageProvider + */ + public function testGetImageExtension(string $data, string $expected): void { + $this->assertSame($expected, FileFormatDetector::getImageExtension($data)); + } + + public static function audioProvider(): array { + return [ + 'mp3 with ID3 tag' => ["ID3\x04\x00" . str_repeat("\0", 32), 'mp3'], + 'mp3 MPEG-1 Layer III' => ["\xFF\xFB\x90\x44" . str_repeat("\0", 32), 'mp3'], + 'mp3 MPEG-2 Layer III' => ["\xFF\xF3\x00\x00" . str_repeat("\0", 32), 'mp3'], + 'mp3 MPEG-2.5 Layer III' => ["\xFF\xE3\x00\x00" . str_repeat("\0", 32), 'mp3'], + 'adts aac MPEG-4' => ["\xFF\xF1\x50\x80" . str_repeat("\0", 32), 'aac'], + 'adts aac MPEG-2' => ["\xFF\xF9\x50\x80" . str_repeat("\0", 32), 'aac'], + 'reserved mpeg version' => ["\xFF\xE8" . str_repeat("\0", 32), ''], + 'wav' => ["RIFF\x24\x00\x00\x00WAVEfmt ", 'wav'], + 'ogg' => ["OggS\x00\x02", 'ogg'], + 'flac' => ["fLaC\x00\x00\x00\x22", 'flac'], + 'm4a' => ["\x00\x00\x00\x20ftypM4A \x00\x00\x00\x00", 'm4a'], + 'mp4' => ["\x00\x00\x00\x20ftypisom\x00\x00\x02\x00", 'mp4'], + 'unknown' => ['garbage', ''], + 'too short' => ['x', ''], + ]; + } + + /** + * @dataProvider audioProvider + */ + public function testGetAudioExtension(string $data, string $expected): void { + $this->assertSame($expected, FileFormatDetector::getAudioExtension($data)); + } +}