aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2023-04-13 12:12:07 +0200
committerMarcel Klehr <mklehr@gmx.net>2023-04-13 12:12:07 +0200
commita8d3fff648c4c6f5be3e37c09f9168e5b7c0e8f8 (patch)
tree6115b6748d00997155016d708879e796bfe21344
parent176f1af26a6bf7c62cadd04448b73839296653c0 (diff)
downloadnextcloud-server-a8d3fff648c4c6f5be3e37c09f9168e5b7c0e8f8.tar.gz
nextcloud-server-a8d3fff648c4c6f5be3e37c09f9168e5b7c0e8f8.zip
Split TranscriptionFinishedEvent into Successful and Failed events
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
-rw-r--r--lib/private/SpeechToText/SpeechToTextManager.php5
-rw-r--r--lib/private/SpeechToText/TranscriptionJob.php28
-rw-r--r--lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php (renamed from lib/public/SpeechToText/Events/TranscriptionFinishedEvent.php)32
-rw-r--r--lib/public/SpeechToText/Events/TranscriptionFailedEvent.php24
-rw-r--r--lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php26
5 files changed, 74 insertions, 41 deletions
diff --git a/lib/private/SpeechToText/SpeechToTextManager.php b/lib/private/SpeechToText/SpeechToTextManager.php
index a734f6bb379..c4f89ed43c7 100644
--- a/lib/private/SpeechToText/SpeechToTextManager.php
+++ b/lib/private/SpeechToText/SpeechToTextManager.php
@@ -33,7 +33,6 @@ 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;
use OCP\PreConditionNotMetException;
@@ -96,7 +95,7 @@ class SpeechToTextManager implements ISpeechToTextManager {
}
try {
$this->jobList->add(TranscriptionJob::class, ['fileId' => $file->getId()]);
- }catch(NotFoundException|InvalidPathException $e) {
+ } catch (NotFoundException|InvalidPathException $e) {
throw new InvalidArgumentException('Invalid file provided for file transcription: ' . $e->getMessage());
}
}
@@ -109,7 +108,7 @@ class SpeechToTextManager implements ISpeechToTextManager {
try {
return $provider->transcribeFile($file);
- }catch (\Throwable $e) {
+ } 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 296a713593b..775a2a7fadc 100644
--- a/lib/private/SpeechToText/TranscriptionJob.php
+++ b/lib/private/SpeechToText/TranscriptionJob.php
@@ -5,9 +5,11 @@ namespace OC\SpeechToText;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\PreConditionNotMetException;
-use OCP\SpeechToText\Events\TranscriptionFinishedEvent;
+use OCP\SpeechToText\Events\TranscriptionFailedEvent;
+use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent;
use OCP\SpeechToText\ISpeechToTextManager;
class TranscriptionJob extends QueuedJob {
@@ -25,24 +27,30 @@ class TranscriptionJob extends QueuedJob {
* @inheritDoc
*/
protected function run($argument) {
+ $fileId = $argument['fileId'];
try {
- $file = $this->rootFolder->getById($argument['fileId']);
+ $file = current($this->rootFolder->getById($fileId));
+ if (!($file instanceof File)) {
+ $this->eventDispatcher->dispatchTyped(
+ new TranscriptionFailedEvent(
+ $fileId,
+ 'File not found',
+ )
+ );
+ return;
+ }
$result = $this->speechToTextManager->transcribeFile($file);
$this->eventDispatcher->dispatchTyped(
- new TranscriptionFinishedEvent(
- true,
+ new TranscriptionSuccessfulEvent(
+ $fileId,
$result,
- '',
- $argument['context']
)
);
} catch (PreConditionNotMetException|\RuntimeException|\InvalidArgumentException $e) {
$this->eventDispatcher->dispatchTyped(
- new TranscriptionFinishedEvent(
- false,
- '',
+ new TranscriptionFailedEvent(
+ $fileId,
$e->getMessage(),
- $argument['context']
)
);
}
diff --git a/lib/public/SpeechToText/Events/TranscriptionFinishedEvent.php b/lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php
index 245ccd413e9..5b00315f74c 100644
--- a/lib/public/SpeechToText/Events/TranscriptionFinishedEvent.php
+++ b/lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php
@@ -30,15 +30,12 @@ use OCP\EventDispatcher\Event;
/**
* @since 27.0.0
*/
-class TranscriptionFinishedEvent extends Event {
+abstract class AbstractTranscriptionEvent extends Event {
/**
* @since 27.0.0
*/
public function __construct(
- private bool $successful,
- private string $transcription,
- private string $errorMessage,
- private array $context
+ private int $fileIdId
) {
parent::__construct();
}
@@ -46,28 +43,7 @@ class TranscriptionFinishedEvent extends Event {
/**
* @since 27.0.0
*/
- public function getContext(): array {
- return $this->context;
- }
-
- /**
- * @since 27.0.0
- */
- public function isSuccessful(): bool {
- return $this->successful;
- }
-
- /**
- * @since 27.0.0
- */
- public function getErrorMessage(): string {
- return $this->errorMessage;
- }
-
- /**
- * @since 27.0.0
- */
- public function getTranscription(): string {
- return $this->transcription;
+ public function getFileId(): int {
+ return $this->fileIdId;
}
}
diff --git a/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php b/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php
new file mode 100644
index 00000000000..3a490995c10
--- /dev/null
+++ b/lib/public/SpeechToText/Events/TranscriptionFailedEvent.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace OCP\SpeechToText\Events;
+
+class TranscriptionFailedEvent extends AbstractTranscriptionEvent {
+
+ /**
+ * @since 27.0.0
+ */
+ public function __construct(
+ int $fileId,
+ private string $errorMessage
+ ) {
+ parent::__construct($fileId);
+ }
+
+ /**
+ * @since 27.0.0
+ * @return string The error message
+ */
+ public function getErrorMessage(): string {
+ return $this->errorMessage;
+ }
+}
diff --git a/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php b/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php
new file mode 100644
index 00000000000..63644b98f6d
--- /dev/null
+++ b/lib/public/SpeechToText/Events/TranscriptionSuccessfulEvent.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace OCP\SpeechToText\Events;
+
+use OCP\Files\File;
+
+class TranscriptionSuccessfulEvent extends AbstractTranscriptionEvent {
+
+ /**
+ * @since 27.0.0
+ */
+ public function __construct(
+ int $fileId,
+ private string $transcript
+ ) {
+ parent::__construct($fileId);
+ }
+
+ /**
+ * @since 27.0.0
+ * @return string The transcript of the media file
+ */
+ public function getTranscript(): string {
+ return $this->transcript;
+ }
+}