Completed
Pull Request — master (#93)
by Janis
04:28
created
lib/Migration/DropOldTable.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -17,42 +17,42 @@
 block discarded – undo
17 17
 
18 18
 class DropOldTable implements IRepairStep {
19 19
 
20
-    /** @var IDBConnection */
21
-    protected $connection;
22
-
23
-    /**
24
-     *
25
-     * @param IDBConnection $connection            
26
-     */
27
-    public function __construct(IDBConnection $connection) {
28
-        $this->connection = $connection;
29
-    }
30
-
31
-    /**
32
-     * Returns the step's name
33
-     * 
34
-     * @return string
35
-     */
36
-    public function getName() {
37
-        return 'Drop old database table';
38
-    }
39
-
40
-    /**
41
-     * Run repair step.
42
-     * Must throw exception on error.
43
-     * 
44
-     * @throws \Exception in case of failure
45
-     */
46
-    public function run(IOutput $output) {
47
-        $output->startProgress(1);
48
-        if ($this->connection->tableExists('ocr_jobs')) {
49
-            $this->connection->dropTable('ocr_jobs');
50
-        }
51
-        // still needed for downwards compatibility
52
-        if ($this->connection->tableExists('ocr_status')) {
53
-            $this->connection->dropTable('ocr_status');
54
-        }
55
-        $output->advance(1, "Drop old database table: ocr_jobs");
56
-        $output->finishProgress();
57
-    }
20
+	/** @var IDBConnection */
21
+	protected $connection;
22
+
23
+	/**
24
+	 *
25
+	 * @param IDBConnection $connection            
26
+	 */
27
+	public function __construct(IDBConnection $connection) {
28
+		$this->connection = $connection;
29
+	}
30
+
31
+	/**
32
+	 * Returns the step's name
33
+	 * 
34
+	 * @return string
35
+	 */
36
+	public function getName() {
37
+		return 'Drop old database table';
38
+	}
39
+
40
+	/**
41
+	 * Run repair step.
42
+	 * Must throw exception on error.
43
+	 * 
44
+	 * @throws \Exception in case of failure
45
+	 */
46
+	public function run(IOutput $output) {
47
+		$output->startProgress(1);
48
+		if ($this->connection->tableExists('ocr_jobs')) {
49
+			$this->connection->dropTable('ocr_jobs');
50
+		}
51
+		// still needed for downwards compatibility
52
+		if ($this->connection->tableExists('ocr_status')) {
53
+			$this->connection->dropTable('ocr_status');
54
+		}
55
+		$output->advance(1, "Drop old database table: ocr_jobs");
56
+		$output->finishProgress();
57
+	}
58 58
 }
59 59
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Controller/PersonalSettingsController.php 1 patch
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -17,23 +17,23 @@
 block discarded – undo
17 17
 
18 18
 class PersonalSettingsController extends Controller {
19 19
 
20
-    /**
21
-     * PersonalSettingsController constructor.
22
-     * 
23
-     * @param string $AppName            
24
-     * @param IRequest $request            
25
-     */
26
-    public function __construct($AppName, IRequest $request) {
27
-        parent::__construct($AppName, $request);
28
-    }
20
+	/**
21
+	 * PersonalSettingsController constructor.
22
+	 * 
23
+	 * @param string $AppName            
24
+	 * @param IRequest $request            
25
+	 */
26
+	public function __construct($AppName, IRequest $request) {
27
+		parent::__construct($AppName, $request);
28
+	}
29 29
 
30
-    /**
31
-     * @NoAdminRequired
32
-     * 
33
-     * @return Template
34
-     */
35
-    public function displayPanel() {
36
-        $tmpl = new Template('ocr', 'settings-personal');
37
-        return $tmpl;
38
-    }
30
+	/**
31
+	 * @NoAdminRequired
32
+	 * 
33
+	 * @return Template
34
+	 */
35
+	public function displayPanel() {
36
+		$tmpl = new Template('ocr', 'settings-personal');
37
+		return $tmpl;
38
+	}
39 39
 }
40 40
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Controller/Errors.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -23,18 +23,18 @@
 block discarded – undo
23 23
  */
24 24
 trait Errors {
25 25
 
26
-    /**
27
-     * handles the thrown Errors for all Controllers
28
-     * and sends a DataResponse with the ErrorMessage of the service
29
-     * 
30
-     * @param Closure $callback            
31
-     * @return DataResponse
32
-     */
33
-    protected function handleNotFound(Closure $callback) {
34
-        try {
35
-            return new DataResponse($callback());
36
-        } catch (NotFoundException $e) {
37
-            return new DataResponse($e->getMessage(), Http::STATUS_NOT_FOUND);
38
-        }
39
-    }
26
+	/**
27
+	 * handles the thrown Errors for all Controllers
28
+	 * and sends a DataResponse with the ErrorMessage of the service
29
+	 * 
30
+	 * @param Closure $callback            
31
+	 * @return DataResponse
32
+	 */
33
+	protected function handleNotFound(Closure $callback) {
34
+		try {
35
+			return new DataResponse($callback());
36
+		} catch (NotFoundException $e) {
37
+			return new DataResponse($e->getMessage(), Http::STATUS_NOT_FOUND);
38
+		}
39
+	}
40 40
 }
41 41
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Controller/AdminSettingsController.php 1 patch
Indentation   +85 added lines, -85 removed lines patch added patch discarded remove patch
@@ -22,95 +22,95 @@
 block discarded – undo
22 22
 
23 23
 class AdminSettingsController extends Controller {
24 24
 
25
-    /** @var IL10N */
26
-    private $l10n;
25
+	/** @var IL10N */
26
+	private $l10n;
27 27
 
28
-    /** @var AppConfigService */
29
-    private $appConfig;
30
-    use Errors;
28
+	/** @var AppConfigService */
29
+	private $appConfig;
30
+	use Errors;
31 31
 
32
-    /**
33
-     * Constructor
34
-     * 
35
-     * @param string $appName            
36
-     * @param IRequest $request            
37
-     * @param IL10N $l10n            
38
-     * @param AppConfigService $appConfig            
39
-     * @param string $userId            
40
-     */
41
-    public function __construct($appName, IRequest $request, IL10N $l10n, AppConfigService $appConfig, $userId) {
42
-        parent::__construct($appName, $request);
43
-        $this->l10n = $l10n;
44
-        $this->appConfig = $appConfig;
45
-    }
32
+	/**
33
+	 * Constructor
34
+	 * 
35
+	 * @param string $appName            
36
+	 * @param IRequest $request            
37
+	 * @param IL10N $l10n            
38
+	 * @param AppConfigService $appConfig            
39
+	 * @param string $userId            
40
+	 */
41
+	public function __construct($appName, IRequest $request, IL10N $l10n, AppConfigService $appConfig, $userId) {
42
+		parent::__construct($appName, $request);
43
+		$this->l10n = $l10n;
44
+		$this->appConfig = $appConfig;
45
+	}
46 46
 
47
-    /**
48
-     * @NoAdminRequired
49
-     * 
50
-     * @return DataResponse
51
-     */
52
-    public function getLanguages() {
53
-        return $this->handleNotFound(
54
-                function () {
55
-                    return [
56
-                            'languages' => $this->appConfig->getAppValue(OcrConstants::LANGUAGES_CONFIG_KEY)
57
-                    ];
58
-                });
59
-    }
47
+	/**
48
+	 * @NoAdminRequired
49
+	 * 
50
+	 * @return DataResponse
51
+	 */
52
+	public function getLanguages() {
53
+		return $this->handleNotFound(
54
+				function () {
55
+					return [
56
+							'languages' => $this->appConfig->getAppValue(OcrConstants::LANGUAGES_CONFIG_KEY)
57
+					];
58
+				});
59
+	}
60 60
 
61
-    /**
62
-     * @NoAdminRequired
63
-     * 
64
-     * @return DataResponse
65
-     */
66
-    public function evaluateRedisSettings() {
67
-        return $this->handleNotFound(
68
-                function () {
69
-                    return [
70
-                            'set' => $this->appConfig->evaluateRedisSettings()
71
-                    ];
72
-                });
73
-    }
61
+	/**
62
+	 * @NoAdminRequired
63
+	 * 
64
+	 * @return DataResponse
65
+	 */
66
+	public function evaluateRedisSettings() {
67
+		return $this->handleNotFound(
68
+				function () {
69
+					return [
70
+							'set' => $this->appConfig->evaluateRedisSettings()
71
+					];
72
+				});
73
+	}
74 74
 
75
-    /**
76
-     * Sets the languages that are supported by the docker worker instance.
77
-     * 
78
-     * @param string $languages            
79
-     * @return DataResponse
80
-     */
81
-    public function setLanguages($languages) {
82
-        return $this->handleNotFound(
83
-                function () use ($languages) {
84
-                    if ($languages !== null) {
85
-                        $this->appConfig->setAppValue(OcrConstants::LANGUAGES_CONFIG_KEY, $languages);
86
-                        return $this->l10n->t('Saved');
87
-                    } else {
88
-                        throw new NotFoundException(
89
-                                $this->l10n->t('The languages are not specified in the correct format.'));
90
-                    }
91
-                });
92
-    }
75
+	/**
76
+	 * Sets the languages that are supported by the docker worker instance.
77
+	 * 
78
+	 * @param string $languages            
79
+	 * @return DataResponse
80
+	 */
81
+	public function setLanguages($languages) {
82
+		return $this->handleNotFound(
83
+				function () use ($languages) {
84
+					if ($languages !== null) {
85
+						$this->appConfig->setAppValue(OcrConstants::LANGUAGES_CONFIG_KEY, $languages);
86
+						return $this->l10n->t('Saved');
87
+					} else {
88
+						throw new NotFoundException(
89
+								$this->l10n->t('The languages are not specified in the correct format.'));
90
+					}
91
+				});
92
+	}
93 93
 
94
-    /**
95
-     * Sets the Redis settings.
96
-     * 
97
-     * @param string $redisHost            
98
-     * @param string $redisPort            
99
-     * @param string $redisDb            
100
-     * @return DataResponse
101
-     */
102
-    public function setRedis($redisHost, $redisPort, $redisDb) {
103
-        return $this->handleNotFound(
104
-                function () use ($redisHost, $redisPort, $redisDb) {
105
-                    if ($redisHost !== null && $redisPort !== null && $redisDb !== null) {
106
-                        $this->appConfig->setAppValue(OcrConstants::REDIS_CONFIG_KEY_HOST, $redisHost);
107
-                        $this->appConfig->setAppValue(OcrConstants::REDIS_CONFIG_KEY_PORT, $redisPort);
108
-                        $this->appConfig->setAppValue(OcrConstants::REDIS_CONFIG_KEY_DB, $redisDb);
109
-                        return $this->l10n->t('Saved');
110
-                    } else {
111
-                        throw new NotFoundException(
112
-                                $this->l10n->t('The Redis settings do not have the right format.'));
113
-                    }
114
-                });
115
-    }
94
+	/**
95
+	 * Sets the Redis settings.
96
+	 * 
97
+	 * @param string $redisHost            
98
+	 * @param string $redisPort            
99
+	 * @param string $redisDb            
100
+	 * @return DataResponse
101
+	 */
102
+	public function setRedis($redisHost, $redisPort, $redisDb) {
103
+		return $this->handleNotFound(
104
+				function () use ($redisHost, $redisPort, $redisDb) {
105
+					if ($redisHost !== null && $redisPort !== null && $redisDb !== null) {
106
+						$this->appConfig->setAppValue(OcrConstants::REDIS_CONFIG_KEY_HOST, $redisHost);
107
+						$this->appConfig->setAppValue(OcrConstants::REDIS_CONFIG_KEY_PORT, $redisPort);
108
+						$this->appConfig->setAppValue(OcrConstants::REDIS_CONFIG_KEY_DB, $redisDb);
109
+						return $this->l10n->t('Saved');
110
+					} else {
111
+						throw new NotFoundException(
112
+								$this->l10n->t('The Redis settings do not have the right format.'));
113
+					}
114
+				});
115
+	}
116 116
 }
117 117
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Controller/JobController.php 1 patch
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -23,71 +23,71 @@
 block discarded – undo
23 23
  */
24 24
 class JobController extends Controller {
25 25
 
26
-    /**
27
-     *
28
-     * @var string
29
-     */
30
-    private $userId;
26
+	/**
27
+	 *
28
+	 * @var string
29
+	 */
30
+	private $userId;
31 31
 
32
-    /**
33
-     *
34
-     * @var OcrService
35
-     */
36
-    private $service;
37
-    use Errors;
32
+	/**
33
+	 *
34
+	 * @var OcrService
35
+	 */
36
+	private $service;
37
+	use Errors;
38 38
 
39
-    /**
40
-     * JobController constructor.
41
-     * 
42
-     * @param string $AppName            
43
-     * @param IRequest $request            
44
-     * @param JobService $service            
45
-     * @param
46
-     *            $UserId
47
-     */
48
-    public function __construct($AppName, IRequest $request, JobService $service, $UserId) {
49
-        parent::__construct($AppName, $request);
50
-        $this->userId = $UserId;
51
-        $this->service = $service;
52
-    }
39
+	/**
40
+	 * JobController constructor.
41
+	 * 
42
+	 * @param string $AppName            
43
+	 * @param IRequest $request            
44
+	 * @param JobService $service            
45
+	 * @param
46
+	 *            $UserId
47
+	 */
48
+	public function __construct($AppName, IRequest $request, JobService $service, $UserId) {
49
+		parent::__construct($AppName, $request);
50
+		$this->userId = $UserId;
51
+		$this->service = $service;
52
+	}
53 53
 
54
-    /**
55
-     * Processing the srcFile(s)
56
-     * @NoAdminRequired
57
-     * 
58
-     * @param string[] $languages
59
-     *            - deu, eng...
60
-     * @param array $files            
61
-     * @return DataResponse
62
-     */
63
-    public function process($languages, $files) {
64
-        return $this->handleNotFound(
65
-                function () use ($languages, $files) {
66
-                    return $this->service->process($languages, $files);
67
-                });
68
-    }
54
+	/**
55
+	 * Processing the srcFile(s)
56
+	 * @NoAdminRequired
57
+	 * 
58
+	 * @param string[] $languages
59
+	 *            - deu, eng...
60
+	 * @param array $files            
61
+	 * @return DataResponse
62
+	 */
63
+	public function process($languages, $files) {
64
+		return $this->handleNotFound(
65
+				function () use ($languages, $files) {
66
+					return $this->service->process($languages, $files);
67
+				});
68
+	}
69 69
 
70
-    /**
71
-     * @NoAdminRequired
72
-     * 
73
-     * @return \OCP\AppFramework\Http\DataResponse
74
-     */
75
-    public function getAllJobs() {
76
-        return $this->handleNotFound(function () {
77
-            return $this->service->getAllJobsForUser($this->userId);
78
-        });
79
-    }
70
+	/**
71
+	 * @NoAdminRequired
72
+	 * 
73
+	 * @return \OCP\AppFramework\Http\DataResponse
74
+	 */
75
+	public function getAllJobs() {
76
+		return $this->handleNotFound(function () {
77
+			return $this->service->getAllJobsForUser($this->userId);
78
+		});
79
+	}
80 80
 
81
-    /**
82
-     * @NoAdminRequired
83
-     * 
84
-     * @param integer $id            
85
-     * @return \OCP\AppFramework\Http\DataResponse
86
-     */
87
-    public function deleteJob($id) {
88
-        return $this->handleNotFound(
89
-                function () use ($id) {
90
-                    return $this->service->deleteJob($id, $this->userId);
91
-                });
92
-    }
81
+	/**
82
+	 * @NoAdminRequired
83
+	 * 
84
+	 * @param integer $id            
85
+	 * @return \OCP\AppFramework\Http\DataResponse
86
+	 */
87
+	public function deleteJob($id) {
88
+		return $this->handleNotFound(
89
+				function () use ($id) {
90
+					return $this->service->deleteJob($id, $this->userId);
91
+				});
92
+	}
93 93
 }
94 94
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Controller/StatusController.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -17,34 +17,34 @@
 block discarded – undo
17 17
 
18 18
 class StatusController extends Controller {
19 19
 
20
-    /**
21
-     *
22
-     * @var OcrService
23
-     */
24
-    private $service;
25
-    use Errors;
20
+	/**
21
+	 *
22
+	 * @var OcrService
23
+	 */
24
+	private $service;
25
+	use Errors;
26 26
 
27
-    /**
28
-     * StatusController constructor.
29
-     * 
30
-     * @param string $AppName            
31
-     * @param IRequest $request            
32
-     * @param StatusService $service            
33
-     */
34
-    public function __construct($AppName, IRequest $request, StatusService $service) {
35
-        parent::__construct($AppName, $request);
36
-        $this->service = $service;
37
-    }
27
+	/**
28
+	 * StatusController constructor.
29
+	 * 
30
+	 * @param string $AppName            
31
+	 * @param IRequest $request            
32
+	 * @param StatusService $service            
33
+	 */
34
+	public function __construct($AppName, IRequest $request, StatusService $service) {
35
+		parent::__construct($AppName, $request);
36
+		$this->service = $service;
37
+	}
38 38
 
39
-    /**
40
-     * Get the current status.
41
-     * @NoAdminRequired
42
-     * 
43
-     * @return DataResponse
44
-     */
45
-    public function getStatus() {
46
-        return $this->handleNotFound(function () {
47
-            return $this->service->getStatus();
48
-        });
49
-    }
39
+	/**
40
+	 * Get the current status.
41
+	 * @NoAdminRequired
42
+	 * 
43
+	 * @return DataResponse
44
+	 */
45
+	public function getStatus() {
46
+		return $this->handleNotFound(function () {
47
+			return $this->service->getStatus();
48
+		});
49
+	}
50 50
 }
Please login to merge, or discard this patch.
lib/Service/JobService.php 1 patch
Indentation   +354 added lines, -354 removed lines patch added patch discarded remove patch
@@ -29,380 +29,380 @@
 block discarded – undo
29 29
  */
30 30
 class JobService {
31 31
 
32
-    /**
33
-     *
34
-     * @var ILogger
35
-     */
36
-    private $logger;
32
+	/**
33
+	 *
34
+	 * @var ILogger
35
+	 */
36
+	private $logger;
37 37
 
38
-    /**
39
-     *
40
-     * @var RedisService
41
-     */
42
-    private $redisService;
38
+	/**
39
+	 *
40
+	 * @var RedisService
41
+	 */
42
+	private $redisService;
43 43
 
44
-    /**
45
-     *
46
-     * @var OcrJobMapper
47
-     */
48
-    private $jobMapper;
44
+	/**
45
+	 *
46
+	 * @var OcrJobMapper
47
+	 */
48
+	private $jobMapper;
49 49
 
50
-    /**
51
-     *
52
-     * @var View
53
-     */
54
-    private $view;
50
+	/**
51
+	 *
52
+	 * @var View
53
+	 */
54
+	private $view;
55 55
 
56
-    /**
57
-     *
58
-     * @var String
59
-     */
60
-    private $userId;
56
+	/**
57
+	 *
58
+	 * @var String
59
+	 */
60
+	private $userId;
61 61
 
62
-    /**
63
-     *
64
-     * @var IL10N
65
-     */
66
-    private $l10n;
62
+	/**
63
+	 *
64
+	 * @var IL10N
65
+	 */
66
+	private $l10n;
67 67
 
68
-    /**
69
-     *
70
-     * @var FileService
71
-     */
72
-    private $fileService;
68
+	/**
69
+	 *
70
+	 * @var FileService
71
+	 */
72
+	private $fileService;
73 73
 
74
-    /**
75
-     *
76
-     * @var ITempManager
77
-     */
78
-    private $tempM;
74
+	/**
75
+	 *
76
+	 * @var ITempManager
77
+	 */
78
+	private $tempM;
79 79
 
80
-    /**
81
-     *
82
-     * @var AppConfigService
83
-     */
84
-    private $appConfigService;
80
+	/**
81
+	 *
82
+	 * @var AppConfigService
83
+	 */
84
+	private $appConfigService;
85 85
 
86
-    /**
87
-     *
88
-     * @var PHPUtil
89
-     */
90
-    private $phpUtil;
86
+	/**
87
+	 *
88
+	 * @var PHPUtil
89
+	 */
90
+	private $phpUtil;
91 91
 
92
-    /**
93
-     * JobService constructor.
94
-     * 
95
-     * @param IL10N $l10n            
96
-     * @param ILogger $logger            
97
-     * @param string $userId            
98
-     * @param View $view            
99
-     * @param RedisService $queueService            
100
-     * @param OcrJobMapper $mapper            
101
-     * @param FileService $fileService            
102
-     * @param AppConfigService $appConfigService            
103
-     * @param PHPUtil $phpUtil            
104
-     */
105
-    public function __construct(IL10N $l10n, ILogger $logger, $userId, View $view, ITempManager $tempManager, 
106
-            RedisService $queueService, OcrJobMapper $mapper, FileService $fileService, 
107
-            AppConfigService $appConfigService, PHPUtil $phpUtil) {
108
-        $this->logger = $logger;
109
-        $this->redisService = $queueService;
110
-        $this->jobMapper = $mapper;
111
-        $this->view = $view;
112
-        $this->userId = $userId;
113
-        $this->l10n = $l10n;
114
-        $this->fileService = $fileService;
115
-        $this->tempM = $tempManager;
116
-        $this->appConfigService = $appConfigService;
117
-        $this->phpUtil = $phpUtil;
118
-    }
92
+	/**
93
+	 * JobService constructor.
94
+	 * 
95
+	 * @param IL10N $l10n            
96
+	 * @param ILogger $logger            
97
+	 * @param string $userId            
98
+	 * @param View $view            
99
+	 * @param RedisService $queueService            
100
+	 * @param OcrJobMapper $mapper            
101
+	 * @param FileService $fileService            
102
+	 * @param AppConfigService $appConfigService            
103
+	 * @param PHPUtil $phpUtil            
104
+	 */
105
+	public function __construct(IL10N $l10n, ILogger $logger, $userId, View $view, ITempManager $tempManager, 
106
+			RedisService $queueService, OcrJobMapper $mapper, FileService $fileService, 
107
+			AppConfigService $appConfigService, PHPUtil $phpUtil) {
108
+		$this->logger = $logger;
109
+		$this->redisService = $queueService;
110
+		$this->jobMapper = $mapper;
111
+		$this->view = $view;
112
+		$this->userId = $userId;
113
+		$this->l10n = $l10n;
114
+		$this->fileService = $fileService;
115
+		$this->tempM = $tempManager;
116
+		$this->appConfigService = $appConfigService;
117
+		$this->phpUtil = $phpUtil;
118
+	}
119 119
 
120
-    /**
121
-     * Processes and prepares the files for OCR.
122
-     * Sends the stuff to the client in order to OCR async.
123
-     * 
124
-     * @param string[] $languages            
125
-     * @param array $files            
126
-     * @return string
127
-     */
128
-    public function process($languages, $files) {
129
-        try {
130
-            $this->logger->debug('Will now process files: {files} with languages: {languages}', 
131
-                    [
132
-                            'files' => json_encode($files),
133
-                            'languages' => json_encode($languages)
134
-                    ]);
135
-            // Check if files and language not empty
136
-            $noLang = $this->noLanguage($languages);
137
-            if (!empty($files) && ($this->checkForAcceptedLanguages($languages) || $noLang)) {
138
-                // language part:
139
-                if ($noLang) {
140
-                    $languages = [];
141
-                }
142
-                // file part:
143
-                $fileInfo = $this->fileService->buildFileInfo($files);
144
-                foreach ($fileInfo as $fInfo) {
145
-                    // Check Shared
146
-                    $shared = $this->fileService->checkSharedWithInitiator($fInfo);
147
-                    $target = $this->fileService->buildTarget($fInfo, $shared);
148
-                    $source = $this->fileService->buildSource($fInfo, $shared);
149
-                    // set the running type
150
-                    $fType = $this->fileService->getCorrectType($fInfo);
151
-                    // create a temp file for ocr processing purposes
152
-                    $tempFile = $this->getTempFile($fType);
153
-                    // Create job object
154
-                    $job = new OcrJob(OcrConstants::STATUS_PENDING, $source, $target, $tempFile, $fType, $this->userId, 
155
-                            false, $fInfo->getName(), null);
156
-                    // Init client and send task / job
157
-                    // Feed the worker
158
-                    $this->redisService->sendJob($job, $languages, \OC::$SERVERROOT);
159
-                }
160
-                return 'PROCESSING';
161
-            } else {
162
-                throw new NotFoundException($this->l10n->t('Empty parameters passed.'));
163
-            }
164
-        } catch (Exception $e) {
165
-            $this->handleException($e);
166
-        }
167
-    }
120
+	/**
121
+	 * Processes and prepares the files for OCR.
122
+	 * Sends the stuff to the client in order to OCR async.
123
+	 * 
124
+	 * @param string[] $languages            
125
+	 * @param array $files            
126
+	 * @return string
127
+	 */
128
+	public function process($languages, $files) {
129
+		try {
130
+			$this->logger->debug('Will now process files: {files} with languages: {languages}', 
131
+					[
132
+							'files' => json_encode($files),
133
+							'languages' => json_encode($languages)
134
+					]);
135
+			// Check if files and language not empty
136
+			$noLang = $this->noLanguage($languages);
137
+			if (!empty($files) && ($this->checkForAcceptedLanguages($languages) || $noLang)) {
138
+				// language part:
139
+				if ($noLang) {
140
+					$languages = [];
141
+				}
142
+				// file part:
143
+				$fileInfo = $this->fileService->buildFileInfo($files);
144
+				foreach ($fileInfo as $fInfo) {
145
+					// Check Shared
146
+					$shared = $this->fileService->checkSharedWithInitiator($fInfo);
147
+					$target = $this->fileService->buildTarget($fInfo, $shared);
148
+					$source = $this->fileService->buildSource($fInfo, $shared);
149
+					// set the running type
150
+					$fType = $this->fileService->getCorrectType($fInfo);
151
+					// create a temp file for ocr processing purposes
152
+					$tempFile = $this->getTempFile($fType);
153
+					// Create job object
154
+					$job = new OcrJob(OcrConstants::STATUS_PENDING, $source, $target, $tempFile, $fType, $this->userId, 
155
+							false, $fInfo->getName(), null);
156
+					// Init client and send task / job
157
+					// Feed the worker
158
+					$this->redisService->sendJob($job, $languages, \OC::$SERVERROOT);
159
+				}
160
+				return 'PROCESSING';
161
+			} else {
162
+				throw new NotFoundException($this->l10n->t('Empty parameters passed.'));
163
+			}
164
+		} catch (Exception $e) {
165
+			$this->handleException($e);
166
+		}
167
+	}
168 168
 
169
-    /**
170
-     * Delete an ocr job for a given id and userId.
171
-     * 
172
-     * @param
173
-     *            $jobId
174
-     * @param string $userId            
175
-     * @return OcrJob
176
-     */
177
-    public function deleteJob($jobId, $userId) {
178
-        try {
179
-            $job = $this->jobMapper->find($jobId);
180
-            if ($job->getUserId() !== $userId) {
181
-                throw new NotFoundException($this->l10n->t('Cannot delete. Wrong owner.'));
182
-            } else {
183
-                $job = $this->jobMapper->delete($job);
184
-            }
185
-            $job->setTarget(null);
186
-            $job->setSource(null);
187
-            $job->setStatus(null);
188
-            $job->setTempFile(null);
189
-            $job->setType(null);
190
-            $job->setUserId(null);
191
-            $job->setErrorDisplayed(null);
192
-            return $job;
193
-        } catch (Exception $e) {
194
-            if ($e instanceof DoesNotExistException) {
195
-                $ex = new NotFoundException($this->l10n->t('Cannot delete. Wrong ID.'));
196
-                $this->handleException($ex);
197
-            } else {
198
-                $this->handleException($e);
199
-            }
200
-        }
201
-    }
169
+	/**
170
+	 * Delete an ocr job for a given id and userId.
171
+	 * 
172
+	 * @param
173
+	 *            $jobId
174
+	 * @param string $userId            
175
+	 * @return OcrJob
176
+	 */
177
+	public function deleteJob($jobId, $userId) {
178
+		try {
179
+			$job = $this->jobMapper->find($jobId);
180
+			if ($job->getUserId() !== $userId) {
181
+				throw new NotFoundException($this->l10n->t('Cannot delete. Wrong owner.'));
182
+			} else {
183
+				$job = $this->jobMapper->delete($job);
184
+			}
185
+			$job->setTarget(null);
186
+			$job->setSource(null);
187
+			$job->setStatus(null);
188
+			$job->setTempFile(null);
189
+			$job->setType(null);
190
+			$job->setUserId(null);
191
+			$job->setErrorDisplayed(null);
192
+			return $job;
193
+		} catch (Exception $e) {
194
+			if ($e instanceof DoesNotExistException) {
195
+				$ex = new NotFoundException($this->l10n->t('Cannot delete. Wrong ID.'));
196
+				$this->handleException($ex);
197
+			} else {
198
+				$this->handleException($e);
199
+			}
200
+		}
201
+	}
202 202
 
203
-    /**
204
-     * Gets all job objects for a specific user.
205
-     * 
206
-     * @param string $userId            
207
-     * @return array
208
-     */
209
-    public function getAllJobsForUser($userId) {
210
-        try {
211
-            $jobs = $this->jobMapper->findAll($userId);
212
-            $jobsNew = array();
213
-            foreach ($jobs as $job) {
214
-                $job->setTarget(null);
215
-                $job->setSource(null);
216
-                $job->setTempFile(null);
217
-                $job->setType(null);
218
-                $job->setUserId(null);
219
-                $job->setErrorDisplayed(null);
220
-                array_push($jobsNew, $job);
221
-            }
222
-            return $jobsNew;
223
-        } catch (Exception $e) {
224
-            $this->handleException($e);
225
-        }
226
-    }
203
+	/**
204
+	 * Gets all job objects for a specific user.
205
+	 * 
206
+	 * @param string $userId            
207
+	 * @return array
208
+	 */
209
+	public function getAllJobsForUser($userId) {
210
+		try {
211
+			$jobs = $this->jobMapper->findAll($userId);
212
+			$jobsNew = array();
213
+			foreach ($jobs as $job) {
214
+				$job->setTarget(null);
215
+				$job->setSource(null);
216
+				$job->setTempFile(null);
217
+				$job->setType(null);
218
+				$job->setUserId(null);
219
+				$job->setErrorDisplayed(null);
220
+				array_push($jobsNew, $job);
221
+			}
222
+			return $jobsNew;
223
+		} catch (Exception $e) {
224
+			$this->handleException($e);
225
+		}
226
+	}
227 227
 
228
-    /**
229
-     * The function checks if there are finished jobs to process finally.
230
-     * 
231
-     * @throws NotFoundException
232
-     */
233
-    public function checkForFinishedJobs() {
234
-        try {
235
-            $finishedJobs = $this->redisService->readingFinishedJobs();
236
-            foreach ($finishedJobs as $finishedJob) {
237
-                $fJob = $this->transformJob($finishedJob);
238
-                $this->logger->debug('The following job finished: {job}', 
239
-                        [
240
-                                'job' => $fJob
241
-                        ]);
242
-                $this->jobFinished($fJob->id, $fJob->error, $fJob->log);
243
-            }
244
-        } catch (Exception $e) {
245
-            throw new NotFoundException($this->l10n->t('Reading the finished jobs from redis went wrong.'));
246
-        }
247
-    }
228
+	/**
229
+	 * The function checks if there are finished jobs to process finally.
230
+	 * 
231
+	 * @throws NotFoundException
232
+	 */
233
+	public function checkForFinishedJobs() {
234
+		try {
235
+			$finishedJobs = $this->redisService->readingFinishedJobs();
236
+			foreach ($finishedJobs as $finishedJob) {
237
+				$fJob = $this->transformJob($finishedJob);
238
+				$this->logger->debug('The following job finished: {job}', 
239
+						[
240
+								'job' => $fJob
241
+						]);
242
+				$this->jobFinished($fJob->id, $fJob->error, $fJob->log);
243
+			}
244
+		} catch (Exception $e) {
245
+			throw new NotFoundException($this->l10n->t('Reading the finished jobs from redis went wrong.'));
246
+		}
247
+	}
248 248
 
249
-    /**
250
-     * The function the worker will call in order to set the jobs status.
251
-     * The worker should call it automatically after each processing step.
252
-     * 
253
-     * @param integer $jobId            
254
-     * @param boolean $error            
255
-     * @param string $log            
256
-     */
257
-    public function jobFinished($jobId, $error, $log) {
258
-        try {
259
-            $job = $this->jobMapper->find($jobId);
260
-            if (!$error) {
261
-                $job->setStatus(OcrConstants::STATUS_PROCESSED);
262
-                $this->jobMapper->update($job);
263
-            } else {
264
-                $job->setStatus(OcrConstants::STATUS_FAILED);
265
-                $job->setErrorLog($log);
266
-                $this->jobMapper->update($job);
267
-                $this->logger->error($log);
268
-            }
269
-        } catch (Exception $e) {
270
-            $this->handleException($e);
271
-        }
272
-    }
249
+	/**
250
+	 * The function the worker will call in order to set the jobs status.
251
+	 * The worker should call it automatically after each processing step.
252
+	 * 
253
+	 * @param integer $jobId            
254
+	 * @param boolean $error            
255
+	 * @param string $log            
256
+	 */
257
+	public function jobFinished($jobId, $error, $log) {
258
+		try {
259
+			$job = $this->jobMapper->find($jobId);
260
+			if (!$error) {
261
+				$job->setStatus(OcrConstants::STATUS_PROCESSED);
262
+				$this->jobMapper->update($job);
263
+			} else {
264
+				$job->setStatus(OcrConstants::STATUS_FAILED);
265
+				$job->setErrorLog($log);
266
+				$this->jobMapper->update($job);
267
+				$this->logger->error($log);
268
+			}
269
+		} catch (Exception $e) {
270
+			$this->handleException($e);
271
+		}
272
+	}
273 273
 
274
-    /**
275
-     * Finishes all Processed files by copying them to the right path and deleteing the temp files.
276
-     * Returns the number of processed files.
277
-     * 
278
-     * @return array
279
-     */
280
-    public function handleProcessed() {
281
-        try {
282
-            $this->logger->debug('Check if files were processed by ocr and if so, put them to the right dirs.');
283
-            $processed = $this->jobMapper->findAllProcessed($this->userId);
284
-            foreach ($processed as $job) {
285
-                if ($this->fileService->fileExists($job->getTempFile())) {
286
-                    // Save the tmp file with newname
287
-                    $this->view->file_put_contents($job->getTarget(), 
288
-                            $this->fileService->getFileContents($job->getTempFile()));
289
-                    $this->jobMapper->delete($job);
290
-                    $this->fileService->execRemove($job->getTempFile());
291
-                } else {
292
-                    $job->setStatus(OcrConstants::STATUS_FAILED);
293
-                    $job->setErrorLog('Temp file does not exist.');
294
-                    $this->jobMapper->update($job);
295
-                    throw new NotFoundException($this->l10n->t('Temp file does not exist.'));
296
-                }
297
-            }
298
-            return $processed;
299
-        } catch (Exception $e) {
300
-            $this->handleException($e);
301
-        }
302
-    }
274
+	/**
275
+	 * Finishes all Processed files by copying them to the right path and deleteing the temp files.
276
+	 * Returns the number of processed files.
277
+	 * 
278
+	 * @return array
279
+	 */
280
+	public function handleProcessed() {
281
+		try {
282
+			$this->logger->debug('Check if files were processed by ocr and if so, put them to the right dirs.');
283
+			$processed = $this->jobMapper->findAllProcessed($this->userId);
284
+			foreach ($processed as $job) {
285
+				if ($this->fileService->fileExists($job->getTempFile())) {
286
+					// Save the tmp file with newname
287
+					$this->view->file_put_contents($job->getTarget(), 
288
+							$this->fileService->getFileContents($job->getTempFile()));
289
+					$this->jobMapper->delete($job);
290
+					$this->fileService->execRemove($job->getTempFile());
291
+				} else {
292
+					$job->setStatus(OcrConstants::STATUS_FAILED);
293
+					$job->setErrorLog('Temp file does not exist.');
294
+					$this->jobMapper->update($job);
295
+					throw new NotFoundException($this->l10n->t('Temp file does not exist.'));
296
+				}
297
+			}
298
+			return $processed;
299
+		} catch (Exception $e) {
300
+			$this->handleException($e);
301
+		}
302
+	}
303 303
 
304
-    /**
305
-     * Handles all failed orders of ocr processing queue and returns the job objects.
306
-     * 
307
-     * @return array
308
-     */
309
-    public function handleFailed() {
310
-        try {
311
-            $failed = $this->jobMapper->findAllFailed($this->userId);
312
-            foreach ($failed as $job) {
313
-                // clean the tempfile
314
-                $this->fileService->execRemove($job->getTempFile());
315
-                // set error displayed
316
-                $job->setErrorDisplayed(true);
317
-                $this->jobMapper->update($job);
318
-            }
319
-            $this->logger->debug('Following jobs failed: {failed}', 
320
-                    [
321
-                            'failed' => json_encode($failed)
322
-                    ]);
323
-            return $failed;
324
-        } catch (Exception $e) {
325
-            $this->handleException($e);
326
-        }
327
-    }
304
+	/**
305
+	 * Handles all failed orders of ocr processing queue and returns the job objects.
306
+	 * 
307
+	 * @return array
308
+	 */
309
+	public function handleFailed() {
310
+		try {
311
+			$failed = $this->jobMapper->findAllFailed($this->userId);
312
+			foreach ($failed as $job) {
313
+				// clean the tempfile
314
+				$this->fileService->execRemove($job->getTempFile());
315
+				// set error displayed
316
+				$job->setErrorDisplayed(true);
317
+				$this->jobMapper->update($job);
318
+			}
319
+			$this->logger->debug('Following jobs failed: {failed}', 
320
+					[
321
+							'failed' => json_encode($failed)
322
+					]);
323
+			return $failed;
324
+		} catch (Exception $e) {
325
+			$this->handleException($e);
326
+		}
327
+	}
328 328
 
329
-    /**
330
-     * Gives a temp file name back depending on the type of the OCR.
331
-     * Later in the worker this file is used as an output.
332
-     * 
333
-     * @param integer $type            
334
-     * @return string
335
-     */
336
-    private function getTempFile($type) {
337
-        if ($type === OcrConstants::TESSERACT) {
338
-            $fileName = $this->phpUtil->tempnamWrapper($this->tempM->getTempBaseDir(), OcrConstants::TEMPFILE_PREFIX);
339
-            $this->phpUtil->unlinkWrapper($fileName);
340
-            $fileNameWithPostfix = $fileName . '.txt';
341
-            $this->phpUtil->touchWrapper($fileNameWithPostfix);
342
-            $this->phpUtil->chmodWrapper($fileNameWithPostfix, 0600);
343
-            return $fileNameWithPostfix;
344
-        } else {
345
-            return $this->phpUtil->tempnamWrapper($this->tempM->getTempBaseDir(), 'ocr_');
346
-        }
347
-    }
329
+	/**
330
+	 * Gives a temp file name back depending on the type of the OCR.
331
+	 * Later in the worker this file is used as an output.
332
+	 * 
333
+	 * @param integer $type            
334
+	 * @return string
335
+	 */
336
+	private function getTempFile($type) {
337
+		if ($type === OcrConstants::TESSERACT) {
338
+			$fileName = $this->phpUtil->tempnamWrapper($this->tempM->getTempBaseDir(), OcrConstants::TEMPFILE_PREFIX);
339
+			$this->phpUtil->unlinkWrapper($fileName);
340
+			$fileNameWithPostfix = $fileName . '.txt';
341
+			$this->phpUtil->touchWrapper($fileNameWithPostfix);
342
+			$this->phpUtil->chmodWrapper($fileNameWithPostfix, 0600);
343
+			return $fileNameWithPostfix;
344
+		} else {
345
+			return $this->phpUtil->tempnamWrapper($this->tempM->getTempBaseDir(), 'ocr_');
346
+		}
347
+	}
348 348
 
349
-    /**
350
-     * Takes care of transforming an incoming finished job into a php readable object.
351
-     * 
352
-     * @param string $job            
353
-     * @throws NotFoundException
354
-     * @return mixed
355
-     */
356
-    private function transformJob($job) {
357
-        $decoded = json_decode($job);
358
-        if ($decoded !== null && isset($decoded->id)) {
359
-            return $decoded;
360
-        } else {
361
-            throw new NotFoundException($this->l10n->t('The finished job retrieved by Redis was corrupt.'));
362
-        }
363
-    }
349
+	/**
350
+	 * Takes care of transforming an incoming finished job into a php readable object.
351
+	 * 
352
+	 * @param string $job            
353
+	 * @throws NotFoundException
354
+	 * @return mixed
355
+	 */
356
+	private function transformJob($job) {
357
+		$decoded = json_decode($job);
358
+		if ($decoded !== null && isset($decoded->id)) {
359
+			return $decoded;
360
+		} else {
361
+			throw new NotFoundException($this->l10n->t('The finished job retrieved by Redis was corrupt.'));
362
+		}
363
+	}
364 364
 
365
-    /**
366
-     * Checks if the given languages are supported or not.
367
-     * 
368
-     * @param string[] $languages            
369
-     * @return boolean
370
-     */
371
-    private function checkForAcceptedLanguages($languages) {
372
-        $installedLanguages = explode(';', $this->appConfigService->getAppValue('languages'));
373
-        if (count(array_diff($languages, $installedLanguages)) === 0) {
374
-            return true;
375
-        } else {
376
-            return false;
377
-        }
378
-    }
365
+	/**
366
+	 * Checks if the given languages are supported or not.
367
+	 * 
368
+	 * @param string[] $languages            
369
+	 * @return boolean
370
+	 */
371
+	private function checkForAcceptedLanguages($languages) {
372
+		$installedLanguages = explode(';', $this->appConfigService->getAppValue('languages'));
373
+		if (count(array_diff($languages, $installedLanguages)) === 0) {
374
+			return true;
375
+		} else {
376
+			return false;
377
+		}
378
+	}
379 379
 
380
-    /**
381
-     * Checks if the process should be initiated without any language specified.
382
-     * 
383
-     * @param string[] $languages            
384
-     * @return boolean
385
-     */
386
-    private function noLanguage($languages) {
387
-        if (in_array('any', $languages)) {
388
-            return true;
389
-        } else {
390
-            return false;
391
-        }
392
-    }
380
+	/**
381
+	 * Checks if the process should be initiated without any language specified.
382
+	 * 
383
+	 * @param string[] $languages            
384
+	 * @return boolean
385
+	 */
386
+	private function noLanguage($languages) {
387
+		if (in_array('any', $languages)) {
388
+			return true;
389
+		} else {
390
+			return false;
391
+		}
392
+	}
393 393
 
394
-    /**
395
-     * Handle the possible thrown Exceptions from all methods of this class.
396
-     * 
397
-     * @param Exception $e            
398
-     * @throws Exception
399
-     * @throws NotFoundException
400
-     */
401
-    private function handleException($e) {
402
-        $this->logger->logException($e, 
403
-                [
404
-                        'message' => 'Exception during job service function processing'
405
-                ]);
406
-        throw $e;
407
-    }
394
+	/**
395
+	 * Handle the possible thrown Exceptions from all methods of this class.
396
+	 * 
397
+	 * @param Exception $e            
398
+	 * @throws Exception
399
+	 * @throws NotFoundException
400
+	 */
401
+	private function handleException($e) {
402
+		$this->logger->logException($e, 
403
+				[
404
+						'message' => 'Exception during job service function processing'
405
+				]);
406
+		throw $e;
407
+	}
408 408
 }
409 409
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Service/FileService.php 1 patch
Indentation   +251 added lines, -251 removed lines patch added patch discarded remove patch
@@ -25,272 +25,272 @@
 block discarded – undo
25 25
  */
26 26
 class FileService {
27 27
 
28
-    /**
29
-     *
30
-     * @var ILogger
31
-     */
32
-    private $logger;
28
+	/**
29
+	 *
30
+	 * @var ILogger
31
+	 */
32
+	private $logger;
33 33
 
34
-    /**
35
-     *
36
-     * @var FileMapper
37
-     */
38
-    private $fileMapper;
34
+	/**
35
+	 *
36
+	 * @var FileMapper
37
+	 */
38
+	private $fileMapper;
39 39
 
40
-    /**
41
-     *
42
-     * @var ShareMapper
43
-     */
44
-    private $shareMapper;
40
+	/**
41
+	 *
42
+	 * @var ShareMapper
43
+	 */
44
+	private $shareMapper;
45 45
 
46
-    /**
47
-     *
48
-     * @var string
49
-     */
50
-    private $userId;
46
+	/**
47
+	 *
48
+	 * @var string
49
+	 */
50
+	private $userId;
51 51
 
52
-    /**
53
-     *
54
-     * @var IL10N
55
-     */
56
-    private $l10n;
52
+	/**
53
+	 *
54
+	 * @var IL10N
55
+	 */
56
+	private $l10n;
57 57
 
58
-    public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper) {
59
-        $this->l10n = $l10n;
60
-        $this->logger = $logger;
61
-        $this->userId = $userId;
62
-        $this->fileMapper = $fileMapper;
63
-        $this->shareMapper = $shareMapper;
64
-    }
58
+	public function __construct(IL10N $l10n, ILogger $logger, $userId, FileMapper $fileMapper, ShareMapper $shareMapper) {
59
+		$this->l10n = $l10n;
60
+		$this->logger = $logger;
61
+		$this->userId = $userId;
62
+		$this->fileMapper = $fileMapper;
63
+		$this->shareMapper = $shareMapper;
64
+	}
65 65
 
66
-    /**
67
-     * Checks if shared with the process initiator
68
-     * 
69
-     * @param File $fileInfo            
70
-     * @return boolean|null
71
-     */
72
-    public function checkSharedWithInitiator($fileInfo) {
73
-        $owner = str_replace('home::', '', $fileInfo->getStoragename());
74
-        if ($this->userId === $owner) {
75
-            // user is owner (no shared file)
76
-            return false;
77
-        } else {
78
-            // user is not owner (shared file)
79
-            return true;
80
-        }
81
-    }
66
+	/**
67
+	 * Checks if shared with the process initiator
68
+	 * 
69
+	 * @param File $fileInfo            
70
+	 * @return boolean|null
71
+	 */
72
+	public function checkSharedWithInitiator($fileInfo) {
73
+		$owner = str_replace('home::', '', $fileInfo->getStoragename());
74
+		if ($this->userId === $owner) {
75
+			// user is owner (no shared file)
76
+			return false;
77
+		} else {
78
+			// user is not owner (shared file)
79
+			return true;
80
+		}
81
+	}
82 82
 
83
-    /**
84
-     * Builds the target name.
85
-     * 
86
-     * @param File $fileInfo            
87
-     * @param boolean $shared            
88
-     * @return string
89
-     */
90
-    public function buildTarget($fileInfo, $shared) {
91
-        if ($shared) {
92
-            $target = $this->buildTargetForShared($fileInfo);
93
-        } else {
94
-            $target = $this->buildTargetNotForShared($fileInfo);
95
-        }
96
-        return $target;
97
-    }
83
+	/**
84
+	 * Builds the target name.
85
+	 * 
86
+	 * @param File $fileInfo            
87
+	 * @param boolean $shared            
88
+	 * @return string
89
+	 */
90
+	public function buildTarget($fileInfo, $shared) {
91
+		if ($shared) {
92
+			$target = $this->buildTargetForShared($fileInfo);
93
+		} else {
94
+			$target = $this->buildTargetNotForShared($fileInfo);
95
+		}
96
+		return $target;
97
+	}
98 98
 
99
-    /**
100
-     * Builds the source name.
101
-     * 
102
-     * @param File $fileInfo            
103
-     * @param boolean $shared            
104
-     * @return string
105
-     */
106
-    public function buildSource($fileInfo, $shared) {
107
-        $source = $fileInfo->getPath();
108
-        if ($shared) {
109
-            $source = str_replace('home::', '', $fileInfo->getStoragename()) . '/' . $source;
110
-        } else {
111
-            $source = $this->userId . '/' . $source;
112
-        }
113
-        return $source;
114
-    }
99
+	/**
100
+	 * Builds the source name.
101
+	 * 
102
+	 * @param File $fileInfo            
103
+	 * @param boolean $shared            
104
+	 * @return string
105
+	 */
106
+	public function buildSource($fileInfo, $shared) {
107
+		$source = $fileInfo->getPath();
108
+		if ($shared) {
109
+			$source = str_replace('home::', '', $fileInfo->getStoragename()) . '/' . $source;
110
+		} else {
111
+			$source = $this->userId . '/' . $source;
112
+		}
113
+		return $source;
114
+	}
115 115
 
116
-    /**
117
-     * Returns the fileInfo for each file in files and checks
118
-     * if it has a allowed MIME type and some other conditions.
119
-     * 
120
-     * @param array $files            
121
-     * @return File[]
122
-     * @throws NotFoundException
123
-     */
124
-    public function buildFileInfo($files) {
125
-        $fileArray = array();
126
-        foreach ($files as $file) {
127
-            // Check if anything is missing and file type is correct
128
-            if (!empty($file['id'])) {
129
-                $fileInfo = $this->fileMapper->find($file['id']);
130
-                $this->checkMimeType($fileInfo);
131
-                array_push($fileArray, $fileInfo);
132
-            } else {
133
-                throw new NotFoundException($this->l10n->t('Wrong parameter.'));
134
-            }
135
-        }
136
-        return $fileArray;
137
-    }
116
+	/**
117
+	 * Returns the fileInfo for each file in files and checks
118
+	 * if it has a allowed MIME type and some other conditions.
119
+	 * 
120
+	 * @param array $files            
121
+	 * @return File[]
122
+	 * @throws NotFoundException
123
+	 */
124
+	public function buildFileInfo($files) {
125
+		$fileArray = array();
126
+		foreach ($files as $file) {
127
+			// Check if anything is missing and file type is correct
128
+			if (!empty($file['id'])) {
129
+				$fileInfo = $this->fileMapper->find($file['id']);
130
+				$this->checkMimeType($fileInfo);
131
+				array_push($fileArray, $fileInfo);
132
+			} else {
133
+				throw new NotFoundException($this->l10n->t('Wrong parameter.'));
134
+			}
135
+		}
136
+		return $fileArray;
137
+	}
138 138
 
139
-    /**
140
-     * Determines the correct type for the ocr process worker.
141
-     * 
142
-     * @param File $fileInfo            
143
-     * @return integer
144
-     */
145
-    public function getCorrectType($fileInfo) {
146
-        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
147
-            return OcrConstants::OCRmyPDF;
148
-        } else {
149
-            return OcrConstants::TESSERACT;
150
-        }
151
-    }
139
+	/**
140
+	 * Determines the correct type for the ocr process worker.
141
+	 * 
142
+	 * @param File $fileInfo            
143
+	 * @return integer
144
+	 */
145
+	public function getCorrectType($fileInfo) {
146
+		if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
147
+			return OcrConstants::OCRmyPDF;
148
+		} else {
149
+			return OcrConstants::TESSERACT;
150
+		}
151
+	}
152 152
 
