aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2023-04-13 11:57:23 +0200
committerMarcel Klehr <mklehr@gmx.net>2023-04-13 11:57:23 +0200
commit176f1af26a6bf7c62cadd04448b73839296653c0 (patch)
treef6a41de996752bead800a6bcec5a6cb2245e1fa4
parentef7ce88cee47ec7d05c43e55c166f8488667dc5d (diff)
downloadnextcloud-server-176f1af26a6bf7c62cadd04448b73839296653c0.tar.gz
nextcloud-server-176f1af26a6bf7c62cadd04448b73839296653c0.zip
ISpeechToTextManager: Take File as input, drop $context
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
-rw-r--r--lib/private/SpeechToText/SpeechToTextManager.php27
-rw-r--r--lib/private/SpeechToText/TranscriptionJob.php5
-rw-r--r--lib/public/SpeechToText/ISpeechToTextManager.php7
3 files changed, 19 insertions, 20 deletions
diff --git a/lib/private/SpeechToText/SpeechToTextManager.php b/lib/private/SpeechToText/SpeechToTextManager.php
index 7b50fa385c9..a734f6bb379 100644
--- a/lib/private/SpeechToText/SpeechToTextManager.php
+++ b/lib/private/SpeechToText/SpeechToTextManager.php
@@ -32,6 +32,7 @@ use InvalidArgumentException;
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\BackgroundJob\IJobList;
use OCP\Files\File;
+use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IServerContainer;
@@ -52,7 +53,6 @@ class SpeechToTextManager implements ISpeechToTextManager {
private Coordinator $coordinator,
private LoggerInterface $logger,
private IJobList $jobList,
- private IRootFolder $rootFolder,
) {
}
@@ -90,35 +90,28 @@ class SpeechToTextManager implements ISpeechToTextManager {
return !empty($context->getTranslationProviders());
}
- public function scheduleFileTranscription(string $path, array $context): void {
+ public function scheduleFileTranscription(File $file): void {
if (!$this->hasProviders()) {
throw new PreConditionNotMetException('No SpeechToText providers have been registered');
}
try {
- $node = $this->rootFolder->get($path);
- } catch (NotFoundException $e) {
- throw new InvalidArgumentException('File does not exist: ' . $path);
+ $this->jobList->add(TranscriptionJob::class, ['fileId' => $file->getId()]);
+ }catch(NotFoundException|InvalidPathException $e) {
+ throw new InvalidArgumentException('Invalid file provided for file transcription: ' . $e->getMessage());
}
- if (!($node instanceof File)) {
- throw new InvalidArgumentException('Path does not resolve to a file');
- }
- $this->jobList->add(TranscriptionJob::class, [ 'path' => $path, 'context' => $context]);
}
- public function transcribeFile(string $path): string {
+ public function transcribeFile(File $file): string {
$provider = current($this->getProviders());
if (!$provider) {
throw new PreConditionNotMetException('No SpeechToText providers have been registered');
}
try {
- $node = $this->rootFolder->get($path);
- if (!($node instanceof File)) {
- throw new InvalidArgumentException('Path does not resolve to a file');
- }
- return $provider->transcribeFile($node);
- } catch (NotFoundException $e) {
- throw new InvalidArgumentException('File does not exist: ' . $path);
+ return $provider->transcribeFile($file);
+ }catch (\Throwable $e) {
+ $this->logger->info('SpeechToText transcription failed', ['exception' => $e]);
+ throw new \RuntimeException('SpeechToText transcription failed: ' . $e->getMessage());
}
}
}
diff --git a/lib/private/SpeechToText/TranscriptionJob.php b/lib/private/SpeechToText/TranscriptionJob.php
index d2cff468024..296a713593b 100644
--- a/lib/private/SpeechToText/TranscriptionJob.php
+++ b/lib/private/SpeechToText/TranscriptionJob.php
@@ -5,6 +5,7 @@ namespace OC\SpeechToText;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\IRootFolder;
use OCP\PreConditionNotMetException;
use OCP\SpeechToText\Events\TranscriptionFinishedEvent;
use OCP\SpeechToText\ISpeechToTextManager;
@@ -14,6 +15,7 @@ class TranscriptionJob extends QueuedJob {
ITimeFactory $timeFactory,
private ISpeechToTextManager $speechToTextManager,
private IEventDispatcher $eventDispatcher,
+ private IRootFolder $rootFolder,
) {
parent::__construct($timeFactory);
}
@@ -24,7 +26,8 @@ class TranscriptionJob extends QueuedJob {
*/
protected function run($argument) {
try {
- $result = $this->speechToTextManager->transcribeFile($argument['path']);
+ $file = $this->rootFolder->getById($argument['fileId']);
+ $result = $this->speechToTextManager->transcribeFile($file);
$this->eventDispatcher->dispatchTyped(
new TranscriptionFinishedEvent(
true,
diff --git a/lib/public/SpeechToText/ISpeechToTextManager.php b/lib/public/SpeechToText/ISpeechToTextManager.php
index 7bcd60bce8c..430a6cbc450 100644
--- a/lib/public/SpeechToText/ISpeechToTextManager.php
+++ b/lib/public/SpeechToText/ISpeechToTextManager.php
@@ -27,6 +27,7 @@ declare(strict_types=1);
namespace OCP\SpeechToText;
use InvalidArgumentException;
+use OCP\Files\File;
use OCP\PreConditionNotMetException;
use RuntimeException;
@@ -50,13 +51,15 @@ interface ISpeechToTextManager {
* @throws InvalidArgumentException If the file could not be found or is not of a supported type
* @throws RuntimeException If the transcription failed for other reasons
*/
- public function scheduleFileTranscription(string $path, array $context): void;
+ public function scheduleFileTranscription(File $file): void;
/**
* @since 27.0.0
+ * @param File $file The media file to transcribe
+ * @returns string The transcription of the the passed media file
* @throws PreConditionNotMetException If no provider was registered but this method was still called
* @throws InvalidArgumentException If the file could not be found or is not of a supported type
* @throws RuntimeException If the transcription failed for other reasons
*/
- public function transcribeFile(string $path) : string;
+ public function transcribeFile(File $file) : string;
}