|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OCA\Testing\TaskProcessing; |
| 11 | + |
| 12 | +use OCA\Testing\AppInfo\Application; |
| 13 | +use OCP\AppFramework\Services\IAppConfig; |
| 14 | +use OCP\Files\File; |
| 15 | +use OCP\TaskProcessing\Exception\ProcessingException; |
| 16 | +use OCP\TaskProcessing\ISynchronousProvider; |
| 17 | +use OCP\TaskProcessing\TaskTypes\ImageToTextOpticalCharacterRecognition; |
| 18 | +use RuntimeException; |
| 19 | + |
| 20 | +class FakeOcrProvider implements ISynchronousProvider { |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + protected IAppConfig $appConfig, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function getId(): string { |
| 28 | + return Application::APP_ID . '-image2text-ocr'; |
| 29 | + } |
| 30 | + |
| 31 | + public function getName(): string { |
| 32 | + return 'Fake OCR task processing provider'; |
| 33 | + } |
| 34 | + |
| 35 | + public function getTaskTypeId(): string { |
| 36 | + return ImageToTextOpticalCharacterRecognition::ID; |
| 37 | + } |
| 38 | + |
| 39 | + public function getExpectedRuntime(): int { |
| 40 | + return 1; |
| 41 | + } |
| 42 | + |
| 43 | + public function getInputShapeEnumValues(): array { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + public function getInputShapeDefaults(): array { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + public function getOptionalInputShape(): array { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + public function getOptionalInputShapeEnumValues(): array { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + public function getOptionalInputShapeDefaults(): array { |
| 60 | + return []; |
| 61 | + } |
| 62 | + |
| 63 | + public function getOutputShapeEnumValues(): array { |
| 64 | + return []; |
| 65 | + } |
| 66 | + |
| 67 | + public function getOptionalOutputShape(): array { |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + public function getOptionalOutputShapeEnumValues(): array { |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + public function process(?string $userId, array $input, callable $reportProgress): array { |
| 76 | + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { |
| 77 | + throw new ProcessingException('Failing as set by AppConfig'); |
| 78 | + } |
| 79 | + |
| 80 | + if (!isset($input['input']) || !is_array($input['input'])) { |
| 81 | + throw new RuntimeException('Invalid input'); |
| 82 | + } |
| 83 | + $outputs = []; |
| 84 | + foreach ($input['input'] as $i => $inputImage) { |
| 85 | + if (!($inputImage instanceof File) || !$inputImage->isReadable()) { |
| 86 | + throw new RuntimeException('Invalid input images'); |
| 87 | + } |
| 88 | + $outputs[] = '[' . $i . '] This is a fake OCR result.'; |
| 89 | + } |
| 90 | + |
| 91 | + return ['output' => $outputs]; |
| 92 | + } |
| 93 | +} |
0 commit comments