Skip to content

Commit

Permalink
Added a fix for #122 and changed output format of image processing to…
Browse files Browse the repository at this point in the history
… pdf
  • Loading branch information
Janis Koehr committed Jan 28, 2018
1 parent 9eb18dc commit 24f5a5f
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 106 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

* Janis Koehr: <janiskoehr@icloud.com> __Main Author__
* stweil
* comradekingu
* comradekingu
* mathiasconradt
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#Changelog
nextcloud-ocr (3.2.0)
* **Feature**: Now the result of a processed image will always be a pdf file, not a txt file anymore.
* **Bugfix**: The ENOENT error in the worker should be fixed now.
nextcloud-ocr (3.1.0-beta.1)
* **Feature**: Now files can be replaced by selecting a checkbox.
nextcloud-ocr (3.0.0-beta.8)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The app uses a docker container with tesseract-ocr, OCRmyPDF and communicates ov
## Prerequisites, Requirements and Dependencies
The OCR app has some prerequisites:
- **[Nextcloud 12](https://nextcloud.com/)**. For older versions take an older major version of this app.
- **Linux** server as environment. (tested with Debian 8, Raspbian and Ubuntu 14.04 (Trusty))
- **Linux** server as environment. (tested with Debian 8 and Ubuntu 14.04 (Trusty)) **currently not compatible to ARM processors like raspberry**
- **Docker** is used for processing files. tesseract-ocr and OCRmyPDF reside in a docker container.
- **php-redis** is used for the communication and has to be a part of your php.

Expand Down
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Please read the related topics in the [wiki](https://github.com/janis91/ocr/wiki
The software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
]]></description>
<version>3.1.0-beta.1</version>
<version>3.2.0</version>
<licence>agpl</licence>
<author>Janis Koehr</author>
<namespace>Ocr</namespace>
Expand Down
2 changes: 1 addition & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function (IAppContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
return new RedisService($c->query('OcrJobMapper'), $c->query('FileUtil'), $c->query('RedisUtil'),
$server->getL10N('ocr'), $server->getLogger());
$server->getL10N('ocr'), $server->getLogger(), $server->getTempManager());
});
/**
* Register the Job Service
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private function buildTargetForShared(File $fileInfo, $replace) {
}
} else {
// IMAGES:
return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.txt');
return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.pdf');
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ private function buildTargetNotForShared(File $fileInfo, $replace) {
}
} else {
// IMAGES:
return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.txt');
return $this->fileUtil->buildNotExistingFilename($filePath, $fileName . '.pdf');
}
}

Expand Down
24 changes: 10 additions & 14 deletions lib/Service/JobService.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function process($languages, $files, $replace) {
// set the running type
$fType = $this->fileService->getCorrectType($fInfo);
// create a temp file for ocr processing purposes
$tempFile = $this->getTempFile($fType);
$tempFile = $this->getTempFile();
// Create job object
$job = new OcrJob(OcrConstants::STATUS_PENDING, $source, $target, $tempFile, $fType, $this->userId,
false, $fInfo->getName(), null, $replace);
Expand Down Expand Up @@ -274,11 +274,11 @@ public function handleProcessed() {
$this->logger->debug('Check if files were processed by ocr and if so, put them to the right dirs.');
$processed = $this->jobMapper->findAllProcessed($this->userId);
foreach ($processed as $job) {
if ($this->fileUtil->fileExists($job->getTempFile())) {
if ($this->fileUtil->fileExists($this->tempM->getTempBaseDir().'/'.$job->getTempFile().'.pdf')) {
// Save the tmp file with newname
$this->pullResult($job);
$this->jobMapper->delete($job);
$this->fileUtil->execRemove($job->getTempFile());
$this->fileUtil->execRemove($this->tempM->getTempBaseDir().'/'.$job->getTempFile().'.pdf');
} else {
$job->setStatus(OcrConstants::STATUS_FAILED);
$job->setErrorLog('Temp file does not exist.');
Expand All @@ -302,7 +302,7 @@ public function handleFailed() {
$failed = $this->jobMapper->findAllFailed($this->userId);
foreach ($failed as $job) {
// clean the tempfile
$this->fileUtil->execRemove($job->getTempFile());
$this->fileUtil->execRemove($this->tempM->getTempBaseDir().'/'.$job->getTempFile().'.pdf');
// set error displayed
$job->setErrorDisplayed(true);
$this->jobMapper->update($job);
Expand All @@ -326,7 +326,7 @@ private function pullResult($job) {
if ($job->getReplace()) {
$this->view->unlink(str_replace($this->userId . '/files', '', $job->getSource()));
}
$result = $this->view->file_put_contents($job->getTarget(), $this->fileUtil->getFileContents($job->getTempFile()));
$result = $this->view->file_put_contents($job->getTarget(), $this->fileUtil->getFileContents($this->tempM->getTempBaseDir().'/'.$job->getTempFile().'.pdf'));
if (!$result) {
throw new NotFoundException($this->l10n->t('OCR could not put processed file to the right target folder. If you selected the replace option, you can restore the file by using the trash bin.'));
}
Expand Down Expand Up @@ -360,21 +360,17 @@ private function jobFinished($jobId, $error, $log) {
/**
* Gives a temp file name back depending on the type of the OCR.
* Later in the worker this file is used as an output.
*
* @param integer $type
*
* @return string
*/
private function getTempFile($type) {
if ($type === OcrConstants::TESSERACT) {
private function getTempFile() {
$fileName = $this->phpUtil->tempnamWrapper($this->tempM->getTempBaseDir(), OcrConstants::TEMPFILE_PREFIX);
$this->phpUtil->unlinkWrapper($fileName);
$fileNameWithPostfix = $fileName . '.txt';
$fileNameWithPostfix = $fileName . '.pdf';
$this->phpUtil->touchWrapper($fileNameWithPostfix);
$this->phpUtil->chmodWrapper($fileNameWithPostfix, 0600);
return $fileNameWithPostfix;
} else {
return $this->phpUtil->tempnamWrapper($this->tempM->getTempBaseDir(), OcrConstants::TEMPFILE_PREFIX);
}
$fileName = basename($fileName);
return $fileName;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions lib/Service/RedisService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Ocr\Db\OcrJob;
use OCA\Ocr\Db\OcrJobMapper;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\ILogger;
use OCA\Ocr\Constants\OcrConstants;
use OCA\Ocr\Util\FileUtil;
Expand Down Expand Up @@ -57,6 +58,12 @@ class RedisService {
* @var IL10N
*/
private $l10n;

/**
*
* @var ITempManager
*/
private $tempM;

/**
* QueueService constructor.
Expand All @@ -68,12 +75,13 @@ class RedisService {
* @param ILogger $logger
*/
public function __construct(OcrJobMapper $mapper, FileUtil $fileUtil, RedisUtil $redisUtil, IL10N $l10n,
ILogger $logger) {
ILogger $logger, ITempManager $tempManager) {
$this->mapper = $mapper;
$this->fileUtil = $fileUtil;
$this->logger = $logger;
$this->l10n = $l10n;
$this->redisUtil = $redisUtil;
$this->tempM = $tempManager;
}

/**
Expand All @@ -99,7 +107,7 @@ public function sendJob($job, $languages) {
throw new NotFoundException($this->l10n->t('Could not add files to the Redis OCR processing queue.'));
}
} catch (Exception $e) {
$this->fileUtil->execRemove($job->getTempFile());
$this->fileUtil->execRemove($this->tempM->getTempBaseDir().'/'.$job->getTempFile().'.pdf');
$job->setStatus(OcrConstants::STATUS_FAILED);
$job->setErrorLog($e->getMessage());
$this->mapper->update($job);
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Service/FileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public function testBuildTargetForSharedForImage() {
->will($this->returnValue($share));
$this->fileUtilMock->expects($this->once())
->method('buildNotExistingFilename')
->with('test/path/to', 'file.txt')
->will($this->returnValue('/test/path/to/file.txt'));
->with('test/path/to', 'file.pdf')
->will($this->returnValue('/test/path/to/file.pdf'));
$result = $this->cut->buildTarget($this->fileInfoSharedPng, true, false);
$this->assertEquals('/test/path/to/file.txt', $result);
$this->assertEquals('/test/path/to/file.pdf', $result);
}

public function testBuildTargetForSharedForPdf() {
Expand Down Expand Up @@ -130,10 +130,10 @@ public function testBuildTargetForSharedForPdfAndReplaceOnRoot() {
public function testBuildTargetNotForSharedForImage() {
$this->fileUtilMock->expects($this->once())
->method('buildNotExistingFilename')
->with('test/path/to', 'file.txt')
->will($this->returnValue('/test/path/to/file.txt'));
->with('test/path/to', 'file.pdf')
->will($this->returnValue('/test/path/to/file.pdf'));
$result = $this->cut->buildTarget($this->fileInfoNotSharedPng, false, false);
$this->assertEquals('/test/path/to/file.txt', $result);
$this->assertEquals('/test/path/to/file.pdf', $result);
}

public function testBuildTargetNotForSharedForPdf() {
Expand Down
Loading

0 comments on commit 24f5a5f

Please sign in to comment.