153
-    /**
154
-     * Executes the exec function with a remove statement for a given file path.
155
-     * @codeCoverageIgnore
156
-     * 
157
-     * @param string $pathToFile            
158
-     */
159
-    public function execRemove($pathToFile) {
160
-        exec('rm ' . $pathToFile);
161
-    }
153
+	/**
154
+	 * Executes the exec function with a remove statement for a given file path.
155
+	 * @codeCoverageIgnore
156
+	 * 
157
+	 * @param string $pathToFile            
158
+	 */
159
+	public function execRemove($pathToFile) {
160
+		exec('rm ' . $pathToFile);
161
+	}
162 162
 
163
-    /**
164
-     * Wraps the static file_get_contents method of php.
165
-     * @codeCoverageIgnore
166
-     * 
167
-     * @param string $pathToFile            
168
-     * @return string
169
-     */
170
-    public function getFileContents($pathToFile) {
171
-        return file_get_contents($pathToFile);
172
-    }
163
+	/**
164
+	 * Wraps the static file_get_contents method of php.
165
+	 * @codeCoverageIgnore
166
+	 * 
167
+	 * @param string $pathToFile            
168
+	 * @return string
169
+	 */
170
+	public function getFileContents($pathToFile) {
171
+		return file_get_contents($pathToFile);
172
+	}
173 173
 
