TranscriptionJob.php 3.0 KB

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