Skip to content

Commit

Permalink
Implement feature #88 (#143) (#147)
Browse files Browse the repository at this point in the history
* Implement assign tags / remove tags after OCR has finished

Closing #88

* Update tests/Unit/Service/OcrServiceTest.php

Co-authored-by: Manuel Bentele <development@manuel-bentele.de>

Co-authored-by: Manuel Bentele <development@manuel-bentele.de>

Co-authored-by: Manuel Bentele <development@manuel-bentele.de>
  • Loading branch information
R0Wi and bahnwaerter authored Sep 7, 2022
1 parent 2b4dcc2 commit d89d43a
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 69 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -197,4 +197,4 @@ jobs:
php -S localhost:8080 &
- name: PHPUnit
working-directory: apps/${{ env.APP_NAME }}
run: make test
run: make php-test
15 changes: 12 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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}/*"
}
},
]
}
}
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
transformIgnorePatterns: [
"node_modules\/(?!(vue-material-design-icons)\/)",
//"node_modules/(?!@babel)"
]
],
setupFilesAfterEnv: ['<rootDir>/src/test/setup-jest.js']
}

26 changes: 26 additions & 0 deletions lib/Model/WorkflowSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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'];
}
}
}
40 changes: 38 additions & 2 deletions lib/Service/OcrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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.");
}
}
}
}
55 changes: 18 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 1 addition & 2 deletions src/components/GlobalSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

<template>
<div>
<SettingsSection
title="Workflow OCR"
<SettingsSection title="Workflow OCR"
:description="description"
doc-url="https://github.com/R0Wi/workflow_ocr/blob/master/README.md#global-settings">
<div class="div-table-row">
Expand Down
Loading

0 comments on commit d89d43a

Please sign in to comment.