diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index afd7c36..0aad983 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -65,7 +65,7 @@ jobs: php -S localhost:8080 & - name: PHPUnit working-directory: apps/${{ env.APP_NAME }} - run: make test + run: make php-test mysql: runs-on: ubuntu-20.04 @@ -130,7 +130,7 @@ jobs: php -S localhost:8080 & - name: PHPUnit working-directory: apps/${{ env.APP_NAME }} - run: make test + run: make php-test pgsql: runs-on: ubuntu-20.04 @@ -197,4 +197,4 @@ jobs: php -S localhost:8080 & - name: PHPUnit working-directory: apps/${{ env.APP_NAME }} - run: make test + run: make php-test diff --git a/.vscode/launch.json b/.vscode/launch.json index d082e93..ed736e6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,7 +4,6 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ - { "name": "Listen for XDebug", "type": "php", @@ -53,6 +52,16 @@ "${file}" ], "port": 9229 - } + }, + { + "type": "chrome", + "request": "launch", + "name": "vuejs: chrome", + "url": "http://localhost/nextcloud/index.php/settings/admin/workflow", + "webRoot": "${workspaceFolder}/src", + "sourceMapPathOverrides": { + "webpack:///workflow_ocr/src/*": "${webRoot}/*" + } + }, ] -} \ No newline at end of file +} diff --git a/Makefile b/Makefile index c55c336..5885a82 100644 --- a/Makefile +++ b/Makefile @@ -183,16 +183,16 @@ appstore: ../$(app_name) \ .PHONY: test -test: composer +php-test: composer $(CURDIR)/vendor/phpunit/phpunit/phpunit -c phpunit.xml $(CURDIR)/vendor/phpunit/phpunit/phpunit -c phpunit.integration.xml -.PHONY: unittest -unittest: composer +.PHONY: php-unittest +php-unittest: composer $(CURDIR)/vendor/phpunit/phpunit/phpunit -c phpunit.xml -.PHONY: integrationtest -integrationtest: composer +.PHONY: php-integrationtest +php-integrationtest: composer $(CURDIR)/vendor/phpunit/phpunit/phpunit -c phpunit.integration.xml .PHONY: coverage-php @@ -223,3 +223,6 @@ lint-fix: composer npm-install .PHONY: js-test js-test: npm run test:unit + +.PHONY: test +test: php-test js-test \ No newline at end of file diff --git a/jest.config.js b/jest.config.js index 7ffb7a5..f311bdc 100644 --- a/jest.config.js +++ b/jest.config.js @@ -17,6 +17,7 @@ module.exports = { transformIgnorePatterns: [ "node_modules\/(?!(vue-material-design-icons)\/)", //"node_modules/(?!@babel)" - ] + ], + setupFilesAfterEnv: ['/src/test/setup-jest.js'] } \ No newline at end of file diff --git a/lib/Model/WorkflowSettings.php b/lib/Model/WorkflowSettings.php index eb31917..1f2c8bd 100644 --- a/lib/Model/WorkflowSettings.php +++ b/lib/Model/WorkflowSettings.php @@ -36,6 +36,12 @@ class WorkflowSettings { /** @var bool */ private $removeBackground = false; + /** @var array string */ + private $tagsToRemoveAfterOcr = []; + + /** @var array string */ + private $tagsToAddAfterOcr = []; + /** * @param string $json The serialized JSON string used in frontend as input for the Vue component */ @@ -57,6 +63,20 @@ public function getRemoveBackground(): bool { return $this->removeBackground; } + /** + * @return array + */ + public function getTagsToRemoveAfterOcr(): array { + return $this->tagsToRemoveAfterOcr; + } + + /** + * @return array + */ + public function getTagsToAddAfterOcr(): array { + return $this->tagsToAddAfterOcr; + } + /** * Checks if a new WorkflowSettings object can be constructed from the given JSON string * @param string $json The serialized JSON string used in frontend as input for the Vue component @@ -89,5 +109,11 @@ private function setJson(string $json = null) { if (array_key_exists('removeBackground', $data) && is_bool($data['removeBackground'])) { $this->removeBackground = $data['removeBackground']; } + if (array_key_exists('tagsToRemoveAfterOcr', $data) && is_array($data['tagsToRemoveAfterOcr'])) { + $this->tagsToRemoveAfterOcr = $data['tagsToRemoveAfterOcr']; + } + if (array_key_exists('tagsToAddAfterOcr', $data) && is_array($data['tagsToAddAfterOcr'])) { + $this->tagsToAddAfterOcr = $data['tagsToAddAfterOcr']; + } } } diff --git a/lib/Service/OcrService.php b/lib/Service/OcrService.php index 7807901..64790ff 100644 --- a/lib/Service/OcrService.php +++ b/lib/Service/OcrService.php @@ -30,6 +30,9 @@ use OCA\WorkflowOcr\OcrProcessors\IOcrProcessorFactory; use OCA\WorkflowOcr\OcrProcessors\OcrProcessorResult; use OCP\Files\File; +use OCP\SystemTag\ISystemTagObjectMapper; +use OCP\SystemTag\TagNotFoundException; +use Psr\Log\LoggerInterface; class OcrService implements IOcrService { /** @var IOcrProcessorFactory */ @@ -38,14 +41,47 @@ class OcrService implements IOcrService { /** @var IGlobalSettingsService */ private $globalSettingsService; - public function __construct(IOcrProcessorFactory $ocrProcessorFactory, IGlobalSettingsService $globalSettingsService) { + /** @var ISystemTagObjectMapper */ + private $systemTagObjectMapper; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(IOcrProcessorFactory $ocrProcessorFactory, IGlobalSettingsService $globalSettingsService, ISystemTagObjectMapper $systemTagObjectMapper, LoggerInterface $logger) { $this->ocrProcessorFactory = $ocrProcessorFactory; $this->globalSettingsService = $globalSettingsService; + $this->systemTagObjectMapper = $systemTagObjectMapper; + $this->logger = $logger; } /** @inheritdoc */ public function ocrFile(File $file, WorkflowSettings $settings) : OcrProcessorResult { $ocrProcessor = $this->ocrProcessorFactory->create($file->getMimeType()); - return $ocrProcessor->ocrFile($file, $settings, $this->globalSettingsService->getGlobalSettings()); + $result = $ocrProcessor->ocrFile($file, $settings, $this->globalSettingsService->getGlobalSettings()); + $this->processTagsAfterSuccessfulOcr($file, $settings); + return $result; + } + + private function processTagsAfterSuccessfulOcr(File $file, WorkflowSettings $settings) : void { + $objectType = 'files'; + $fileId = strval($file->getId()); + $tagsToRemove = $settings->getTagsToRemoveAfterOcr(); + $tagsToAdd = $settings->getTagsToAddAfterOcr(); + + foreach ($tagsToRemove as $tagToRemove) { + try { + $this->systemTagObjectMapper->unassignTags($fileId, $objectType, $tagToRemove); + } catch (TagNotFoundException $ex) { + $this->logger->warning("Cannot remove tag with id '$tagToRemove' because it was not found. Skipping."); + } + } + + foreach ($tagsToAdd as $tagToAdd) { + try { + $this->systemTagObjectMapper->assignTags($fileId, $objectType, $tagToAdd); + } catch (TagNotFoundException $ex) { + $this->logger->warning("Cannot add tag with id '$tagToAdd' because it was not found. Skipping."); + } + } } } diff --git a/package-lock.json b/package-lock.json index 909c87a..d1184e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,8 @@ "@nextcloud/webpack-vue-config": "^4.1.4", "@vue/cli": "^4.5.15", "@vue/cli-plugin-unit-jest": "^4.5.15", - "@vue/test-utils": "^1.3.0" + "@vue/test-utils": "^1.3.0", + "regenerator-runtime": "^0.13.9" }, "engines": { "node": "^14.0.0", @@ -8547,6 +8548,11 @@ "deprecated": "core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.", "hasInstallScript": true }, + "node_modules/babel-runtime/node_modules/regenerator-runtime": { + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" + }, "node_modules/babel-template": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-template/-/babel-template-6.26.0.tgz", @@ -25139,9 +25145,9 @@ } }, "node_modules/regenerator-runtime": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" + "version": "0.13.9", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", + "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" }, "node_modules/regenerator-transform": { "version": "0.14.5", @@ -25162,11 +25168,6 @@ "node": ">=6.9.0" } }, - "node_modules/regenerator-transform/node_modules/regenerator-runtime": { - "version": "0.13.9", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", - "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" - }, "node_modules/regex-not": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz", @@ -29752,11 +29753,6 @@ "node": ">=6.9.0" } }, - "node_modules/v-tooltip/node_modules/regenerator-runtime": { - "version": "0.13.9", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", - "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" - }, "node_modules/v8-compile-cache": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz", @@ -30209,11 +30205,6 @@ "node": ">=6.9.0" } }, - "node_modules/vue-resize/node_modules/regenerator-runtime": { - "version": "0.13.9", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", - "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" - }, "node_modules/vue-style-loader": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/vue-style-loader/-/vue-style-loader-4.1.3.tgz", @@ -38406,6 +38397,11 @@ "version": "2.6.12", "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.12.tgz", "integrity": "sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==" + }, + "regenerator-runtime": { + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" } } }, @@ -51727,9 +51723,9 @@ } }, "regenerator-runtime": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" + "version": "0.13.9", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", + "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" }, "regenerator-transform": { "version": "0.14.5", @@ -51746,11 +51742,6 @@ "requires": { "regenerator-runtime": "^0.13.4" } - }, - "regenerator-runtime": { - "version": "0.13.9", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", - "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" } } }, @@ -55390,11 +55381,6 @@ "requires": { "regenerator-runtime": "^0.13.4" } - }, - "regenerator-runtime": { - "version": "0.13.9", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", - "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" } } }, @@ -55745,11 +55731,6 @@ "requires": { "regenerator-runtime": "^0.13.4" } - }, - "regenerator-runtime": { - "version": "0.13.9", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", - "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" } } }, diff --git a/package.json b/package.json index 2dee2e7..996d749 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@nextcloud/webpack-vue-config": "^4.1.4", "@vue/cli": "^4.5.15", "@vue/cli-plugin-unit-jest": "^4.5.15", - "@vue/test-utils": "^1.3.0" + "@vue/test-utils": "^1.3.0", + "regenerator-runtime": "^0.13.9" } } diff --git a/src/components/GlobalSettings.vue b/src/components/GlobalSettings.vue index 7967f8d..5d471ac 100644 --- a/src/components/GlobalSettings.vue +++ b/src/components/GlobalSettings.vue @@ -22,8 +22,7 @@