|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author André Gaul <[email protected]> |
|
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
7
|
|
|
* @author Bart Visscher <[email protected]> |
|
8
|
|
|
* @author Christian Berendt <[email protected]> |
|
9
|
|
|
* @author Christopher T. Johnson <[email protected]> |
|
10
|
|
|
* @author Johan Björk <[email protected]> |
|
11
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
12
|
|
|
* @author Martin Mattel <[email protected]> |
|
13
|
|
|
* @author Michael Gapczynski <[email protected]> |
|
14
|
|
|
* @author Morris Jobke <[email protected]> |
|
15
|
|
|
* @author Philipp Kapfer <[email protected]> |
|
16
|
|
|
* @author Robin Appelman <[email protected]> |
|
17
|
|
|
* @author Robin McCorkell <[email protected]> |
|
18
|
|
|
* @author Thomas Müller <[email protected]> |
|
19
|
|
|
* @author Vincent Petry <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @license AGPL-3.0 |
|
22
|
|
|
* |
|
23
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
24
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
25
|
|
|
* as published by the Free Software Foundation. |
|
26
|
|
|
* |
|
27
|
|
|
* This program is distributed in the hope that it will be useful, |
|
28
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
29
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
30
|
|
|
* GNU Affero General Public License for more details. |
|
31
|
|
|
* |
|
32
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
33
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
namespace OCA\Files_External\Lib\Storage; |
|
38
|
|
|
|
|
39
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . |
|
40
|
|
|
\OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php'); |
|
41
|
|
|
require_once 'aws-autoloader.php'; |
|
42
|
|
|
|
|
43
|
|
|
use Aws\S3\S3Client; |
|
44
|
|
|
use Aws\S3\Exception\S3Exception; |
|
45
|
|
|
use Icewind\Streams\IteratorDirectory; |
|
46
|
|
|
use OC\Files\ObjectStore\S3ConnectionTrait; |
|
47
|
|
|
|
|
48
|
|
|
class AmazonS3 extends \OC\Files\Storage\Common { |
|
49
|
|
|
use S3ConnectionTrait; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private static $tmpFiles = array(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var int in seconds |
|
58
|
|
|
*/ |
|
59
|
|
|
private $rescanDelay = 10; |
|
60
|
|
|
|
|
61
|
|
|
public function __construct($parameters) { |
|
62
|
|
|
parent::__construct($parameters); |
|
63
|
|
|
$this->parseParams($parameters); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $path |
|
68
|
|
|
* @return string correctly encoded path |
|
69
|
|
|
*/ |
|
70
|
|
|
private function normalizePath($path) { |
|
71
|
|
|
$path = trim($path, '/'); |
|
72
|
|
|
|
|
73
|
|
|
if (!$path) { |
|
74
|
|
|
$path = '.'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $path; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function isRoot($path) { |
|
81
|
|
|
return $path === '.'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function cleanKey($path) { |
|
85
|
|
|
if ($this->isRoot($path)) { |
|
86
|
|
|
return '/'; |
|
87
|
|
|
} |
|
88
|
|
|
return $path; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name. |
|
93
|
|
|
* TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $params |
|
96
|
|
|
*/ |
|
97
|
|
|
public function updateLegacyId (array $params) { |
|
98
|
|
|
$oldId = 'amazon::' . $params['key'] . md5($params['secret']); |
|
99
|
|
|
|
|
100
|
|
|
// find by old id or bucket |
|
101
|
|
|
$stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
102
|
|
|
'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)' |
|
103
|
|
|
); |
|
104
|
|
|
$stmt->execute(array($oldId, $this->id)); |
|
105
|
|
|
while ($row = $stmt->fetch()) { |
|
106
|
|
|
$storages[$row['id']] = $row['numeric_id']; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (isset($storages[$this->id]) && isset($storages[$oldId])) { |
|
110
|
|
|
// if both ids exist, delete the old storage and corresponding filecache entries |
|
111
|
|
|
\OC\Files\Cache\Storage::remove($oldId); |
|
112
|
|
|
} else if (isset($storages[$oldId])) { |
|
113
|
|
|
// if only the old id exists do an update |
|
114
|
|
|
$stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
115
|
|
|
'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?' |
|
116
|
|
|
); |
|
117
|
|
|
$stmt->execute(array($this->id, $oldId)); |
|
118
|
|
|
} |
|
119
|
|
|
// only the bucket based id may exist, do nothing |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Remove a file or folder |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $path |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function remove($path) { |
|
129
|
|
|
// remember fileType to reduce http calls |
|
130
|
|
|
$fileType = $this->filetype($path); |
|
131
|
|
|
if ($fileType === 'dir') { |
|
132
|
|
|
return $this->rmdir($path); |
|
133
|
|
|
} else if ($fileType === 'file') { |
|
134
|
|
|
return $this->unlink($path); |
|
135
|
|
|
} else { |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function mkdir($path) { |
|
141
|
|
|
$path = $this->normalizePath($path); |
|
142
|
|
|
|
|
143
|
|
|
if ($this->is_dir($path)) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
$this->getConnection()->putObject(array( |
|
149
|
|
|
'Bucket' => $this->bucket, |
|
150
|
|
|
'Key' => $path . '/', |
|
151
|
|
|
'Body' => '', |
|
152
|
|
|
'ContentType' => 'httpd/unix-directory' |
|
153
|
|
|
)); |
|
154
|
|
|
$this->testTimeout(); |
|
155
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
156
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return true; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function file_exists($path) { |
|
164
|
|
|
return $this->filetype($path) !== false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
public function rmdir($path) { |
|
169
|
|
|
$path = $this->normalizePath($path); |
|
170
|
|
|
|
|
171
|
|
|
if ($this->isRoot($path)) { |
|
172
|
|
|
return $this->clearBucket(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (!$this->file_exists($path)) { |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this->batchDelete($path); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
protected function clearBucket() { |
|
183
|
|
|
try { |
|
184
|
|
|
$this->getConnection()->clearBucket($this->bucket); |
|
185
|
|
|
return true; |
|
186
|
|
|
// clearBucket() is not working with Ceph, so if it fails we try the slower approach |
|
187
|
|
|
} catch (\Exception $e) { |
|
188
|
|
|
return $this->batchDelete(); |
|
189
|
|
|
} |
|
190
|
|
|
return false; |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
private function batchDelete ($path = null) { |
|
194
|
|
|
$params = array( |
|
195
|
|
|
'Bucket' => $this->bucket |
|
196
|
|
|
); |
|
197
|
|
|
if ($path !== null) { |
|
198
|
|
|
$params['Prefix'] = $path . '/'; |
|
199
|
|
|
} |
|
200
|
|
|
try { |
|
201
|
|
|
// Since there are no real directories on S3, we need |
|
202
|
|
|
// to delete all objects prefixed with the path. |
|
203
|
|
|
do { |
|
204
|
|
|
// instead of the iterator, manually loop over the list ... |
|
205
|
|
|
$objects = $this->getConnection()->listObjects($params); |
|
206
|
|
|
// ... so we can delete the files in batches |
|
207
|
|
|
$this->getConnection()->deleteObjects(array( |
|
208
|
|
|
'Bucket' => $this->bucket, |
|
209
|
|
|
'Objects' => $objects['Contents'] |
|
210
|
|
|
)); |
|
211
|
|
|
$this->testTimeout(); |
|
212
|
|
|
// we reached the end when the list is no longer truncated |
|
213
|
|
|
} while ($objects['IsTruncated']); |
|
214
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
215
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
216
|
|
|
return false; |
|
217
|
|
|
} |
|
218
|
|
|
return true; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function opendir($path) { |
|
222
|
|
|
$path = $this->normalizePath($path); |
|
223
|
|
|
|
|
224
|
|
|
if ($this->isRoot($path)) { |
|
225
|
|
|
$path = ''; |
|
226
|
|
|
} else { |
|
227
|
|
|
$path .= '/'; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
try { |
|
231
|
|
|
$files = array(); |
|
232
|
|
|
$result = $this->getConnection()->getIterator('ListObjects', array( |
|
233
|
|
|
'Bucket' => $this->bucket, |
|
234
|
|
|
'Delimiter' => '/', |
|
235
|
|
|
'Prefix' => $path |
|
236
|
|
|
), array('return_prefixes' => true)); |
|
237
|
|
|
|
|
238
|
|
|
foreach ($result as $object) { |
|
239
|
|
|
if (isset($object['Key']) && $object['Key'] === $path) { |
|
240
|
|
|
// it's the directory itself, skip |
|
241
|
|
|
continue; |
|
242
|
|
|
} |
|
243
|
|
|
$file = basename( |
|
244
|
|
|
isset($object['Key']) ? $object['Key'] : $object['Prefix'] |
|
245
|
|
|
); |
|
246
|
|
|
$files[] = $file; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return IteratorDirectory::wrap($files); |
|
250
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
251
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
252
|
|
|
return false; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function stat($path) { |
|
257
|
|
|
$path = $this->normalizePath($path); |
|
258
|
|
|
|
|
259
|
|
|
try { |
|
260
|
|
|
$stat = array(); |
|
261
|
|
|
if ($this->is_dir($path)) { |
|
262
|
|
|
//folders don't really exist |
|
263
|
|
|
$stat['size'] = -1; //unknown |
|
264
|
|
|
$stat['mtime'] = time() - $this->rescanDelay * 1000; |
|
265
|
|
|
} else { |
|
266
|
|
|
$result = $this->getConnection()->headObject(array( |
|
267
|
|
|
'Bucket' => $this->bucket, |
|
268
|
|
|
'Key' => $path |
|
269
|
|
|
)); |
|
270
|
|
|
|
|
271
|
|
|
$stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0; |
|
272
|
|
|
if ($result['Metadata']['lastmodified']) { |
|
273
|
|
|
$stat['mtime'] = strtotime($result['Metadata']['lastmodified']); |
|
274
|
|
|
} else { |
|
275
|
|
|
$stat['mtime'] = strtotime($result['LastModified']); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
$stat['atime'] = time(); |
|
279
|
|
|
|
|
280
|
|
|
return $stat; |
|
281
|
|
|
} catch(S3Exception $e) { |
|
|
|
|
|
|
282
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
283
|
|
|
return false; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function filetype($path) { |
|
288
|
|
|
$path = $this->normalizePath($path); |
|
289
|
|
|
|
|
290
|
|
|
if ($this->isRoot($path)) { |
|
291
|
|
|
return 'dir'; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
try { |
|
295
|
|
|
if ($this->getConnection()->doesObjectExist($this->bucket, $path)) { |
|
296
|
|
|
return 'file'; |
|
297
|
|
|
} |
|
298
|
|
|
if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) { |
|
299
|
|
|
return 'dir'; |
|
300
|
|
|
} |
|
301
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
302
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
303
|
|
|
return false; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return false; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
public function unlink($path) { |
|
310
|
|
|
$path = $this->normalizePath($path); |
|
311
|
|
|
|
|
312
|
|
|
if ($this->is_dir($path)) { |
|
313
|
|
|
return $this->rmdir($path); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
try { |
|
317
|
|
|
$this->getConnection()->deleteObject(array( |
|
318
|
|
|
'Bucket' => $this->bucket, |
|
319
|
|
|
'Key' => $path |
|
320
|
|
|
)); |
|
321
|
|
|
$this->testTimeout(); |
|
322
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
323
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
324
|
|
|
return false; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
return true; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
public function fopen($path, $mode) { |
|
331
|
|
|
$path = $this->normalizePath($path); |
|
332
|
|
|
|
|
333
|
|
|
switch ($mode) { |
|
334
|
|
|
case 'r': |
|
335
|
|
|
case 'rb': |
|
336
|
|
|
$tmpFile = \OCP\Files::tmpFile(); |
|
|
|
|
|
|
337
|
|
|
self::$tmpFiles[$tmpFile] = $path; |
|
338
|
|
|
|
|
339
|
|
|
try { |
|
340
|
|
|
$this->getConnection()->getObject(array( |
|
341
|
|
|
'Bucket' => $this->bucket, |
|
342
|
|
|
'Key' => $path, |
|
343
|
|
|
'SaveAs' => $tmpFile |
|
344
|
|
|
)); |
|
345
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
346
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
347
|
|
|
return false; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
return fopen($tmpFile, 'r'); |
|
351
|
|
|
case 'w': |
|
352
|
|
|
case 'wb': |
|
353
|
|
|
case 'a': |
|
354
|
|
|
case 'ab': |
|
355
|
|
|
case 'r+': |
|
356
|
|
|
case 'w+': |
|
357
|
|
|
case 'wb+': |
|
358
|
|
|
case 'a+': |
|
359
|
|
|
case 'x': |
|
360
|
|
|
case 'x+': |
|
361
|
|
|
case 'c': |
|
362
|
|
View Code Duplication |
case 'c+': |
|
|
|
|
|
|
363
|
|
|
if (strrpos($path, '.') !== false) { |
|
364
|
|
|
$ext = substr($path, strrpos($path, '.')); |
|
365
|
|
|
} else { |
|
366
|
|
|
$ext = ''; |
|
367
|
|
|
} |
|
368
|
|
|
$tmpFile = \OCP\Files::tmpFile($ext); |
|
|
|
|
|
|
369
|
|
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
|
370
|
|
|
if ($this->file_exists($path)) { |
|
371
|
|
|
$source = $this->fopen($path, 'r'); |
|
372
|
|
|
file_put_contents($tmpFile, $source); |
|
373
|
|
|
} |
|
374
|
|
|
self::$tmpFiles[$tmpFile] = $path; |
|
375
|
|
|
|
|
376
|
|
|
return fopen('close://' . $tmpFile, $mode); |
|
377
|
|
|
} |
|
378
|
|
|
return false; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
public function touch($path, $mtime = null) { |
|
382
|
|
|
$path = $this->normalizePath($path); |
|
383
|
|
|
|
|
384
|
|
|
$metadata = array(); |
|
|
|
|
|
|
385
|
|
|
if (is_null($mtime)) { |
|
386
|
|
|
$mtime = time(); |
|
387
|
|
|
} |
|
388
|
|
|
$metadata = [ |
|
389
|
|
|
'lastmodified' => gmdate(\Aws\Common\Enum\DateFormat::RFC1123, $mtime) |
|
390
|
|
|
]; |
|
391
|
|
|
|
|
392
|
|
|
$fileType = $this->filetype($path); |
|
393
|
|
|
try { |
|
394
|
|
|
if ($fileType !== false) { |
|
395
|
|
|
if ($fileType === 'dir' && ! $this->isRoot($path)) { |
|
396
|
|
|
$path .= '/'; |
|
397
|
|
|
} |
|
398
|
|
|
$this->getConnection()->copyObject([ |
|
399
|
|
|
'Bucket' => $this->bucket, |
|
400
|
|
|
'Key' => $this->cleanKey($path), |
|
401
|
|
|
'Metadata' => $metadata, |
|
402
|
|
|
'CopySource' => $this->bucket . '/' . $path, |
|
403
|
|
|
'MetadataDirective' => 'REPLACE', |
|
404
|
|
|
]); |
|
405
|
|
|
$this->testTimeout(); |
|
406
|
|
|
} else { |
|
407
|
|
|
$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
408
|
|
|
$this->getConnection()->putObject([ |
|
409
|
|
|
'Bucket' => $this->bucket, |
|
410
|
|
|
'Key' => $this->cleanKey($path), |
|
411
|
|
|
'Metadata' => $metadata, |
|
412
|
|
|
'Body' => '', |
|
413
|
|
|
'ContentType' => $mimeType, |
|
414
|
|
|
'MetadataDirective' => 'REPLACE', |
|
415
|
|
|
]); |
|
416
|
|
|
$this->testTimeout(); |
|
417
|
|
|
} |
|
418
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
419
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
420
|
|
|
return false; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
return true; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
public function copy($path1, $path2) { |
|
427
|
|
|
$path1 = $this->normalizePath($path1); |
|
428
|
|
|
$path2 = $this->normalizePath($path2); |
|
429
|
|
|
|
|
430
|
|
|
if ($this->is_file($path1)) { |
|
431
|
|
|
try { |
|
432
|
|
|
$this->getConnection()->copyObject(array( |
|
433
|
|
|
'Bucket' => $this->bucket, |
|
434
|
|
|
'Key' => $this->cleanKey($path2), |
|
435
|
|
|
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
436
|
|
|
)); |
|
437
|
|
|
$this->testTimeout(); |
|
438
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
439
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
440
|
|
|
return false; |
|
441
|
|
|
} |
|
442
|
|
|
} else { |
|
443
|
|
|
$this->remove($path2); |
|
444
|
|
|
|
|
445
|
|
|
try { |
|
446
|
|
|
$this->getConnection()->copyObject(array( |
|
447
|
|
|
'Bucket' => $this->bucket, |
|
448
|
|
|
'Key' => $path2 . '/', |
|
449
|
|
|
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
450
|
|
|
)); |
|
451
|
|
|
$this->testTimeout(); |
|
452
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
453
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
454
|
|
|
return false; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
$dh = $this->opendir($path1); |
|
458
|
|
|
if (is_resource($dh)) { |
|
459
|
|
|
while (($file = readdir($dh)) !== false) { |
|
460
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
461
|
|
|
continue; |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
$source = $path1 . '/' . $file; |
|
465
|
|
|
$target = $path2 . '/' . $file; |
|
466
|
|
|
$this->copy($source, $target); |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
return true; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
public function rename($path1, $path2) { |
|
475
|
|
|
$path1 = $this->normalizePath($path1); |
|
476
|
|
|
$path2 = $this->normalizePath($path2); |
|
477
|
|
|
|
|
478
|
|
|
if ($this->is_file($path1)) { |
|
479
|
|
|
|
|
480
|
|
|
if ($this->copy($path1, $path2) === false) { |
|
481
|
|
|
return false; |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
if ($this->unlink($path1) === false) { |
|
485
|
|
|
$this->unlink($path2); |
|
486
|
|
|
return false; |
|
487
|
|
|
} |
|
488
|
|
|
} else { |
|
489
|
|
|
|
|
490
|
|
|
if ($this->copy($path1, $path2) === false) { |
|
491
|
|
|
return false; |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
if ($this->rmdir($path1) === false) { |
|
495
|
|
|
$this->rmdir($path2); |
|
496
|
|
|
return false; |
|
497
|
|
|
} |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
return true; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
public function test() { |
|
504
|
|
|
$test = $this->getConnection()->getBucketAcl(array( |
|
505
|
|
|
'Bucket' => $this->bucket, |
|
506
|
|
|
)); |
|
507
|
|
|
if (isset($test) && !is_null($test->getPath('Owner/ID'))) { |
|
508
|
|
|
return true; |
|
509
|
|
|
} |
|
510
|
|
|
return false; |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
public function getId() { |
|
514
|
|
|
return $this->id; |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
public function writeBack($tmpFile) { |
|
518
|
|
|
if (!isset(self::$tmpFiles[$tmpFile])) { |
|
519
|
|
|
return false; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
try { |
|
523
|
|
|
$this->getConnection()->putObject(array( |
|
524
|
|
|
'Bucket' => $this->bucket, |
|
525
|
|
|
'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), |
|
526
|
|
|
'SourceFile' => $tmpFile, |
|
527
|
|
|
'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile), |
|
528
|
|
|
'ContentLength' => filesize($tmpFile) |
|
529
|
|
|
)); |
|
530
|
|
|
$this->testTimeout(); |
|
531
|
|
|
|
|
532
|
|
|
unlink($tmpFile); |
|
533
|
|
|
} catch (S3Exception $e) { |
|
|
|
|
|
|
534
|
|
|
\OCP\Util::logException('files_external', $e); |
|
|
|
|
|
|
535
|
|
|
return false; |
|
536
|
|
|
} |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* check if curl is installed |
|
541
|
|
|
*/ |
|
542
|
|
|
public static function checkDependencies() { |
|
543
|
|
|
return true; |
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
|
|
} |
|
547
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.