-
Notifications
You must be signed in to change notification settings - Fork 4
/
ServiceConnector.php
197 lines (164 loc) · 5.1 KB
/
ServiceConnector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace SilverStripe\DocumentConverter;
use CURLFile;
use SilverStripe\Assets\Folder;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\DataObject;
use ZipArchive;
/**
* Utility class hiding the specifics of the document conversion process.
*/
class ServiceConnector
{
use Configurable;
use Injectable;
/**
* @config
* @var array Docvert connection username
*/
private static $username = null;
/**
* @config
* @var array Docvert connection password
*/
private static $password = null;
/**
* @config
* @var array Docvert service URL
*/
private static $url = null;
/**
* Associative array of:
* - name: the full name of the file including the extension.
* - path: the path to the file on the local filesystem.
* - mimeType
*/
protected $fileDescriptor;
/**
* @var int
* ID of a SilverStripe\Assets\Folder
*/
protected $chosenFolderID;
/**
* @var array instance specific connection details
*/
protected $docvertDetails = [
'username' => null,
'password' => null,
'url' => null
];
public function __construct($fileDescriptor, $chosenFolderID = null)
{
$this->fileDescriptor = $fileDescriptor;
$this->chosenFolderID = $chosenFolderID;
}
/**
* Retrieves detail in priority order from
* 1. local instance field
* 2. Config
* 3. Environment
*
* @param string $detail key name for detail
* @return string the value for that key
*/
protected function getDetail($detail)
{
$fromDetails = $this->docvertDetails[$detail];
if ($fromDetails) {
return $fromDetails;
}
$fromConfig = $this->config()->get($detail);
if ($fromConfig) {
return $fromConfig;
}
$fromEnv = Environment::getEnv('DOCVERT_' . strtoupper($detail ?? ''));
if ($fromEnv) {
return $fromEnv;
}
}
public function setUsername($username = null)
{
$this->docvertDetails['username'] = $username;
return $this;
}
public function getUsername()
{
return $this->getDetail('username');
}
public function setPassword($password = null)
{
$this->docvertDetails['password'] = $password;
return $this;
}
public function getPassword()
{
return $this->getDetail('password');
}
public function setUrl($url = null)
{
$this->docvertDetails['url'] = $url;
return $this;
}
public function getUrl()
{
return $this->getDetail('url');
}
public function import()
{
$ch = curl_init();
$file = new CURLFile(
$this->fileDescriptor['path'],
$this->fileDescriptor['mimeType'],
$this->fileDescriptor['name']
);
curl_setopt_array($ch, [
CURLOPT_URL => $this->getUrl(),
CURLOPT_USERPWD => sprintf('%s:%s', $this->getUsername(), $this->getPassword()),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => ['file' => $file],
CURLOPT_CONNECTTIMEOUT => 25,
CURLOPT_TIMEOUT => 100,
]);
$chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null;
$folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : '';
$outname = tempnam(ASSETS_PATH, 'convert');
$outzip = $outname . '.zip';
$out = fopen($outzip ?? '', 'w');
curl_setopt($ch, CURLOPT_FILE, $out);
$returnValue = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($out);
chmod($outzip ?? '', 0666);
if (!$returnValue || ($status != 200)) {
return ['error' => _t(
__CLASS__ . '.SERVERUNREACHABLE',
'Could not contact document conversion server. Please try again later ' .
'or contact your system administrator.',
'Document Converter process Word documents into HTML.'
)];
}
// extract the converted document into assets
// you need php zip, e.g. apt-get install php-zip
$zip = new ZipArchive();
if ($zip->open($outzip)) {
$zip->extractTo(ASSETS_PATH .$folderName);
$zip->close();
}
// remove temporary files
unlink($outname ?? '');
unlink($outzip ?? '');
if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) {
return ['error' => _t(
__CLASS__ . '.PROCESSFAILED',
'Could not process document, please double-check you uploaded a .doc or .docx format.',
'Document Converter processes Word documents into HTML.'
)];
}
$content = file_get_contents(ASSETS_PATH . $folderName . '/index.html');
unlink(ASSETS_PATH . $folderName . '/index.html');
return $content;
}
}