AbstractTranscriptionEvent: Add File param

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
This commit is contained in:
Marcel Klehr 2023-04-17 14:45:18 +02:00
parent ad66180c34
commit 71523b9816
4 changed files with 24 additions and 3 deletions

View file

@ -35,6 +35,7 @@ use OCP\PreConditionNotMetException;
use OCP\SpeechToText\Events\TranscriptionFailedEvent;
use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent;
use OCP\SpeechToText\ISpeechToTextManager;
use Psr\Log\LoggerInterface;
class TranscriptionJob extends QueuedJob {
public function __construct(
@ -42,6 +43,7 @@ class TranscriptionJob extends QueuedJob {
private ISpeechToTextManager $speechToTextManager,
private IEventDispatcher $eventDispatcher,
private IRootFolder $rootFolder,
private LoggerInterface $logger,
) {
parent::__construct($timeFactory);
}
@ -52,12 +54,15 @@ class TranscriptionJob extends QueuedJob {
*/
protected function run($argument) {
$fileId = $argument['fileId'];
$file = null;
try {
$file = current($this->rootFolder->getById($fileId));
if (!($file instanceof File)) {
$this->logger->warning('Transcription of file ' . $fileId . ' failed. The file could not be found');
$this->eventDispatcher->dispatchTyped(
new TranscriptionFailedEvent(
$fileId,
null,
'File not found',
)
);
@ -67,13 +72,16 @@ class TranscriptionJob extends QueuedJob {
$this->eventDispatcher->dispatchTyped(
new TranscriptionSuccessfulEvent(
$fileId,
$file,
$result,
)
);
} catch (PreConditionNotMetException|\RuntimeException|\InvalidArgumentException $e) {
$this->logger->warning('Transcription of file ' . $fileId . ' failed', ['exception' => $e]);
$this->eventDispatcher->dispatchTyped(
new TranscriptionFailedEvent(
$fileId,
$file,
$e->getMessage(),
)
);

View file

@ -26,6 +26,7 @@ declare(strict_types=1);
namespace OCP\SpeechToText\Events;
use OCP\EventDispatcher\Event;
use OCP\Files\File;
/**
* @since 27.0.0
@ -35,7 +36,8 @@ abstract class AbstractTranscriptionEvent extends Event {
* @since 27.0.0
*/
public function __construct(
private int $fileIdId
private int $fileIdId,
private ?File $file,
) {
parent::__construct();
}
@ -46,4 +48,11 @@ abstract class AbstractTranscriptionEvent extends Event {
public function getFileId(): int {
return $this->fileIdId;
}
/**
* @since 27.0.0
*/
public function getFile(): ?File {
return $this->file;
}
}

View file

@ -26,6 +26,8 @@ declare(strict_types=1);
namespace OCP\SpeechToText\Events;
use OCP\Files\File;
class TranscriptionFailedEvent extends AbstractTranscriptionEvent {
/**
@ -33,9 +35,10 @@ class TranscriptionFailedEvent extends AbstractTranscriptionEvent {
*/
public function __construct(
int $fileId,
?File $file,
private string $errorMessage
) {
parent::__construct($fileId);
parent::__construct($fileId, $file);
}
/**

View file

@ -35,9 +35,10 @@ class TranscriptionSuccessfulEvent extends AbstractTranscriptionEvent {
*/
public function __construct(
int $fileId,
?File $file,
private string $transcript
) {
parent::__construct($fileId);
parent::__construct($fileId, $file);
}
/**