174
-    /**
175
-     * Wraps the static file_exists method of php.
176
-     * @codeCoverageIgnore
177
-     * 
178
-     * @param string $pathToFile            
179
-     * @return boolean
180
-     */
181
-    public function fileExists($pathToFile) {
182
-        return file_exists($pathToFile);
183
-    }
174
+	/**
175
+	 * Wraps the static file_exists method of php.
176
+	 * @codeCoverageIgnore
177
+	 * 
178
+	 * @param string $pathToFile            
179
+	 * @return boolean
180
+	 */
181
+	public function fileExists($pathToFile) {
182
+		return file_exists($pathToFile);
183
+	}
184 184
 
185
-    /**
186
-     * Wraps the static function \OCP\Files::buildNotExistingFileName() in order to be able to test everything else.
187
-     * @codeCoverageIgnore
188
-     * 
189
-     * @param string $filePath            
190
-     * @param string $fileName            
191
-     * @return string
192
-     */
193
-    public function buildNotExistingFilename($filePath, $fileName) {
194
-        return \OCP\Files::buildNotExistingFileName($filePath, $fileName . '_OCR.pdf');
195
-    }
185
+	/**
186
+	 * Wraps the static function \OCP\Files::buildNotExistingFileName() in order to be able to test everything else.
187
+	 * @codeCoverageIgnore
188
+	 * 
189
+	 * @param string $filePath            
190
+	 * @param string $fileName            
191
+	 * @return string
192
+	 */
193
+	public function buildNotExistingFilename($filePath, $fileName) {
194
+		return \OCP\Files::buildNotExistingFileName($filePath, $fileName . '_OCR.pdf');
195
+	}
196 196
 
