Passed
Push — master ( f5c4f5...fd473f )
by Joas
18:20 queued 13s
created

TranscriptionJob::run()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 42
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 36
nc 9
nop 1
dl 0
loc 42
rs 9.344
c 3
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2023 Marcel Klehr <[email protected]>
7
 *
8
 * @author Marcel Klehr <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
27
namespace OC\SpeechToText;
28
29
use OC\User\NoUserException;
30
use OCP\AppFramework\Utility\ITimeFactory;
31
use OCP\BackgroundJob\QueuedJob;
32
use OCP\EventDispatcher\IEventDispatcher;
33
use OCP\Files\File;
34
use OCP\Files\IRootFolder;
35
use OCP\Files\NotFoundException;
36
use OCP\Files\NotPermittedException;
37
use OCP\PreConditionNotMetException;
38
use OCP\SpeechToText\Events\TranscriptionFailedEvent;
39
use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent;
40
use OCP\SpeechToText\ISpeechToTextManager;
41
use Psr\Log\LoggerInterface;
42
43
class TranscriptionJob extends QueuedJob {
44
	public function __construct(
45
		ITimeFactory $timeFactory,
46
		private ISpeechToTextManager $speechToTextManager,
47
		private IEventDispatcher $eventDispatcher,
48
		private IRootFolder $rootFolder,
49
		private LoggerInterface $logger,
50
	) {
51
		parent::__construct($timeFactory);
52
	}
53
54
55
	/**
56
	 * @inheritDoc
57
	 */
58
	protected function run($argument) {
59
		$fileId = $argument['fileId'];
60
		$owner = $argument['owner'];
61
		$userId = $argument['userId'];
62
		$appId = $argument['appId'];
63
		$file = null;
64
		try {
65
			\OC_Util::setupFS($owner);
66
			$userFolder = $this->rootFolder->getUserFolder($owner);
67
			$file = current($userFolder->getById($fileId));
68
			if (!($file instanceof File)) {
69
				$this->logger->warning('Transcription of file ' . $fileId . ' failed. The file could not be found');
70
				$this->eventDispatcher->dispatchTyped(
71
					new TranscriptionFailedEvent(
72
						$fileId,
73
						null,
74
						'File not found',
75
						$userId,
76
						$appId,
77
					)
78
				);
79
				return;
80
			}
81
			$result = $this->speechToTextManager->transcribeFile($file);
82
			$this->eventDispatcher->dispatchTyped(
83
				new TranscriptionSuccessfulEvent(
84
					$fileId,
85
					$file,
86
					$result,
87
					$userId,
88
					$appId,
89
				)
90
			);
91
		} catch (PreConditionNotMetException|\RuntimeException|\InvalidArgumentException|NotFoundException|NotPermittedException|NoUserException $e) {
92
			$this->logger->warning('Transcription of file ' . $fileId . ' failed', ['exception' => $e]);
93
			$this->eventDispatcher->dispatchTyped(
94
				new TranscriptionFailedEvent(
95
					$fileId,
96
					$file,
97
					$e->getMessage(),
98
					$userId,
99
					$appId,
100
				)
101
			);
102
		}
103
	}
104
}
105