-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlobObjectStorage.php
245 lines (224 loc) · 7.12 KB
/
BlobObjectStorage.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* @file
* Blob Object Storage Class
*/
declare(strict_types=1);
namespace Kanboard\Plugin\BlobStorage;
use Kanboard\Core\ObjectStorage\ObjectStorageException;
use Kanboard\Core\ObjectStorage\ObjectStorageInterface;
use Kanboard\Plugin\BlobStorage\Helper\BlobHelper;
use Dbp\Relay\BlobLibrary\Api\BlobApi;
use Dbp\Relay\BlobLibrary\Api\BlobApiError;
use GuzzleHttp\Exception\GuzzleException;
/**
* Blob Object Storage
*
* @category File-storage
* @package BlobObjectStorage
*/
class BlobObjectStorage implements ObjectStorageInterface
{
/**
* @var BlobApi
*/
private $blobApi;
/**
* @var string blobBaseUrl
*/
private $blobBaseUrl;
/**
* @var string blobBucketId
*/
private $blobBucketId;
/**
* @var string blobKey
*/
private $blobKey;
/**
* @var string oauthIDPUrl
*/
private $oauthIDPUrl;
/**
* @var string clientID
*/
private $clientID;
/**
* @var string clientSecret
*/
private $clientSecret;
/**
* Constructor
*
* @access public
*/
public function __construct(
string $blobKey,
string $blobBucketId,
string $blobBaseUrl,
string $oauthIDPUrl,
string $clientID,
string $clientSecret
) {
$this->blobBaseUrl = $blobBaseUrl;
$this->blobBucketId = $blobBucketId;
$this->blobKey = $blobKey;
$this->blobApi = new BlobApi($this->blobBaseUrl, $this->blobBucketId, $this->blobKey);
$this->oauthIDPUrl = $oauthIDPUrl;
$this->clientID = $clientID;
$this->clientSecret = $clientSecret;
try {
$this->blobApi->setOAuth2Token($oauthIDPUrl, $clientID, $clientSecret);
} catch (\JsonException $e) {
echo $e->getMessage() . "\n";
throw new BlobApiError('Something went wrong while decoding the json!', 'blob-library:get-token-json-error', ['message' => $e->getMessage()]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
echo $e->getMessage() . "\n";
throw new BlobApiError('Something went wrong in the request!', 'blob-library:get-token-request-error', ['message' => $e->getMessage()]);
}
}
/**
* Fetch file contents from Object Storage.
* Not in use...
*
* @access public
*
* @param string $prefix blob key
*
* @return string blob contents
*
* @throws ObjectStorageException
*/
public function get($prefix): string
{
try {
$fileData = $this->blobApi->getFileDataByPrefix($prefix);
if (is_array($fileData) && isset($fileData["hydra:member"][0]["contentUrl"])) {
return base64_decode(explode(',', $fileData["hydra:member"][0]["contentUrl"])[1], true);
} else {
throw new ObjectStorageException(e('File could not be downloaded from Blob!'));
}
} catch (BlobApiError $e) {
$errorMessage = BlobHelper::getBlobErrorMessage($e);
throw new ObjectStorageException(e('File could not be downloaded from Blob! %s', $errorMessage));
}
}
/**
* Output file content directly from object storage.
* Used by Download method and image gallery.
*
* @access public
*
* @param string $prefix blob key
*
* @return void
*
* @throws ObjectStorageException
*/
public function output($prefix): void
{
try {
$fileData = $this->blobApi->getFileDataByPrefix($prefix);
if (is_array($fileData) && isset($fileData["hydra:member"][0]["contentUrl"])) {
echo base64_decode(explode(',', $fileData["hydra:member"][0]["contentUrl"])[1], true);
}
} catch (BlobApiError $e) {
$errorMessage = BlobHelper::getBlobErrorMessage($e);
throw new ObjectStorageException(e('Unable to output file. %s', $errorMessage));
}
}
/**
* Upload file to object storage
*
* @access public
*
* @param string $file_tmp_src array contain local file data
* @param string $key blob key and orig filename
*
* @return boolean
*
* @throws ObjectStorageException
*/
public function moveFile($file_tmp_src, $key): bool
{
try {
if (BlobHelper::checkIfFileIsAllowed($file_tmp_src)) {
list($filename, $prefix) = BlobHelper::getFilenameAndPrefixFromKey($key);
if ($filename && $prefix) {
$this->blobApi->uploadFile($prefix, $filename, file_get_contents($file_tmp_src));
return true;
} else {
throw new ObjectStorageException(e('Unable to upload file. Wrong key supplied.'));
}
} else {
throw new ObjectStorageException(e('File type is not allowed. Only images, documents and zip files are allowed.'));
}
} catch (BlobApiError $e) {
$errorMessage = BlobHelper::getBlobErrorMessage($e);
throw new ObjectStorageException(e('Unable to upload file. %s', $errorMessage));
}
}
/**
* Upload image to object storage.
*
* @access public
*
* @param string $key blob key and orig filename
* @param string $blob file content blob
*
* @throws ObjectStorageException
*/
public function put($key, &$blob): void
{
try {
if (BlobHelper::checkIfFileIsAllowed($blob)) {
list($filename, $prefix) = BlobHelper::getFilenameAndPrefixFromKey($key);
if ($filename && $prefix) {
$this->blobApi->uploadFile($prefix, $filename, $blob);
} else {
throw new ObjectStorageException(e('Unable to upload file. Wrong key supplied.'));
}
} else {
throw new ObjectStorageException(e('File type is not allowed. Only images, documents and zip files are allowed.'));
}
} catch (BlobApiError $e) {
$errorMessage = BlobHelper::getBlobErrorMessage($e);
throw new ObjectStorageException(e('Unable to upload file. %s', $errorMessage));
}
}
/**
* Move uploaded file to object storage
*
* @access public
*
* @param string $file_tmp_src array contain local file data
* @param string $key blob key
*
* @return boolean
*/
public function moveUploadedFile($file_tmp_src, $key)
{
return $this->moveFile($file_tmp_src, $key);
}
/**
* Delete file from object storage.
*
* @access public
*
* @param string $prefix blob key
*
* @throws ObjectStorageException
*
* @return boolean
*/
public function remove($prefix): bool
{
try {
$isDeleted = $this->blobApi->deleteFilesByPrefix($prefix);
return true;
} catch (BlobApiError $e) {
$errorMessage = BlobHelper::getBlobErrorMessage($e);
throw new ObjectStorageException(e('Files could not be deleted from Blob! %s', $errorMessage));
}
}
}