197
-    /**
198
-     * Returns a not existing file name for pdf or image processing
199
-     * protected as of testing issues with static methods.
200
-     * (Actually
201
-     * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not
202
-     * static anymore
203
-     * @codeCoverageIgnore
204
-     * 
205
-     * @param File $fileInfo            
206
-     * @return string
207
-     */
208
-    private function buildTargetForShared(File $fileInfo) {
209
-        $share = $this->shareMapper->find($fileInfo->getFileid(), $this->userId, 
210
-                str_replace('home::', '', $fileInfo->getStoragename()));
211
-        // get rid of the .png or .pdf and so on
212
-        $fileName = substr($share->getFileTarget(), 0, (strrpos($share->getFileTarget(), '.'))); // '/thedom.png'
213
-                                                                                                         // => '/thedom'
214
-                                                                                                         // ||
215
-                                                                                                         // '/Test/thedom.png'
216
-                                                                                                         // =>
217
-                                                                                                         // '/Test/thedom'
218
-                                                                                                         // remove
219
-                                                                                                         // everything
220
-                                                                                                         // in front of
221
-                                                                                                         // and
222
-                                                                                                         // including of
223
-                                                                                                         // the first
224
-                                                                                                         // appearance
225
-                                                                                                         // of a slash
226
-                                                                                                         // from behind
227
-        $fileName = substr(strrchr($fileName, "/"), 1); // '/thedom' => 'thedom' || '/Test/thedom' => 'thedom'
228
-                                                              // eliminate the file name from the path
229
-        $filePath = dirname($share->getFileTarget()); // '/thedom.png' => '/' || '/Test/thedom.png' => '/Test'
230
-                                                          // replace the first slash
231
-        $pos = strpos($filePath, '/');
232
-        if ($pos !== false) {
233
-            $filePath = substr_replace($filePath, '', $pos, strlen('/')); // '/' => '' || '/Test/' => 'Test'
234
-        }
235
-        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
236
-            // PDFs:
237
-            return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.pdf');
238
-        } else {
239
-            // IMAGES:
240
-            return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.txt');
241
-        }
242
-    }
197
+	/**
198
+	 * Returns a not existing file name for pdf or image processing
199
+	 * protected as of testing issues with static methods.
200
+	 * (Actually
201
+	 * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not
202
+	 * static anymore
203
+	 * @codeCoverageIgnore
204
+	 * 
205
+	 * @param File $fileInfo            
206
+	 * @return string
207
+	 */
208
+	private function buildTargetForShared(File $fileInfo) {
209
+		$share = $this->shareMapper->find($fileInfo->getFileid(), $this->userId, 
210
+				str_replace('home::', '', $fileInfo->getStoragename()));
211
+		// get rid of the .png or .pdf and so on
212
+		$fileName = substr($share->getFileTarget(), 0, (strrpos($share->getFileTarget(), '.'))); // '/thedom.png'
213
+																										 // => '/thedom'
214
+																										 // ||
215
+																										 // '/Test/thedom.png'
216
+																										 // =>
217
+																										 // '/Test/thedom'
218
+																										 // remove
219
+																										 // everything
220
+																										 // in front of
221
+																										 // and
222
+																										 // including of
223
+																										 // the first
224
+																										 // appearance
225
+																										 // of a slash
226
+																										 // from behind
227
+		$fileName = substr(strrchr($fileName, "/"), 1); // '/thedom' => 'thedom' || '/Test/thedom' => 'thedom'
228
+															  // eliminate the file name from the path
229
+		$filePath = dirname($share->getFileTarget()); // '/thedom.png' => '/' || '/Test/thedom.png' => '/Test'
230
+														  // replace the first slash
231
+		$pos = strpos($filePath, '/');
232
+		if ($pos !== false) {
233
+			$filePath = substr_replace($filePath, '', $pos, strlen('/')); // '/' => '' || '/Test/' => 'Test'
234
+		}
235
+		if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
236
+			// PDFs:
237
+			return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.pdf');
238
+		} else {
239
+			// IMAGES:
240
+			return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.txt');
241
+		}
242
+	}
243 243
 
