|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Nextcloud - OCR |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Janis Koehr <[email protected]> |
|
10
|
|
|
* @copyright Janis Koehr 2017 |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace OCA\Ocr\Service; |
|
13
|
|
|
|
|
14
|
|
|
use OCA\Ocr\Db\OcrJobMapper; |
|
15
|
|
|
use OC\Files\View; |
|
16
|
|
|
use OCP\ILogger; |
|
17
|
|
|
use OCP\IL10N; |
|
18
|
|
|
use OCP\ITempManager; |
|
19
|
|
|
use OCA\Ocr\Db\OcrJob; |
|
20
|
|
|
use OCA\Ocr\Db\File; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class JobService |
|
24
|
|
|
* |
|
25
|
|
|
* @package OCA\Ocr\Service |
|
26
|
|
|
*/ |
|
27
|
|
|
class JobService { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var ILogger |
|
32
|
|
|
*/ |
|
33
|
|
|
private $logger; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @var QueueService |
|
38
|
|
|
*/ |
|
39
|
|
|
private $queueService; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @var OcrJobMapper |
|
44
|
|
|
*/ |
|
45
|
|
|
private $jobMapper; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @var View |
|
50
|
|
|
*/ |
|
51
|
|
|
private $view; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @var String |
|
56
|
|
|
*/ |
|
57
|
|
|
private $userId; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* @var IL10N |
|
62
|
|
|
*/ |
|
63
|
|
|
private $l10n; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* |
|
67
|
|
|
* @var FileService |
|
68
|
|
|
*/ |
|
69
|
|
|
private $fileService; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* |
|
73
|
|
|
* @var ITempManager |
|
74
|
|
|
*/ |
|
75
|
|
|
private $tempM; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* JobService constructor. |
|
79
|
|
|
* |
|
80
|
|
|
* @param IL10N $l10n |
|
81
|
|
|
* @param ILogger $logger |
|
82
|
|
|
* @param string $userId |
|
83
|
|
|
* @param View $view |
|
84
|
|
|
* @param QueueService $queueService |
|
85
|
|
|
* @param OcrJobMapper $mapper |
|
86
|
|
|
* @param FileService $fileService |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __construct(IL10N $l10n, ILogger $logger, $userId, View $view, ITempManager $tempManager, QueueService $queueService, OcrJobMapper $mapper, FileService $fileService) { |
|
89
|
|
|
$this->logger = $logger; |
|
90
|
|
|
$this->queueService = $queueService; |
|
91
|
|
|
$this->jobMapper = $mapper; |
|
92
|
|
|
$this->view = $view; |
|
93
|
|
|
$this->userId = $userId; |
|
94
|
|
|
$this->l10n = $l10n; |
|
95
|
|
|
$this->fileService = $fileService; |
|
96
|
|
|
$this->tempM = $tempManager; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Processes and prepares the files for OCR. |
|
101
|
|
|
* Sends the stuff to the client in order to OCR async. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string[] $languages |
|
104
|
|
|
* @param array $files |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function process($languages, $files) { |
|
108
|
|
|
try { |
|
109
|
|
|
$this->logger->debug ( 'Will now process files: ' . json_encode ( $files ) . ' with languages: ' . json_encode ( $languages ), [ |
|
110
|
|
|
'app' => 'ocr' |
|
111
|
|
|
] ); |
|
112
|
|
|
// Check if files and language not empty |
|
113
|
|
|
$noLang = $this->noLanguage ( $languages ); |
|
114
|
|
|
if (! empty ( $files ) && ($this->checkForAcceptedLanguages ( $languages ) || $noLang)) { |
|
115
|
|
|
// language part: |
|
116
|
|
|
if ($noLang) { |
|
117
|
|
|
$languages = []; |
|
118
|
|
|
} |
|
119
|
|
|
// file part: |
|
120
|
|
|
$fileInfo = $this->fileService->buildFileInfo ( $files ); |
|
121
|
|
|
foreach ( $fileInfo as $fInfo ) { |
|
|
|
|
|
|
122
|
|
|
// Check Shared |
|
123
|
|
|
$shared = $this->fileService->checkSharedWithInitiator ( $fInfo ); |
|
124
|
|
|
$target = $this->fileService->buildTarget ( $fInfo, $shared ); |
|
|
|
|
|
|
125
|
|
|
$source = $this->fileService->buildSource ( $fInfo, $shared ); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
// create a temp file for ocr processing purposes |
|
128
|
|
|
$tempFile = $this->tempM->getTemporaryFile (); |
|
129
|
|
|
|
|
130
|
|
|
// set the running type |
|
131
|
|
|
$fType = $this->fileService->getCorrectType ( $fInfo ); |
|
132
|
|
|
|
|
133
|
|
|
// TODO: create a security token |
|
134
|
|
|
// Create job object |
|
135
|
|
|
$job = new OcrJob ( 'PENDING', $source, $target, $tempFile, $fType, $this->userId, false ); |
|
136
|
|
|
|
|
137
|
|
|
// Init client and send task / job |
|
138
|
|
|
// Feed the worker |
|
139
|
|
|
$this->queueService->sendJob ( $job, $languages, \OC::$SERVERROOT ); |
|
140
|
|
|
} |
|
141
|
|
|
return 'PROCESSING'; |
|
142
|
|
|
} else { |
|
143
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Empty parameters passed.' ) ); |
|
144
|
|
|
} |
|
145
|
|
|
} catch ( Exception $e ) { |
|
|
|
|
|
|
146
|
|
|
$this->handleException ( $e ); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Delete an ocr job for a given id and userId. |
|
152
|
|
|
* |
|
153
|
|
|
* @param |
|
154
|
|
|
* $jobId |
|
155
|
|
|
* @param string $userId |
|
156
|
|
|
* @return OcrJob |
|
157
|
|
|
*/ |
|
158
|
|
|
public function deleteJob($jobId, $userId) { |
|
159
|
|
|
try { |
|
160
|
|
|
$job = $this->jobMapper->find ( $jobId ); |
|
161
|
|
|
if ($job->getUserId () !== $userId) { |
|
162
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Cannot delete. Wrong owner.' ) ); |
|
163
|
|
|
} else { |
|
164
|
|
|
$job = $this->jobMapper->delete ( $job ); |
|
165
|
|
|
} |
|
166
|
|
|
// TODO: return sanitized job for the personal settings page (no information about the source and target and such things. |
|
167
|
|
|
return $job; |
|
168
|
|
|
} catch ( Exception $e ) { |
|
|
|
|
|
|
169
|
|
|
if ($e instanceof DoesNotExistException) { |
|
|
|
|
|
|
170
|
|
|
$ex = new NotFoundException ( $this->l10n->t ( 'Cannot delete. Wrong ID.' ) ); |
|
171
|
|
|
$this->handleException ( $ex ); |
|
|
|
|
|
|
172
|
|
|
} else { |
|
173
|
|
|
$this->handleException ( $e ); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Gets all job objects for a specific user. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $userId |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getAllJobsForUser($userId) { |
|
185
|
|
|
try { |
|
186
|
|
|
$jobs = $this->jobMapper->findAll ( $userId ); |
|
187
|
|
|
$jobsNew = array (); |
|
188
|
|
|
for($x = 0; $x < count ( $jobs ); $x ++) { |
|
|
|
|
|
|
189
|
|
|
$newName = $this->fileService->removeFileExtension ( $jobs [$x] ); |
|
190
|
|
|
$jobs [$x]->setTarget ( $newName ); |
|
191
|
|
|
$jobs [$x]->setSource ( null ); |
|
192
|
|
|
$jobs [$x]->setTempFile ( null ); |
|
193
|
|
|
$jobs [$x]->setType ( null ); |
|
194
|
|
|
$jobs [$x]->setErrorDisplayed ( null ); |
|
195
|
|
|
array_push ( $jobsNew, $jobs [$x] ); |
|
196
|
|
|
} |
|
197
|
|
|
return $jobsNew; |
|
198
|
|
|
} catch ( Exception $e ) { |
|
|
|
|
|
|
199
|
|
|
$this->handleException ( $e ); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* TODO: Verify the security token and so on. |
|
205
|
|
|
* The function the worker will call in order to set the jobs status. |
|
206
|
|
|
* The worker should call it automatically after each processing step. |
|
207
|
|
|
* |
|
208
|
|
|
* @param integer $jobId |
|
209
|
|
|
* @param boolean $failed |
|
210
|
|
|
* @param string $errorMessage |
|
211
|
|
|
*/ |
|
212
|
|
|
public function jobFinished($jobId, $failed, $errorMessage) { |
|
213
|
|
|
try { |
|
214
|
|
|
$job = $this->jobMapper->find ( $jobId ); |
|
215
|
|
|
if (! $failed) { |
|
216
|
|
|
$job->setStatus ( 'PROCESSED' ); |
|
217
|
|
|
$this->jobMapper->update ( $job ); |
|
218
|
|
|
} else { |
|
219
|
|
|
$job->setStatus ( 'FAILED' ); |
|
220
|
|
|
// TODO: add error message log and so on |
|
221
|
|
|
$this->jobMapper->update ( $job ); |
|
222
|
|
|
$this->logger->error ( $errorMessage, [ |
|
223
|
|
|
'app' => 'ocr' |
|
224
|
|
|
] ); |
|
225
|
|
|
} |
|
226
|
|
|
} catch ( Exception $e ) { |
|
|
|
|
|
|
227
|
|
|
$this->handleException ( $e ); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Finishes all Processed files by copying them to the right path and deleteing the temp files. |
|
233
|
|
|
* Returns the number of processed files. |
|
234
|
|
|
* TODO: adjust behaviour for saving of the worker with the right file extension or not |
|
235
|
|
|
* |
|
236
|
|
|
* @return array |
|
237
|
|
|
*/ |
|
238
|
|
|
public function handleProcessed() { |
|
239
|
|
|
try { |
|
240
|
|
|
$this->logger->debug ( 'Check if files were processed by ocr and if so, put them to the right dirs.', [ |
|
241
|
|
|
'app' => 'ocr' |
|
242
|
|
|
] ); |
|
243
|
|
|
$processed = $this->jobMapper->findAllProcessed ( $this->userId ); |
|
244
|
|
|
foreach ( $processed as $job ) { |
|
245
|
|
|
if ($job->getType () === 'tess' && $this->fileService->fileExists ( $job->getTempFile () . '.txt' )) { |
|
246
|
|
|
// Save the tmp file with newname |
|
247
|
|
|
$this->view->file_put_contents ( $job->getTarget (), $this->fileService->getFileContents ( $job->getTempFile () . '.txt' ) ); // need .txt because tesseract saves it like this |
|
248
|
|
|
// Cleaning temp files |
|
249
|
|
|
$this->jobMapper->delete ( $job ); |
|
250
|
|
|
$this->fileService->execRemove ( $job->getTempFile () . '.txt' ); |
|
251
|
|
|
} elseif ($job->getType () === 'mypdf' && $this->fileService->fileExists ( $job->getTempFile () )) { |
|
252
|
|
|
// Save the tmp file with newname |
|
253
|
|
|
$this->view->file_put_contents ( $job->getTarget (), $this->fileService->getFileContents ( $job->getTempFile () ) ); // don't need to extend with .pdf / it uses the tmp file to save |
|
254
|
|
|
$this->jobMapper->delete ( $job ); |
|
255
|
|
|
$this->fileService->execRemove ( $job->getTempFile () ); |
|
256
|
|
|
} else { |
|
257
|
|
|
$job->setStatus ( 'FAILED' ); |
|
258
|
|
|
$this->jobMapper->update ( $job ); |
|
259
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Temp file does not exist.' ) ); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
return $processed; |
|
263
|
|
|
} catch ( Exception $e ) { |
|
|
|
|
|
|
264
|
|
|
$this->handleException ( $e ); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Handles all failed orders of ocr processing queue and returns the job objects. |
|
270
|
|
|
* |
|
271
|
|
|
* @return array |
|
272
|
|
|
*/ |
|
273
|
|
|
public function handleFailed() { |
|
274
|
|
|
try { |
|
275
|
|
|
$failed = $this->jobMapper->findAllFailed ( $this->userId ); |
|
276
|
|
|
foreach ( $failed as $job ) { |
|
277
|
|
|
// clean the tempfile |
|
278
|
|
|
$this->fileService->execRemove ( $job->getTempFile () ); |
|
279
|
|
|
// set error displayed |
|
280
|
|
|
$job->setErrorDisplayed ( true ); |
|
281
|
|
|
$this->jobMapper->update ( $job ); |
|
282
|
|
|
} |
|
283
|
|
|
$this->logger->debug ( 'Following jobs failed: ' . json_encode ( $failed ), [ |
|
284
|
|
|
'app' => 'ocr' |
|
285
|
|
|
] ); |
|
286
|
|
|
return $failed; |
|
287
|
|
|
} catch ( Exception $e ) { |
|
|
|
|
|
|
288
|
|
|
$this->handleException ( $e ); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Checks if the given languages are supported or not. |
|
294
|
|
|
* TODO: $this->config->getAppValue() |
|
295
|
|
|
* |
|
296
|
|
|
* @param string[] $languages |
|
297
|
|
|
* @return boolean |
|
298
|
|
|
*/ |
|
299
|
|
|
private function checkForAcceptedLanguages($languages) { |
|
300
|
|
|
if (count ( array_diff ( $languages, [ |
|
301
|
|
|
'deu', |
|
302
|
|
|
'eng' |
|
303
|
|
|
] ) ) === 0) { |
|
304
|
|
|
return true; |
|
305
|
|
|
} else { |
|
306
|
|
|
return false; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Checks if the process should be initiated without any language specified. |
|
312
|
|
|
* |
|
313
|
|
|
* @param string[] $languages |
|
314
|
|
|
* @return boolean |
|
315
|
|
|
*/ |
|
316
|
|
|
private function noLanguage($languages) { |
|
317
|
|
|
if (in_array ( 'none', $languages )) { |
|
318
|
|
|
return true; |
|
319
|
|
|
} else { |
|
320
|
|
|
return false; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
|
326
|
|
|
* |
|
327
|
|
|
* @param Exception $e |
|
328
|
|
|
* @throws Exception |
|
329
|
|
|
* @throws NotFoundException |
|
330
|
|
|
*/ |
|
331
|
|
View Code Duplication |
private function handleException($e) { |
|
332
|
|
|
$this->logger->logException ( $e, [ |
|
333
|
|
|
'app' => 'ocr', |
|
334
|
|
|
'message' => 'Exception during job service function processing' |
|
335
|
|
|
] ); |
|
336
|
|
|
if ($e instanceof NotFoundException) { |
|
337
|
|
|
throw new NotFoundException ( $e->getMessage () ); |
|
338
|
|
|
} else { |
|
339
|
|
|
throw $e; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.