@@ -17,7 +17,6 @@ |
||
| 17 | 17 | use OCP\IL10N; |
| 18 | 18 | use OCP\ITempManager; |
| 19 | 19 | use OCA\Ocr\Db\OcrJob; |
| 20 | -use OCA\Ocr\Db\File; |
|
| 21 | 20 | |
| 22 | 21 | /** |
| 23 | 22 | * Class JobService |
@@ -106,44 +106,44 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | public function process($languages, $files) { |
| 108 | 108 | try { |
| 109 | - $this->logger->debug ( 'Will now process files: ' . json_encode ( $files ) . ' with languages: ' . json_encode ( $languages ), [ |
|
| 109 | + $this->logger->debug('Will now process files: ' . json_encode($files) . ' with languages: ' . json_encode($languages), [ |
|
| 110 | 110 | 'app' => 'ocr' |
| 111 | - ] ); |
|
| 111 | + ]); |
|
| 112 | 112 | // Check if files and language not empty |
| 113 | - $noLang = $this->noLanguage ( $languages ); |
|
| 114 | - if (! empty ( $files ) && ($this->checkForAcceptedLanguages ( $languages ) || $noLang)) { |
|
| 113 | + $noLang = $this->noLanguage($languages); |
|
| 114 | + if (!empty ($files) && ($this->checkForAcceptedLanguages($languages) || $noLang)) { |
|
| 115 | 115 | // language part: |
| 116 | 116 | if ($noLang) { |
| 117 | 117 | $languages = []; |
| 118 | 118 | } |
| 119 | 119 | // file part: |
| 120 | - $fileInfo = $this->fileService->buildFileInfo ( $files ); |
|
| 121 | - foreach ( $fileInfo as $fInfo ) { |
|
| 120 | + $fileInfo = $this->fileService->buildFileInfo($files); |
|
| 121 | + foreach ($fileInfo as $fInfo) { |
|
| 122 | 122 | // Check Shared |
| 123 | - $shared = $this->fileService->checkSharedWithInitiator ( $fInfo ); |
|
| 124 | - $target = $this->fileService->buildTarget ( $fInfo, $shared ); |
|
| 125 | - $source = $this->fileService->buildSource ( $fInfo, $shared ); |
|
| 123 | + $shared = $this->fileService->checkSharedWithInitiator($fInfo); |
|
| 124 | + $target = $this->fileService->buildTarget($fInfo, $shared); |
|
| 125 | + $source = $this->fileService->buildSource($fInfo, $shared); |
|
| 126 | 126 | |
| 127 | 127 | // create a temp file for ocr processing purposes |
| 128 | - $tempFile = $this->tempM->getTemporaryFile (); |
|
| 128 | + $tempFile = $this->tempM->getTemporaryFile(); |
|
| 129 | 129 | |
| 130 | 130 | // set the running type |
| 131 | - $fType = $this->fileService->getCorrectType ( $fInfo ); |
|
| 131 | + $fType = $this->fileService->getCorrectType($fInfo); |
|
| 132 | 132 | |
| 133 | 133 | // TODO: create a security token |
| 134 | 134 | // Create job object |
| 135 | - $job = new OcrJob ( 'PENDING', $source, $target, $tempFile, $fType, $this->userId, false, $fInfo->getName(), null ); |
|
| 135 | + $job = new OcrJob('PENDING', $source, $target, $tempFile, $fType, $this->userId, false, $fInfo->getName(), null); |
|
| 136 | 136 | |
| 137 | 137 | // Init client and send task / job |
| 138 | 138 | // Feed the worker |
| 139 | - $this->queueService->sendJob ( $job, $languages, \OC::$SERVERROOT ); |
|
| 139 | + $this->queueService->sendJob($job, $languages, \OC::$SERVERROOT); |
|
| 140 | 140 | } |
| 141 | 141 | return 'PROCESSING'; |
| 142 | 142 | } else { |
| 143 | - throw new NotFoundException ( $this->l10n->t ( 'Empty parameters passed.' ) ); |
|
| 143 | + throw new NotFoundException($this->l10n->t('Empty parameters passed.')); |
|
| 144 | 144 | } |
| 145 | - } catch ( Exception $e ) { |
|
| 146 | - $this->handleException ( $e ); |
|
| 145 | + } catch (Exception $e) { |
|
| 146 | + $this->handleException($e); |
|
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | |
@@ -157,20 +157,20 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | public function deleteJob($jobId, $userId) { |
| 159 | 159 | try { |
| 160 | - $job = $this->jobMapper->find ( $jobId ); |
|
| 161 | - if ($job->getUserId () !== $userId) { |
|
| 162 | - throw new NotFoundException ( $this->l10n->t ( 'Cannot delete. Wrong owner.' ) ); |
|
| 160 | + $job = $this->jobMapper->find($jobId); |
|
| 161 | + if ($job->getUserId() !== $userId) { |
|
| 162 | + throw new NotFoundException($this->l10n->t('Cannot delete. Wrong owner.')); |
|
| 163 | 163 | } else { |
| 164 | - $job = $this->jobMapper->delete ( $job ); |
|
| 164 | + $job = $this->jobMapper->delete($job); |
|
| 165 | 165 | } |
| 166 | 166 | // TODO: return sanitized job for the personal settings page (no information about the source and target and such things. |
| 167 | 167 | return $job; |
| 168 | - } catch ( Exception $e ) { |
|
| 168 | + } catch (Exception $e) { |
|
| 169 | 169 | if ($e instanceof DoesNotExistException) { |
| 170 | - $ex = new NotFoundException ( $this->l10n->t ( 'Cannot delete. Wrong ID.' ) ); |
|
| 171 | - $this->handleException ( $ex ); |
|
| 170 | + $ex = new NotFoundException($this->l10n->t('Cannot delete. Wrong ID.')); |
|
| 171 | + $this->handleException($ex); |
|
| 172 | 172 | } else { |
| 173 | - $this->handleException ( $e ); |
|
| 173 | + $this->handleException($e); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | } |
@@ -184,20 +184,20 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | public function getAllJobsForUser($userId) { |
| 186 | 186 | try { |
| 187 | - $jobs = $this->jobMapper->findAll ( $userId ); |
|
| 188 | - $jobsNew = array (); |
|
| 189 | - for($x = 0; $x < count ( $jobs ); $x ++) { |
|
| 190 | - $newName = $this->fileService->removeFileExtension ( $jobs [$x] ); |
|
| 191 | - $jobs [$x]->setTarget ( $newName ); |
|
| 192 | - $jobs [$x]->setSource ( null ); |
|
| 193 | - $jobs [$x]->setTempFile ( null ); |
|
| 194 | - $jobs [$x]->setType ( null ); |
|
| 195 | - $jobs [$x]->setErrorDisplayed ( null ); |
|
| 196 | - array_push ( $jobsNew, $jobs [$x] ); |
|
| 187 | + $jobs = $this->jobMapper->findAll($userId); |
|
| 188 | + $jobsNew = array(); |
|
| 189 | + for ($x = 0; $x < count($jobs); $x++) { |
|
| 190 | + $newName = $this->fileService->removeFileExtension($jobs [$x]); |
|
| 191 | + $jobs [$x]->setTarget($newName); |
|
| 192 | + $jobs [$x]->setSource(null); |
|
| 193 | + $jobs [$x]->setTempFile(null); |
|
| 194 | + $jobs [$x]->setType(null); |
|
| 195 | + $jobs [$x]->setErrorDisplayed(null); |
|
| 196 | + array_push($jobsNew, $jobs [$x]); |
|
| 197 | 197 | } |
| 198 | 198 | return $jobsNew; |
| 199 | - } catch ( Exception $e ) { |
|
| 200 | - $this->handleException ( $e ); |
|
| 199 | + } catch (Exception $e) { |
|
| 200 | + $this->handleException($e); |
|
| 201 | 201 | } |
| 202 | 202 | } |
| 203 | 203 | |
@@ -212,20 +212,20 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | public function jobFinished($jobId, $failed, $errorMessage) { |
| 214 | 214 | try { |
| 215 | - $job = $this->jobMapper->find ( $jobId ); |
|
| 216 | - if (! $failed) { |
|
| 217 | - $job->setStatus ( 'PROCESSED' ); |
|
| 218 | - $this->jobMapper->update ( $job ); |
|
| 215 | + $job = $this->jobMapper->find($jobId); |
|
| 216 | + if (!$failed) { |
|
| 217 | + $job->setStatus('PROCESSED'); |
|
| 218 | + $this->jobMapper->update($job); |
|
| 219 | 219 | } else { |
| 220 | - $job->setStatus ( 'FAILED' ); |
|
| 220 | + $job->setStatus('FAILED'); |
|
| 221 | 221 | // TODO: add error message log and so on |
| 222 | - $this->jobMapper->update ( $job ); |
|
| 223 | - $this->logger->error ( $errorMessage, [ |
|
| 222 | + $this->jobMapper->update($job); |
|
| 223 | + $this->logger->error($errorMessage, [ |
|
| 224 | 224 | 'app' => 'ocr' |
| 225 | - ] ); |
|
| 225 | + ]); |
|
| 226 | 226 | } |
| 227 | - } catch ( Exception $e ) { |
|
| 228 | - $this->handleException ( $e ); |
|
| 227 | + } catch (Exception $e) { |
|
| 228 | + $this->handleException($e); |
|
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | |
@@ -238,31 +238,31 @@ discard block |
||
| 238 | 238 | */ |
| 239 | 239 | public function handleProcessed() { |
| 240 | 240 | try { |
| 241 | - $this->logger->debug ( 'Check if files were processed by ocr and if so, put them to the right dirs.', [ |
|
| 241 | + $this->logger->debug('Check if files were processed by ocr and if so, put them to the right dirs.', [ |
|
| 242 | 242 | 'app' => 'ocr' |
| 243 | - ] ); |
|
| 244 | - $processed = $this->jobMapper->findAllProcessed ( $this->userId ); |
|
| 245 | - foreach ( $processed as $job ) { |
|
| 246 | - if ($job->getType () === 'tess' && $this->fileService->fileExists ( $job->getTempFile () . '.txt' )) { |
|
| 243 | + ]); |
|
| 244 | + $processed = $this->jobMapper->findAllProcessed($this->userId); |
|
| 245 | + foreach ($processed as $job) { |
|
| 246 | + if ($job->getType() === 'tess' && $this->fileService->fileExists($job->getTempFile() . '.txt')) { |
|
| 247 | 247 | // Save the tmp file with newname |
| 248 | - $this->view->file_put_contents ( $job->getTarget (), $this->fileService->getFileContents ( $job->getTempFile () . '.txt' ) ); // need .txt because tesseract saves it like this |
|
| 248 | + $this->view->file_put_contents($job->getTarget(), $this->fileService->getFileContents($job->getTempFile() . '.txt')); // need .txt because tesseract saves it like this |
|
| 249 | 249 | // Cleaning temp files |
| 250 | - $this->jobMapper->delete ( $job ); |
|
| 251 | - $this->fileService->execRemove ( $job->getTempFile () . '.txt' ); |
|
| 252 | - } elseif ($job->getType () === 'mypdf' && $this->fileService->fileExists ( $job->getTempFile () )) { |
|
| 250 | + $this->jobMapper->delete($job); |
|
| 251 | + $this->fileService->execRemove($job->getTempFile() . '.txt'); |
|
| 252 | + } elseif ($job->getType() === 'mypdf' && $this->fileService->fileExists($job->getTempFile())) { |
|
| 253 | 253 | // Save the tmp file with newname |
| 254 | - $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 |
|
| 255 | - $this->jobMapper->delete ( $job ); |
|
| 256 | - $this->fileService->execRemove ( $job->getTempFile () ); |
|
| 254 | + $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 |
|
| 255 | + $this->jobMapper->delete($job); |
|
| 256 | + $this->fileService->execRemove($job->getTempFile()); |
|
| 257 | 257 | } else { |
| 258 | - $job->setStatus ( 'FAILED' ); |
|
| 259 | - $this->jobMapper->update ( $job ); |
|
| 260 | - throw new NotFoundException ( $this->l10n->t ( 'Temp file does not exist.' ) ); |
|
| 258 | + $job->setStatus('FAILED'); |
|
| 259 | + $this->jobMapper->update($job); |
|
| 260 | + throw new NotFoundException($this->l10n->t('Temp file does not exist.')); |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | return $processed; |
| 264 | - } catch ( Exception $e ) { |
|
| 265 | - $this->handleException ( $e ); |
|
| 264 | + } catch (Exception $e) { |
|
| 265 | + $this->handleException($e); |
|
| 266 | 266 | } |
| 267 | 267 | } |
| 268 | 268 | |
@@ -273,20 +273,20 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | public function handleFailed() { |
| 275 | 275 | try { |
| 276 | - $failed = $this->jobMapper->findAllFailed ( $this->userId ); |
|
| 277 | - foreach ( $failed as $job ) { |
|
| 276 | + $failed = $this->jobMapper->findAllFailed($this->userId); |
|
| 277 | + foreach ($failed as $job) { |
|
| 278 | 278 | // clean the tempfile |
| 279 | - $this->fileService->execRemove ( $job->getTempFile () ); |
|
| 279 | + $this->fileService->execRemove($job->getTempFile()); |
|
| 280 | 280 | // set error displayed |
| 281 | - $job->setErrorDisplayed ( true ); |
|
| 282 | - $this->jobMapper->update ( $job ); |
|
| 281 | + $job->setErrorDisplayed(true); |
|
| 282 | + $this->jobMapper->update($job); |
|
| 283 | 283 | } |
| 284 | - $this->logger->debug ( 'Following jobs failed: ' . json_encode ( $failed ), [ |
|
| 284 | + $this->logger->debug('Following jobs failed: ' . json_encode($failed), [ |
|
| 285 | 285 | 'app' => 'ocr' |
| 286 | - ] ); |
|
| 286 | + ]); |
|
| 287 | 287 | return $failed; |
| 288 | - } catch ( Exception $e ) { |
|
| 289 | - $this->handleException ( $e ); |
|
| 288 | + } catch (Exception $e) { |
|
| 289 | + $this->handleException($e); |
|
| 290 | 290 | } |
| 291 | 291 | } |
| 292 | 292 | |
@@ -298,10 +298,10 @@ discard block |
||
| 298 | 298 | * @return boolean |
| 299 | 299 | */ |
| 300 | 300 | private function checkForAcceptedLanguages($languages) { |
| 301 | - if (count ( array_diff ( $languages, [ |
|
| 301 | + if (count(array_diff($languages, [ |
|
| 302 | 302 | 'deu', |
| 303 | 303 | 'eng' |
| 304 | - ] ) ) === 0) { |
|
| 304 | + ])) === 0) { |
|
| 305 | 305 | return true; |
| 306 | 306 | } else { |
| 307 | 307 | return false; |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | * @return boolean |
| 316 | 316 | */ |
| 317 | 317 | private function noLanguage($languages) { |
| 318 | - if (in_array ( 'none', $languages )) { |
|
| 318 | + if (in_array('none', $languages)) { |
|
| 319 | 319 | return true; |
| 320 | 320 | } else { |
| 321 | 321 | return false; |
@@ -330,12 +330,12 @@ discard block |
||
| 330 | 330 | * @throws NotFoundException |
| 331 | 331 | */ |
| 332 | 332 | private function handleException($e) { |
| 333 | - $this->logger->logException ( $e, [ |
|
| 333 | + $this->logger->logException($e, [ |
|
| 334 | 334 | 'app' => 'ocr', |
| 335 | 335 | 'message' => 'Exception during job service function processing' |
| 336 | - ] ); |
|
| 336 | + ]); |
|
| 337 | 337 | if ($e instanceof NotFoundException) { |
| 338 | - throw new NotFoundException ( $e->getMessage () ); |
|
| 338 | + throw new NotFoundException($e->getMessage()); |
|
| 339 | 339 | } else { |
| 340 | 340 | throw $e; |
| 341 | 341 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * $UserId |
| 48 | 48 | */ |
| 49 | 49 | public function __construct($AppName, IRequest $request, JobService $service, $UserId) { |
| 50 | - parent::__construct ( $AppName, $request ); |
|
| 50 | + parent::__construct($AppName, $request); |
|
| 51 | 51 | $this->userId = $UserId; |
| 52 | 52 | $this->service = $service; |
| 53 | 53 | } |
@@ -62,8 +62,8 @@ discard block |
||
| 62 | 62 | * @return DataResponse |
| 63 | 63 | */ |
| 64 | 64 | public function process($languages, $files) { |
| 65 | - return $this->handleNotFound ( function () use ($languages, $files) { |
|
| 66 | - return $this->service->process ( $languages, $files ); |
|
| 65 | + return $this->handleNotFound(function() use ($languages, $files) { |
|
| 66 | + return $this->service->process($languages, $files); |
|
| 67 | 67 | } ); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -73,8 +73,8 @@ discard block |
||
| 73 | 73 | * @return \OCP\AppFramework\Http\DataResponse |
| 74 | 74 | */ |
| 75 | 75 | public function getAllJobs() { |
| 76 | - return $this->handleNotFound ( function () { |
|
| 77 | - return $this->service->getAllJobsForUser ( $this->userId ); |
|
| 76 | + return $this->handleNotFound(function() { |
|
| 77 | + return $this->service->getAllJobsForUser($this->userId); |
|
| 78 | 78 | } ); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -85,8 +85,8 @@ discard block |
||
| 85 | 85 | * @return \OCP\AppFramework\Http\DataResponse |
| 86 | 86 | */ |
| 87 | 87 | public function deleteJob($id) { |
| 88 | - return $this->handleNotFound ( function () use ($id) { |
|
| 89 | - return $this->service->deleteJob ( $id, $this->userId ); |
|
| 88 | + return $this->handleNotFound(function() use ($id) { |
|
| 89 | + return $this->service->deleteJob($id, $this->userId); |
|
| 90 | 90 | } ); |
| 91 | 91 | } |
| 92 | 92 | |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | * @return \OCP\AppFramework\Http\DataResponse |
| 102 | 102 | */ |
| 103 | 103 | public function finish($id, $failed, $error) { |
| 104 | - return $this->handleNotFound ( function () use ($id, $failed, $error) { |
|
| 105 | - return $this->service->jobFinished ( $id, $failed, $error ); |
|
| 104 | + return $this->handleNotFound(function() use ($id, $failed, $error) { |
|
| 105 | + return $this->service->jobFinished($id, $failed, $error); |
|
| 106 | 106 | } ); |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | \ No newline at end of file |
@@ -82,23 +82,23 @@ discard block |
||
| 82 | 82 | try { |
| 83 | 83 | // TODO: release lock |
| 84 | 84 | // returns user specific processed files |
| 85 | - $processed = $this->jobMapper->findAllProcessed ( $this->userId ); |
|
| 86 | - $this->jobService->handleProcessed (); |
|
| 85 | + $processed = $this->jobMapper->findAllProcessed($this->userId); |
|
| 86 | + $this->jobService->handleProcessed(); |
|
| 87 | 87 | |
| 88 | 88 | // return user specific pending state |
| 89 | - $pending = $this->jobMapper->findAllPending ( $this->userId ); |
|
| 89 | + $pending = $this->jobMapper->findAllPending($this->userId); |
|
| 90 | 90 | |
| 91 | 91 | // return user specific failed state and set up error |
| 92 | - $failed = $this->jobMapper->findAllFailed ( $this->userId ); |
|
| 93 | - $this->jobService->handleFailed (); |
|
| 92 | + $failed = $this->jobMapper->findAllFailed($this->userId); |
|
| 93 | + $this->jobService->handleFailed(); |
|
| 94 | 94 | |
| 95 | 95 | return [ |
| 96 | - 'processed' => count ( $processed ), |
|
| 97 | - 'failed' => count ( $failed ), |
|
| 98 | - 'pending' => count ( $pending ) |
|
| 96 | + 'processed' => count($processed), |
|
| 97 | + 'failed' => count($failed), |
|
| 98 | + 'pending' => count($pending) |
|
| 99 | 99 | ]; |
| 100 | - } catch ( Exception $e ) { |
|
| 101 | - $this->handleException ( $e ); |
|
| 100 | + } catch (Exception $e) { |
|
| 101 | + $this->handleException($e); |
|
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | |
@@ -110,12 +110,12 @@ discard block |
||
| 110 | 110 | * @throws NotFoundException |
| 111 | 111 | */ |
| 112 | 112 | private function handleException($e) { |
| 113 | - $this->logger->logException ( $e, [ |
|
| 113 | + $this->logger->logException($e, [ |
|
| 114 | 114 | 'app' => 'ocr', |
| 115 | 115 | 'message' => 'Exception during status service function processing' |
| 116 | - ] ); |
|
| 116 | + ]); |
|
| 117 | 117 | if ($e instanceof NotFoundException) { |
| 118 | - throw new NotFoundException ( $e->getMessage () ); |
|
| 118 | + throw new NotFoundException($e->getMessage()); |
|
| 119 | 119 | } else { |
| 120 | 120 | throw $e; |
| 121 | 121 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | public function checkSharedWithInitiator($fileInfo) { |
| 106 | 106 | try { |
| 107 | - $owner = str_replace ( 'home::', '', $fileInfo->getStoragename () ); |
|
| 107 | + $owner = str_replace('home::', '', $fileInfo->getStoragename()); |
|
| 108 | 108 | if ($this->userId === $owner) { |
| 109 | 109 | // user is owner (no shared file) |
| 110 | 110 | return false; |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | // user is not owner (shared file) |
| 113 | 113 | return true; |
| 114 | 114 | } |
| 115 | - } catch ( Exception $e ) { |
|
| 116 | - $this->handleException ( $e ); |
|
| 115 | + } catch (Exception $e) { |
|
| 116 | + $this->handleException($e); |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | public function buildTarget($fileInfo, $shared) { |
| 128 | 128 | if ($shared) { |
| 129 | - $target = $this->buildTargetForShared ( $fileInfo ); |
|
| 129 | + $target = $this->buildTargetForShared($fileInfo); |
|
| 130 | 130 | } else { |
| 131 | - $target = $this->buildTargetNotForShared ( $fileInfo ); |
|
| 131 | + $target = $this->buildTargetNotForShared($fileInfo); |
|
| 132 | 132 | } |
| 133 | 133 | return $target; |
| 134 | 134 | } |
@@ -141,9 +141,9 @@ discard block |
||
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | 143 | public function buildSource($fileInfo, $shared) { |
| 144 | - $source = $fileInfo->getPath (); |
|
| 144 | + $source = $fileInfo->getPath(); |
|
| 145 | 145 | if ($shared) { |
| 146 | - $source = str_replace ( 'home::', '', $fileInfo->getStoragename () ) . '/' . $source; |
|
| 146 | + $source = str_replace('home::', '', $fileInfo->getStoragename()) . '/' . $source; |
|
| 147 | 147 | } else { |
| 148 | 148 | $source = $this->userId . '/' . $source; |
| 149 | 149 | } |
@@ -160,22 +160,22 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | public function buildFileInfo($files) { |
| 162 | 162 | try { |
| 163 | - $fileArray = array (); |
|
| 164 | - foreach ( $files as $file ) { |
|
| 163 | + $fileArray = array(); |
|
| 164 | + foreach ($files as $file) { |
|
| 165 | 165 | // Check if anything is missing and file type is correct |
| 166 | - if (! empty ( $file ['id'] )) { |
|
| 166 | + if (!empty ($file ['id'])) { |
|
| 167 | 167 | |
| 168 | - $fileInfo = $this->fileMapper->find ( $file ['id'] ); |
|
| 169 | - $this->checkMimeType ( $fileInfo ); |
|
| 168 | + $fileInfo = $this->fileMapper->find($file ['id']); |
|
| 169 | + $this->checkMimeType($fileInfo); |
|
| 170 | 170 | |
| 171 | - array_push ( $fileArray, $fileInfo ); |
|
| 171 | + array_push($fileArray, $fileInfo); |
|
| 172 | 172 | } else { |
| 173 | - throw new NotFoundException ( $this->l10n->t ( 'Wrong parameter.' ) ); |
|
| 173 | + throw new NotFoundException($this->l10n->t('Wrong parameter.')); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | return $fileArray; |
| 177 | - } catch ( Exception $e ) { |
|
| 178 | - $this->handleException ( $e ); |
|
| 177 | + } catch (Exception $e) { |
|
| 178 | + $this->handleException($e); |
|
| 179 | 179 | } |
| 180 | 180 | } |
| 181 | 181 | |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | * @return string |
| 187 | 187 | */ |
| 188 | 188 | public function removeFileExtension($job) { |
| 189 | - return substr ( $job->getTarget (), 0, strrpos ( $job->getTarget (), '_OCR' ) ); |
|
| 189 | + return substr($job->getTarget(), 0, strrpos($job->getTarget(), '_OCR')); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | * @return string |
| 198 | 198 | */ |
| 199 | 199 | public function getCorrectType($fileInfo) { |
| 200 | - if ($fileInfo->getMimetype () === FileService::$MIMETYPE_PDF) { |
|
| 200 | + if ($fileInfo->getMimetype() === FileService::$MIMETYPE_PDF) { |
|
| 201 | 201 | return 'mypdf'; |
| 202 | 202 | } else { |
| 203 | 203 | return 'tess'; |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * @param string $pathToFile |
| 213 | 213 | */ |
| 214 | 214 | public function execRemove($pathToFile) { |
| 215 | - exec ( 'rm ' . $pathToFile ); |
|
| 215 | + exec('rm ' . $pathToFile); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | * @return string |
| 225 | 225 | */ |
| 226 | 226 | public function getFileContents($pathToFile) { |
| 227 | - return file_get_contents ( $pathToFile ); |
|
| 227 | + return file_get_contents($pathToFile); |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | * @return boolean |
| 237 | 237 | */ |
| 238 | 238 | public function fileExists($pathToFile) { |
| 239 | - return file_exists ( $pathToFile ); |
|
| 239 | + return file_exists($pathToFile); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | /** |
@@ -251,32 +251,32 @@ discard block |
||
| 251 | 251 | */ |
| 252 | 252 | protected function buildTargetForShared(File $fileInfo) { |
| 253 | 253 | try { |
| 254 | - $share = $this->shareMapper->find ( $fileInfo->getFileid (), $this->userId, str_replace ( 'home::', '', $fileInfo->getStoragename () ) ); |
|
| 254 | + $share = $this->shareMapper->find($fileInfo->getFileid(), $this->userId, str_replace('home::', '', $fileInfo->getStoragename())); |
|
| 255 | 255 | |
| 256 | 256 | // get rid of the .png or .pdf and so on |
| 257 | - $fileName = substr ( $share->getFileTarget (), 0, (strrpos ( $share->getFileTarget (), '.' )) ); // '/thedom.png' => '/thedom' || '/Test/thedom.png' => '/Test/thedom' |
|
| 257 | + $fileName = substr($share->getFileTarget(), 0, (strrpos($share->getFileTarget(), '.'))); // '/thedom.png' => '/thedom' || '/Test/thedom.png' => '/Test/thedom' |
|
| 258 | 258 | |
| 259 | 259 | // remove everything in front of and including of the first appearance of a slash from behind |
| 260 | - $fileName = substr ( strrchr ( $fileName, "/" ), 1 ); // '/thedom' => 'thedom' || '/Test/thedom' => 'thedom' |
|
| 260 | + $fileName = substr(strrchr($fileName, "/"), 1); // '/thedom' => 'thedom' || '/Test/thedom' => 'thedom' |
|
| 261 | 261 | |
| 262 | 262 | // eliminate the file name from the path |
| 263 | - $filePath = dirname ( $share->getFileTarget () ); // '/thedom.png' => '/' || '/Test/thedom.png' => '/Test' |
|
| 263 | + $filePath = dirname($share->getFileTarget()); // '/thedom.png' => '/' || '/Test/thedom.png' => '/Test' |
|
| 264 | 264 | |
| 265 | 265 | // replace the first slash |
| 266 | - $pos = strpos ( $filePath, '/' ); |
|
| 266 | + $pos = strpos($filePath, '/'); |
|
| 267 | 267 | if ($pos !== false) { |
| 268 | - $filePath = substr_replace ( $filePath, '', $pos, strlen ( '/' ) ); // '/' => '' || '/Test/' => 'Test' |
|
| 268 | + $filePath = substr_replace($filePath, '', $pos, strlen('/')); // '/' => '' || '/Test/' => 'Test' |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - if ($fileInfo->getMimetype () === FileService::$MIMETYPE_PDF) { |
|
| 271 | + if ($fileInfo->getMimetype() === FileService::$MIMETYPE_PDF) { |
|
| 272 | 272 | // PDFs: |
| 273 | - return \OCP\Files::buildNotExistingFileName ( $filePath, $fileName . '_OCR.pdf' ); |
|
| 273 | + return \OCP\Files::buildNotExistingFileName($filePath, $fileName . '_OCR.pdf'); |
|
| 274 | 274 | } else { |
| 275 | 275 | // IMAGES: |
| 276 | - return \OCP\Files::buildNotExistingFileName ( $filePath, $fileName . '_OCR.txt' ); |
|
| 276 | + return \OCP\Files::buildNotExistingFileName($filePath, $fileName . '_OCR.txt'); |
|
| 277 | 277 | } |
| 278 | - } catch ( Exception $e ) { |
|
| 279 | - $this->handleException ( $e ); |
|
| 278 | + } catch (Exception $e) { |
|
| 279 | + $this->handleException($e); |
|
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | |
@@ -293,32 +293,32 @@ discard block |
||
| 293 | 293 | protected function buildTargetNotForShared(File $fileInfo) { |
| 294 | 294 | try { |
| 295 | 295 | // get rid of the .png or .pdf and so on |
| 296 | - $fileName = substr ( $fileInfo->getName (), 0, (strrpos ( $fileInfo->getName (), '.' )) ); // 'thedom.png' => 'thedom' |
|
| 296 | + $fileName = substr($fileInfo->getName(), 0, (strrpos($fileInfo->getName(), '.'))); // 'thedom.png' => 'thedom' |
|
| 297 | 297 | |
| 298 | 298 | // eliminate the file name from the path |
| 299 | - $filePath = str_replace ( $fileInfo->getName (), '', $fileInfo->getPath () ); // 'files/Test/thedom.png' => 'files/Test/' || 'files/thedom.png' => 'files/' |
|
| 299 | + $filePath = str_replace($fileInfo->getName(), '', $fileInfo->getPath()); // 'files/Test/thedom.png' => 'files/Test/' || 'files/thedom.png' => 'files/' |
|
| 300 | 300 | |
| 301 | 301 | // and get the path on top of the files/ dir |
| 302 | - $filePath = str_replace ( 'files', '', $filePath ); // 'files/Test/' => '/Test/' || 'files/' => '/' |
|
| 302 | + $filePath = str_replace('files', '', $filePath); // 'files/Test/' => '/Test/' || 'files/' => '/' |
|
| 303 | 303 | |
| 304 | 304 | // remove the last slash |
| 305 | - $filePath = substr_replace ( $filePath, '', strrpos ( $filePath, '/' ), strlen ( '/' ) ); // '/Test/' => '/Test' || '/' => '' |
|
| 305 | + $filePath = substr_replace($filePath, '', strrpos($filePath, '/'), strlen('/')); // '/Test/' => '/Test' || '/' => '' |
|
| 306 | 306 | |
| 307 | 307 | // replace the first slash |
| 308 | - $pos = strpos ( $filePath, '/' ); |
|
| 308 | + $pos = strpos($filePath, '/'); |
|
| 309 | 309 | if ($pos !== false) { |
| 310 | - $filePath = substr_replace ( $filePath, '', $pos, strlen ( '/' ) ); // '/Test' => '// 'Test' || '/' => '' |
|
| 310 | + $filePath = substr_replace($filePath, '', $pos, strlen('/')); // '/Test' => '// 'Test' || '/' => '' |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | - if ($fileInfo->getMimetype () === FileService::$MIMETYPE_PDF) { |
|
| 313 | + if ($fileInfo->getMimetype() === FileService::$MIMETYPE_PDF) { |
|
| 314 | 314 | // PDFs: |
| 315 | - return \OCP\Files::buildNotExistingFileName ( $filePath, $fileName . '_OCR.pdf' ); |
|
| 315 | + return \OCP\Files::buildNotExistingFileName($filePath, $fileName . '_OCR.pdf'); |
|
| 316 | 316 | } else { |
| 317 | 317 | // IMAGES: |
| 318 | - return \OCP\Files::buildNotExistingFileName ( $filePath, $fileName . '_OCR.txt' ); |
|
| 318 | + return \OCP\Files::buildNotExistingFileName($filePath, $fileName . '_OCR.txt'); |
|
| 319 | 319 | } |
| 320 | - } catch ( Exception $e ) { |
|
| 321 | - $this->handleException ( $e ); |
|
| 320 | + } catch (Exception $e) { |
|
| 321 | + $this->handleException($e); |
|
| 322 | 322 | } |
| 323 | 323 | } |
| 324 | 324 | |
@@ -329,14 +329,14 @@ discard block |
||
| 329 | 329 | */ |
| 330 | 330 | private function checkMimeType(File $fileInfo) { |
| 331 | 331 | try { |
| 332 | - if (! $fileInfo || ! in_array ( $fileInfo->getMimetype (), FileService::$ALLOWED_MIMETYPES )) { |
|
| 333 | - $this->logger->debug ( 'Getting FileInfo did not work or not included in the ALLOWED_MIMETYPES array.', [ |
|
| 332 | + if (!$fileInfo || !in_array($fileInfo->getMimetype(), FileService::$ALLOWED_MIMETYPES)) { |
|
| 333 | + $this->logger->debug('Getting FileInfo did not work or not included in the ALLOWED_MIMETYPES array.', [ |
|
| 334 | 334 | 'app' => 'ocr' |
| 335 | - ] ); |
|
| 336 | - throw new NotFoundException ( $this->l10n->t ( 'Wrong MIME type.' ) ); |
|
| 335 | + ]); |
|
| 336 | + throw new NotFoundException($this->l10n->t('Wrong MIME type.')); |
|
| 337 | 337 | } |
| 338 | - } catch ( Exception $e ) { |
|
| 339 | - $this->handleException ( $e ); |
|
| 338 | + } catch (Exception $e) { |
|
| 339 | + $this->handleException($e); |
|
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | 342 | |
@@ -348,12 +348,12 @@ discard block |
||
| 348 | 348 | * @throws NotFoundException |
| 349 | 349 | */ |
| 350 | 350 | private function handleException($e) { |
| 351 | - $this->logger->logException ( $e, [ |
|
| 351 | + $this->logger->logException($e, [ |
|
| 352 | 352 | 'app' => 'ocr', |
| 353 | 353 | 'message' => 'Exception during file service function processing' |
| 354 | - ] ); |
|
| 354 | + ]); |
|
| 355 | 355 | if ($e instanceof NotFoundException) { |
| 356 | - throw new NotFoundException ( $e->getMessage () ); |
|
| 356 | + throw new NotFoundException($e->getMessage()); |
|
| 357 | 357 | } else { |
| 358 | 358 | throw $e; |
| 359 | 359 | } |
@@ -38,7 +38,7 @@ |
||
| 38 | 38 | * |
| 39 | 39 | */ |
| 40 | 40 | public function getName() { |
| 41 | - return $this->l->t ( 'OCR' ); |
|
| 41 | + return $this->l->t('OCR'); |
|
| 42 | 42 | } |
| 43 | 43 | /** |
| 44 | 44 | * |
@@ -31,9 +31,9 @@ |
||
| 31 | 31 | * @return TemplateResponse |
| 32 | 32 | */ |
| 33 | 33 | public function getForm() { |
| 34 | - return new TemplateResponse( 'ocr', 'settings-admin', [ |
|
| 35 | - 'languages' => $this->config->getAppValue ( 'ocr', 'languages' ) |
|
| 36 | - ], 'blank' ); |
|
| 34 | + return new TemplateResponse('ocr', 'settings-admin', [ |
|
| 35 | + 'languages' => $this->config->getAppValue('ocr', 'languages') |
|
| 36 | + ], 'blank'); |
|
| 37 | 37 | } |
| 38 | 38 | /** |
| 39 | 39 | * |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @return string |
| 52 | 52 | */ |
| 53 | 53 | public function getAppValue($key) { |
| 54 | - return $this->config->getAppValue ( $this->appName, $key ); |
|
| 54 | + return $this->config->getAppValue($this->appName, $key); |
|
| 55 | 55 | } |
| 56 | 56 | /** |
| 57 | 57 | * Set a value by key |
@@ -62,10 +62,10 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | public function setAppValue($key, $value) { |
| 64 | 64 | if ($key === 'languages') { |
| 65 | - if (! preg_match ( '/^(([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4});)*([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4})$/', $value )) { |
|
| 66 | - throw new NotFoundException ( $this->l10n->t ( 'The languages are not specified in the correct format.' ) ); |
|
| 65 | + if (!preg_match('/^(([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4});)*([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4})$/', $value)) { |
|
| 66 | + throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.')); |
|
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | - return $this->config->setAppValue ( $this->appName, $key, $value ); |
|
| 69 | + return $this->config->setAppValue($this->appName, $key, $value); |
|
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | \ No newline at end of file |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @param string $userId |
| 40 | 40 | */ |
| 41 | 41 | public function __construct($appName, IRequest $request, IL10N $l10n, AppConfigService $appConfig, $userId) { |
| 42 | - parent::__construct ( $appName, $request ); |
|
| 42 | + parent::__construct($appName, $request); |
|
| 43 | 43 | $this->l10n = $l10n; |
| 44 | 44 | $this->appConfig = $appConfig; |
| 45 | 45 | } |
@@ -50,8 +50,8 @@ discard block |
||
| 50 | 50 | * @return DataResponse |
| 51 | 51 | */ |
| 52 | 52 | public function getSettings() { |
| 53 | - return $this->handleNotFound ( function () { |
|
| 54 | - return [ 'languages' => $this->appConfig->getAppValue ( 'languages' )]; |
|
| 53 | + return $this->handleNotFound(function() { |
|
| 54 | + return ['languages' => $this->appConfig->getAppValue('languages')]; |
|
| 55 | 55 | } ); |
| 56 | 56 | } |
| 57 | 57 | |
@@ -62,12 +62,12 @@ discard block |
||
| 62 | 62 | * @return DataResponse |
| 63 | 63 | */ |
| 64 | 64 | public function setSettings($languages) { |
| 65 | - return $this->handleNotFound ( function () use ($languages) { |
|
| 65 | + return $this->handleNotFound(function() use ($languages) { |
|
| 66 | 66 | if ($languages !== null) { |
| 67 | - $this->appConfig->setAppValue ( 'languages', $languages ); |
|
| 68 | - return $this->l10n->t ( 'Saved' ); |
|
| 67 | + $this->appConfig->setAppValue('languages', $languages); |
|
| 68 | + return $this->l10n->t('Saved'); |
|
| 69 | 69 | } else { |
| 70 | - throw new NotFoundException ( $this->l10n->t ( 'The languages are not specified in the correct format.' ) ); |
|
| 70 | + throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.')); |
|
| 71 | 71 | } |
| 72 | 72 | } ); |
| 73 | 73 | } |
@@ -88,35 +88,35 @@ discard block |
||
| 88 | 88 | public function sendJob($job, $languages, $occDir) { |
| 89 | 89 | try { |
| 90 | 90 | // check for messaging and put everything together |
| 91 | - if (!extension_loaded ( 'redis' )) { |
|
| 92 | - $this->logger->debug ( 'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.', [ |
|
| 91 | + if (!extension_loaded('redis')) { |
|
| 92 | + $this->logger->debug('It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.', [ |
|
| 93 | 93 | 'app' => 'ocr' |
| 94 | - ] ); |
|
| 94 | + ]); |
|
| 95 | 95 | throw new NotFoundException($this->l10n->t('Message queueing capabilities are missing on the server.')); |
| 96 | 96 | } |
| 97 | 97 | // TODO: adjust mapping according to zettel |
| 98 | - $job = $this->mapper->insert ( $job ); |
|
| 99 | - $msg = json_encode ( array ( |
|
| 100 | - 'type' => $job->getType (), |
|
| 101 | - 'source' => $this->config->getSystemValue ( 'datadirectory' ) . '/' . $job->getSource (), |
|
| 102 | - 'tempfile' => $job->getTempFile (), |
|
| 98 | + $job = $this->mapper->insert($job); |
|
| 99 | + $msg = json_encode(array( |
|
| 100 | + 'type' => $job->getType(), |
|
| 101 | + 'source' => $this->config->getSystemValue('datadirectory') . '/' . $job->getSource(), |
|
| 102 | + 'tempfile' => $job->getTempFile(), |
|
| 103 | 103 | 'languages' => $languages, |
| 104 | - 'jobid' => $job->getId (), |
|
| 104 | + 'jobid' => $job->getId(), |
|
| 105 | 105 | 'occdir' => $occDir |
| 106 | - ) ); |
|
| 107 | - if (msg_send ( $this->queue, 1, $msg )) { |
|
| 108 | - $this->logger->debug ( 'Client message: ' . $msg, [ |
|
| 106 | + )); |
|
| 107 | + if (msg_send($this->queue, 1, $msg)) { |
|
| 108 | + $this->logger->debug('Client message: ' . $msg, [ |
|
| 109 | 109 | 'app' => 'ocr' |
| 110 | - ] ); |
|
| 110 | + ]); |
|
| 111 | 111 | } else { |
| 112 | - $this->mapper->delete ( $job ); |
|
| 113 | - throw new NotFoundException ( $this->l10n->t ( 'Could not add files to the OCR processing queue.' ) ); |
|
| 112 | + $this->mapper->delete($job); |
|
| 113 | + throw new NotFoundException($this->l10n->t('Could not add files to the OCR processing queue.')); |
|
| 114 | 114 | } |
| 115 | - } catch ( Exception $e ) { |
|
| 116 | - exec ( 'rm ' . $job->getTempFile () ); |
|
| 117 | - $job->setStatus ( 'FAILED' ); |
|
| 118 | - $this->mapper->update ( $job ); |
|
| 119 | - $this->handleException ( $e ); |
|
| 115 | + } catch (Exception $e) { |
|
| 116 | + exec('rm ' . $job->getTempFile()); |
|
| 117 | + $job->setStatus('FAILED'); |
|
| 118 | + $this->mapper->update($job); |
|
| 119 | + $this->handleException($e); |
|
| 120 | 120 | } |
| 121 | 121 | } |
| 122 | 122 | |
@@ -130,12 +130,12 @@ discard block |
||
| 130 | 130 | * @throws NotFoundException |
| 131 | 131 | */ |
| 132 | 132 | private function handleException($e) { |
| 133 | - $this->logger->logException ( $e, [ |
|
| 133 | + $this->logger->logException($e, [ |
|
| 134 | 134 | 'app' => 'ocr', |
| 135 | 135 | 'message' => 'Exception during message queue processing' |
| 136 | - ] ); |
|
| 136 | + ]); |
|
| 137 | 137 | if ($e instanceof NotFoundException) { |
| 138 | - throw new NotFoundException ( $e->getMessage () ); |
|
| 138 | + throw new NotFoundException($e->getMessage()); |
|
| 139 | 139 | } else { |
| 140 | 140 | throw $e; |
| 141 | 141 | } |