244
-    /**
245
-     * Returns a not existing file name for PDF or image processing
246
-     * protected as of testing issues with static methods.
247
-     * (Actually
248
-     * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not
249
-     * static anymore
250
-     * @codeCoverageIgnore
251
-     * 
252
-     * @param File $fileInfo            
253
-     * @return string
254
-     */
255
-    private function buildTargetNotForShared(File $fileInfo) {
256
-        // get rid of the .png or .pdf and so on
257
-        $fileName = substr($fileInfo->getName(), 0, (strrpos($fileInfo->getName(), '.'))); // 'thedom.png' =>
258
-                                                                                                   // 'thedom'
259
-                                                                                                   // eliminate the file
260
-                                                                                                   // name from the path
261
-        $filePath = str_replace($fileInfo->getName(), '', $fileInfo->getPath()); // 'files/Test/thedom.png' =>
262
-                                                                                      // 'files/Test/' ||
263
-                                                                                      // 'files/thedom.png' => 'files/'
264
-                                                                                      // and get the path on top of the
265
-                                                                                      // files/ dir
266
-        $filePath = str_replace('files', '', $filePath); // 'files/Test/' => '/Test/' || 'files/' => '/'
267
-                                                            // remove the last slash
268
-        $filePath = substr_replace($filePath, '', strrpos($filePath, '/'), strlen('/')); // '/Test/' => '/Test'
269
-                                                                                                  // || '/' => ''
270
-                                                                                                  // replace the first
271
-                                                                                                  // slash
272
-        $pos = strpos($filePath, '/');
273
-        if ($pos !== false) {
274
-            $filePath = substr_replace($filePath, '', $pos, strlen('/')); // '/Test' => '// 'Test' || '/' => ''
275
-        }
276
-        if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
277
-            // PDFs:
278
-            return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.pdf');
279
-        } else {
280
-            // IMAGES:
281
-            return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.txt');
282
-        }
283
-    }
244
+	/**
245
+	 * Returns a not existing file name for PDF or image processing
246
+	 * protected as of testing issues with static methods.
247
+	 * (Actually
248
+	 * it will be mocked partially) FIXME: Change this behaviour as soon as the buidlNotExistingFileName function is not
249
+	 * static anymore
250
+	 * @codeCoverageIgnore
251
+	 * 
252
+	 * @param File $fileInfo            
253
+	 * @return string
254
+	 */
255
+	private function buildTargetNotForShared(File $fileInfo) {
256
+		// get rid of the .png or .pdf and so on
257
+		$fileName = substr($fileInfo->getName(), 0, (strrpos($fileInfo->getName(), '.'))); // 'thedom.png' =>
258
+																								   // 'thedom'
259
+																								   // eliminate the file
260
+																								   // name from the path
261
+		$filePath = str_replace($fileInfo->getName(), '', $fileInfo->getPath()); // 'files/Test/thedom.png' =>
262
+																					  // 'files/Test/' ||
263
+																					  // 'files/thedom.png' => 'files/'
264
+																					  // and get the path on top of the
265
+																					  // files/ dir
266
+		$filePath = str_replace('files', '', $filePath); // 'files/Test/' => '/Test/' || 'files/' => '/'
267
+															// remove the last slash
268
+		$filePath = substr_replace($filePath, '', strrpos($filePath, '/'), strlen('/')); // '/Test/' => '/Test'
269
+																								  // || '/' => ''
270
+																								  // replace the first
271
+																								  // slash
272
+		$pos = strpos($filePath, '/');
273
+		if ($pos !== false) {
274
+			$filePath = substr_replace($filePath, '', $pos, strlen('/')); // '/Test' => '// 'Test' || '/' => ''
275
+		}
276
+		if ($fileInfo->getMimetype() === OcrConstants::MIME_TYPE_PDF) {
277
+			// PDFs:
278
+			return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.pdf');
279
+		} else {
280
+			// IMAGES:
281
+			return $this->buildNotExistingFilename($filePath, $fileName . '_OCR.txt');
282
+		}
283
+	}
284 284
 
