forked from utopia-php/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S3Base.php
277 lines (231 loc) · 10.9 KB
/
S3Base.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace Utopia\Tests\Storage;
use PHPUnit\Framework\TestCase;
use Utopia\Storage\Device\S3;
abstract class S3Base extends TestCase
{
abstract protected function init(): void;
/**
* @return string
*/
abstract protected function getAdapterName(): string;
/**
* @return string
*/
abstract protected function getAdapterDescription(): string;
/**
* @var S3
*/
protected $object = null;
/**
* @var string
*/
protected $root = '/root';
/**
* @var bool
*/
protected $vhost = true;
public function setUp(): void
{
$this->init();
$this->uploadTestFiles();
}
private function uploadTestFiles()
{
$this->object->upload(__DIR__.'/../resources/disk-a/kitten-1.jpg', $this->object->getPath('testing/kitten-1.jpg'));
$this->object->upload(__DIR__.'/../resources/disk-a/kitten-2.jpg', $this->object->getPath('testing/kitten-2.jpg'));
$this->object->upload(__DIR__.'/../resources/disk-b/kitten-1.png', $this->object->getPath('testing/kitten-1.png'));
$this->object->upload(__DIR__.'/../resources/disk-b/kitten-2.png', $this->object->getPath('testing/kitten-2.png'));
}
private function removeTestFiles()
{
$this->object->delete($this->object->getPath('testing/kitten-1.jpg'));
$this->object->delete($this->object->getPath('testing/kitten-2.jpg'));
$this->object->delete($this->object->getPath('testing/kitten-1.png'));
$this->object->delete($this->object->getPath('testing/kitten-2.png'));
}
public function tearDown(): void
{
$this->removeTestFiles();
}
public function testName()
{
$this->assertEquals($this->getAdapterName(), $this->object->getName());
}
public function testType()
{
$this->assertEquals($this->getAdapterType(), $this->object->getType());
}
public function testDescription()
{
$this->assertEquals($this->getAdapterDescription(), $this->object->getDescription());
}
public function testRoot()
{
$this->assertEquals($this->root, $this->object->getRoot());
}
public function testPath()
{
$this->assertEquals($this->root.'/image.png', $this->object->getPath('image.png'));
}
public function testWrite()
{
$this->assertEquals(true, $this->object->write($this->object->getPath('text.txt'), 'Hello World', 'text/plain'));
$this->object->delete($this->object->getPath('text.txt'));
}
public function testRead()
{
$this->assertEquals(true, $this->object->write($this->object->getPath('text-for-read.txt'), 'Hello World', 'text/plain'));
$this->assertEquals('Hello World', $this->object->read($this->object->getPath('text-for-read.txt')));
$this->object->delete($this->object->getPath('text-for-read.txt'));
}
public function testFileExists()
{
$this->assertEquals(true, $this->object->exists($this->object->getPath('testing/kitten-1.jpg')));
$this->assertEquals(false, $this->object->exists($this->object->getPath('testing/kitten-5.jpg')));
}
public function testMove()
{
$this->assertEquals(true, $this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($this->object->getPath('text-for-move.txt')));
$this->assertEquals(true, $this->object->move($this->object->getPath('text-for-move.txt'), $this->object->getPath('text-for-move-new.txt')));
$this->assertEquals('Hello World', $this->object->read($this->object->getPath('text-for-move-new.txt')));
$this->assertEquals(false, $this->object->exists($this->object->getPath('text-for-move.txt')));
$this->object->delete($this->object->getPath('text-for-move-new.txt'));
}
public function testDelete()
{
$this->assertEquals(true, $this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($this->object->getPath('text-for-delete.txt')));
$this->assertEquals(true, $this->object->delete($this->object->getPath('text-for-delete.txt')));
}
public function testSVGUpload()
{
$this->assertEquals(true, $this->object->upload(__DIR__.'/../resources/disk-b/appwrite.svg', $this->object->getPath('testing/appwrite.svg')));
$this->assertEquals(file_get_contents(__DIR__.'/../resources/disk-b/appwrite.svg'), $this->object->read($this->object->getPath('testing/appwrite.svg')));
$this->assertEquals(true, $this->object->exists($this->object->getPath('testing/appwrite.svg')));
$this->assertEquals(true, $this->object->delete($this->object->getPath('testing/appwrite.svg')));
}
public function testXMLUpload()
{
$this->assertEquals(true, $this->object->upload(__DIR__.'/../resources/disk-a/config.xml', $this->object->getPath('testing/config.xml')));
$this->assertEquals(file_get_contents(__DIR__.'/../resources/disk-a/config.xml'), $this->object->read($this->object->getPath('testing/config.xml')));
$this->assertEquals(true, $this->object->exists($this->object->getPath('testing/config.xml')));
$this->assertEquals(true, $this->object->delete($this->object->getPath('testing/config.xml')));
}
public function testDeletePath()
{
if ($this->vhost) {
$dpParam = 'bucket';
} else {
$dpParam = '';
}
// Test Single Object
$path = $this->object->getPath('text-for-delete-path.txt');
$path = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path);
$this->assertEquals(true, $this->object->write($path, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path));
$this->assertEquals(true, $this->object->deletePath($dpParam));
$this->assertEquals(false, $this->object->exists($path));
// Test Multiple Objects
$path = $this->object->getPath('text-for-delete-path1.txt');
$path = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path);
$this->assertEquals(true, $this->object->write($path, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path));
$path2 = $this->object->getPath('text-for-delete-path2.txt');
$path2 = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path2);
$this->assertEquals(true, $this->object->write($path2, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path2));
$this->assertEquals(true, $this->object->deletePath($dpParam));
$this->assertEquals(false, $this->object->exists($path));
$this->assertEquals(false, $this->object->exists($path2));
}
public function testFileSize()
{
$this->assertEquals(599639, $this->object->getFileSize($this->object->getPath('testing/kitten-1.jpg')));
$this->assertEquals(131958, $this->object->getFileSize($this->object->getPath('testing/kitten-2.jpg')));
}
public function testFileMimeType()
{
$this->assertEquals('image/jpeg', $this->object->getFileMimeType($this->object->getPath('testing/kitten-1.jpg')));
$this->assertEquals('image/jpeg', $this->object->getFileMimeType($this->object->getPath('testing/kitten-2.jpg')));
$this->assertEquals('image/png', $this->object->getFileMimeType($this->object->getPath('testing/kitten-1.png')));
$this->assertEquals('image/png', $this->object->getFileMimeType($this->object->getPath('testing/kitten-2.png')));
}
public function testFileHash()
{
$this->assertEquals('7551f343143d2e24ab4aaf4624996b6a', $this->object->getFileHash($this->object->getPath('testing/kitten-1.jpg')));
$this->assertEquals('81702fdeef2e55b1a22617bce4951cb5', $this->object->getFileHash($this->object->getPath('testing/kitten-2.jpg')));
$this->assertEquals('03010f4f02980521a8fd6213b52ec313', $this->object->getFileHash($this->object->getPath('testing/kitten-1.png')));
$this->assertEquals('8a9ed992b77e4b62b10e3a5c8ed72062', $this->object->getFileHash($this->object->getPath('testing/kitten-2.png')));
}
public function testDirectoryCreate()
{
$this->assertTrue($this->object->createDirectory('temp'));
}
public function testDirectorySize()
{
$this->assertEquals(-1, $this->object->getDirectorySize('resources/disk-a/'));
}
public function testPartitionFreeSpace()
{
$this->assertEquals(-1, $this->object->getPartitionFreeSpace());
}
public function testPartitionTotalSpace()
{
$this->assertEquals(-1, $this->object->getPartitionTotalSpace());
}
public function testPartUpload()
{
$source = __DIR__.'/../resources/disk-a/large_file.mp4';
$dest = $this->object->getPath('uploaded.mp4');
$totalSize = \filesize($source);
// AWS S3 requires each part to be at least 5MB except for last part
$chunkSize = 5 * 1024 * 1024;
$chunks = ceil($totalSize / $chunkSize);
$chunk = 1;
$start = 0;
$metadata = [
'parts' => [],
'chunks' => 0,
'uploadId' => null,
'content_type' => \mime_content_type($source),
];
$handle = @fopen($source, 'rb');
$op = __DIR__.'/chunk.part';
while ($start < $totalSize) {
$contents = fread($handle, $chunkSize);
$op = __DIR__.'/chunk.part';
$cc = fopen($op, 'wb');
fwrite($cc, $contents);
fclose($cc);
$etag = $this->object->upload($op, $dest, $chunk, $chunks, $metadata);
$parts[] = ['partNumber' => $chunk, 'etag' => $etag];
$start += strlen($contents);
$chunk++;
fseek($handle, $start);
}
@fclose($handle);
unlink($op);
$this->assertEquals(\filesize($source), $this->object->getFileSize($dest));
// S3 doesnt provide a method to get a proper MD5-hash of a file created using multipart upload
// https://stackoverflow.com/questions/8618218/amazon-s3-checksum
// More info on how AWS calculates ETag for multipart upload here
// https://savjee.be/2015/10/Verifying-Amazon-S3-multi-part-uploads-with-ETag-hash/
// TODO
// $this->assertEquals(\md5_file($source), $this->object->getFileHash($dest));
// $this->object->delete($dest);
return $dest;
}
/**
* @depends testPartUpload
*/
public function testPartRead($path)
{
$source = __DIR__.'/../resources/disk-a/large_file.mp4';
$chunk = file_get_contents($source, false, null, 0, 500);
$readChunk = $this->object->read($path, 0, 500);
$this->assertEquals($chunk, $readChunk);
$this->object->delete($path);
}
}