1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nextcloud - OCR |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Janis Koehr <[email protected]> |
9
|
|
|
* @copyright Janis Koehr 2017 |
10
|
|
|
*/ |
11
|
|
|
namespace OCA\Ocr\Util; |
12
|
|
|
|
13
|
|
|
use OCA\Ocr\Service\NotFoundException; |
14
|
|
|
use OCP\IL10N; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class PHPUtil |
19
|
|
|
* |
20
|
|
|
* @package OCA\Ocr\Util |
21
|
|
|
*/ |
22
|
|
|
class PHPUtil { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var IL10N |
27
|
|
|
*/ |
28
|
|
|
private $l10n; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @param IL10N $l10n |
33
|
|
|
*/ |
34
|
5 |
|
public function __construct(IL10N $l10n) { |
35
|
5 |
|
$this->l10n = $l10n; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Wraps the php native function tempnam() |
40
|
|
|
* @codeCoverageIgnore |
41
|
|
|
* |
42
|
|
|
* @param string $dir |
43
|
|
|
* @param string $prefix |
44
|
|
|
* @throws NotFoundException |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function tempnamWrapper($dir, $prefix) { |
48
|
|
|
$tempFile = tempnam($dir, $prefix); |
49
|
|
|
if ($tempFile === false) { |
50
|
|
|
throw new NotFoundException($this->l10n->t('Temp file cannot be created.')); |
51
|
|
|
} else { |
52
|
|
|
return $tempFile; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Wraps the php native function unlink() |
58
|
|
|
* @codeCoverageIgnore |
59
|
|
|
* |
60
|
|
|
* @param string $fileName |
61
|
|
|
* @throws NotFoundException |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
|
|
public function unlinkWrapper($fileName) { |
65
|
|
|
if (unlink($fileName)) { |
66
|
|
|
return true; |
67
|
|
|
} else { |
68
|
|
|
throw new NotFoundException( |
69
|
|
|
$this->l10n->t('Cannot delete temporary file during creation of temp file for Tesseract.')); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Wraps the php native function touch() |
75
|
|
|
* @codeCoverageIgnore |
76
|
|
|
* |
77
|
|
|
* @param string $fileName |
78
|
|
|
* @throws NotFoundException |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
public function touchWrapper($fileName) { |
82
|
|
|
if (touch($fileName)) { |
83
|
|
|
return true; |
84
|
|
|
} else { |
85
|
|
|
throw new NotFoundException($this->l10n->t('Cannot create temporary file for Tesseract.')); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Wraps the php native function chmod() |
91
|
|
|
* @codeCoverageIgnore |
92
|
|
|
* |
93
|
|
|
* @param string $fileName |
94
|
|
|
* @param integer $mode |
95
|
|
|
* @throws NotFoundException |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
|
public function chmodWrapper($fileName, $mode) { |
99
|
|
|
if (chmod($fileName, $mode)) { |
100
|
|
|
return true; |
101
|
|
|
} else { |
102
|
|
|
throw new NotFoundException($this->l10n->t('Cannot set permissions for temporary Tesseract file.')); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|