285
-    /**
286
-     * Checks a MIME type for a specifically given FileInfo.
287
-     * 
288
-     * @param File $fileInfo            
289
-     */
290
-    private function checkMimeType(File $fileInfo) {
291
-        if (!$fileInfo || !in_array($fileInfo->getMimetype(), OcrConstants::ALLOWED_MIME_TYPES)) {
292
-            $this->logger->debug('Getting FileInfo did not work or not included in the ALLOWED_MIMETYPES array.');
293
-            throw new NotFoundException($this->l10n->t('Wrong MIME type.'));
294
-        }
295
-    }
285
+	/**
286
+	 * Checks a MIME type for a specifically given FileInfo.
287
+	 * 
288
+	 * @param File $fileInfo            
289
+	 */
290
+	private function checkMimeType(File $fileInfo) {
291
+		if (!$fileInfo || !in_array($fileInfo->getMimetype(), OcrConstants::ALLOWED_MIME_TYPES)) {
292
+			$this->logger->debug('Getting FileInfo did not work or not included in the ALLOWED_MIMETYPES array.');
293
+			throw new NotFoundException($this->l10n->t('Wrong MIME type.'));
294
+		}
295
+	}
296 296
 }
297 297
\ No newline at end of file
Please login to merge, or discard this patch.
lib/Service/AppConfigService.php 1 patch
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -17,93 +17,93 @@
 block discarded – undo
