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