17 17
 
18 18
 class AppConfigService {
19 19
 
20
-    /**
21
-     *
22
-     * @var string
23
-     */
24
-    private $appName = 'ocr';
20
+	/**
21
+	 *
22
+	 * @var string
23
+	 */
24
+	private $appName = 'ocr';
25 25
 
26
-    /**
27
-     *
28
-     * @var IConfig
29
-     */
30
-    private $config;
26
+	/**
27
+	 *
28
+	 * @var IConfig
29
+	 */
30
+	private $config;
31 31
 
32
-    /**
33
-     *
34
-     * @var IL10N
35
-     */
36
-    private $l10n;
32
+	/**
33
+	 *
34
+	 * @var IL10N
35
+	 */
36
+	private $l10n;
37 37
 
38
-    /**
39
-     * Constructor
40
-     * 
41
-     * @param IConfig $config            
42
-     * @param IL10N $l10n            
43
-     */
44
-    public function __construct(IConfig $config, IL10N $l10n) {
45
-        $this->config = $config;
46
-        $this->l10n = $l10n;
47
-    }
38
+	/**
39
+	 * Constructor
40
+	 * 
41
+	 * @param IConfig $config            
42
+	 * @param IL10N $l10n            
43
+	 */
44
+	public function __construct(IConfig $config, IL10N $l10n) {
45
+		$this->config = $config;
46
+		$this->l10n = $l10n;
47
+	}
48 48
 
49
-    /**
50
-     * Get a value by key
51
-     * 
52
-     * @param string $key            
53
-     * @return string
54
-     */
55
-    public function getAppValue($key) {
56
-        return $this->config->getAppValue($this->appName, $key);
57
-    }
49
+	/**
50
+	 * Get a value by key
51
+	 * 
52
+	 * @param string $key            
53
+	 * @return string
54
+	 */
55
+	public function getAppValue($key) {
56
+		return $this->config->getAppValue($this->appName, $key);
57
+	}
58 58
 
59
-    /**
60
-     * Evaluate if all redis related settings are set.
61
-     * 
62
-     * @return boolean
63
-     */
64
-    public function evaluateRedisSettings() {
65
-        if ($this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_HOST) !== '' &&
66
-                 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_PORT) !== '' &&
67
-                 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_DB) !== '') {
68
-            return true;
69
-        } else {
70
-            return false;
71
-        }
72
-    }
59
+	/**
60
+	 * Evaluate if all redis related settings are set.
61
+	 * 
62
+	 * @return boolean
63
+	 */
64
+	public function evaluateRedisSettings() {
65
+		if ($this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_HOST) !== '' &&
66
+				 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_PORT) !== '' &&
67
+				 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_DB) !== '') {
68
+			return true;
69
+		} else {
70
+			return false;
71
+		}
72
+	}
73 73
 
74
-    /**
75
-     * Set a value by key
76
-     * 
77
-     * @param string $key            
78
-     * @param string $value            
79
-     * @return string
80
-     */
81
-    public function setAppValue($key, $value) {
82
-        if ($key === OcrConstants::LANGUAGES_CONFIG_KEY) {
83
-            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)) {
84
-                throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.'));
85
-            }
86
-        }
87
-        if ($key === OcrConstants::REDIS_CONFIG_KEY_HOST) {
88
-            if (!preg_match(
89
-                    '/(^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)/', 
90
-                    $value)) {
91
-                throw new NotFoundException($this->l10n->t('The redis host is not specified in the correct format.'));
92
-            }
93
-        }
94
-        if ($key === OcrConstants::REDIS_CONFIG_KEY_PORT) {
95
-            $value = intval($value);
96
-            if (!($value > 0 && $value < 65535)) {
97
-                throw new NotFoundException(
98
-                        $this->l10n->t('The redis port number is not specified in the correct format.'));
99
-            }
100
-        }
101
-        if ($key === OcrConstants::REDIS_CONFIG_KEY_DB) {
102
-            $value = intval($value);
103
-            if (!($value >= 0)) {
104
-                throw new NotFoundException($this->l10n->t('The redis db is not specified in the correct format.'));
105
-            }
106
-        }
107
-        return $this->config->setAppValue($this->appName, $key, $value);
108
-    }
74
+	/**
75
+	 * Set a value by key
76
+	 * 
77
+	 * @param string $key            
78
+	 * @param string $value            
79
+	 * @return string
80
+	 */
81
+	public function setAppValue($key, $value) {
82
+		if ($key === OcrConstants::LANGUAGES_CONFIG_KEY) {
83
+			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)) {
84
+				throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.'));
85
+			}
86
+		}
87
+		if ($key === OcrConstants::REDIS_CONFIG_KEY_HOST) {
88
+			if (!preg_match(
89
+					'/(^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)/', 
90
+					$value)) {
91
+				throw new NotFoundException($this->l10n->t('The redis host is not specified in the correct format.'));
92
+			}
93
+		}
94
+		if ($key === OcrConstants::REDIS_CONFIG_KEY_PORT) {
95
+			$value = intval($value);
96
+			if (!($value > 0 && $value < 65535)) {
97
+				throw new NotFoundException(
98
+						$this->l10n->t('The redis port number is not specified in the correct format.'));
99
+			}
100
+		}
101
+		if ($key === OcrConstants::REDIS_CONFIG_KEY_DB) {
102
+			$value = intval($value);
103
+			if (!($value >= 0)) {
104
+				throw new NotFoundException($this->l10n->t('The redis db is not specified in the correct format.'));
105
+			}
106
+		}
107
+		return $this->config->setAppValue($this->appName, $key, $value);
108
+	}
109 109
 }
110 110
\ No newline at end of file
Please login to merge, or discard this patch.