Skip to content

Commit 9ca7a24

Browse files
committed
resumable upload support
1 parent 817d06a commit 9ca7a24

File tree

7 files changed

+103
-88
lines changed

7 files changed

+103
-88
lines changed

src/Appwrite/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Client
3737
*/
3838
protected $headers = [
3939
'content-type' => '',
40-
'x-sdk-version' => 'appwrite:php:4.0.0',
40+
'x-sdk-version' => 'appwrite:php:4.1.0',
4141
];
4242

4343
/**

src/Appwrite/Services/Account.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public function get(): array
3636
* be deleted separately.
3737
*
3838
* @throws AppwriteException
39-
* @return string
39+
* @return array
4040
*/
41-
public function delete(): string
41+
public function delete(): array
4242
{
4343
$path = str_replace([], [], '/account');
4444
$params = [];
@@ -359,9 +359,9 @@ public function getSessions(): array
359359
* from the end client.
360360
*
361361
* @throws AppwriteException
362-
* @return string
362+
* @return array
363363
*/
364-
public function deleteSessions(): string
364+
public function deleteSessions(): array
365365
{
366366
$path = str_replace([], [], '/account/sessions');
367367
$params = [];
@@ -426,9 +426,9 @@ public function updateSession(string $sessionId): array
426426
*
427427
* @param string $sessionId
428428
* @throws AppwriteException
429-
* @return string
429+
* @return array
430430
*/
431-
public function deleteSession(string $sessionId): string
431+
public function deleteSession(string $sessionId): array
432432
{
433433
if (!isset($sessionId)) {
434434
throw new AppwriteException('Missing required parameter: "sessionId"');

src/Appwrite/Services/Database.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ public function updateCollection(string $collectionId, string $name, string $per
210210
*
211211
* @param string $collectionId
212212
* @throws AppwriteException
213-
* @return string
213+
* @return array
214214
*/
215-
public function deleteCollection(string $collectionId): string
215+
public function deleteCollection(string $collectionId): array
216216
{
217217
if (!isset($collectionId)) {
218218
throw new AppwriteException('Missing required parameter: "collectionId"');
@@ -732,9 +732,9 @@ public function getAttribute(string $collectionId, string $key): array
732732
* @param string $collectionId
733733
* @param string $key
734734
* @throws AppwriteException
735-
* @return string
735+
* @return array
736736
*/
737-
public function deleteAttribute(string $collectionId, string $key): string
737+
public function deleteAttribute(string $collectionId, string $key): array
738738
{
739739
if (!isset($collectionId)) {
740740
throw new AppwriteException('Missing required parameter: "collectionId"');
@@ -954,9 +954,9 @@ public function updateDocument(string $collectionId, string $documentId, array $
954954
* @param string $collectionId
955955
* @param string $documentId
956956
* @throws AppwriteException
957-
* @return string
957+
* @return array
958958
*/
959-
public function deleteDocument(string $collectionId, string $documentId): string
959+
public function deleteDocument(string $collectionId, string $documentId): array
960960
{
961961
if (!isset($collectionId)) {
962962
throw new AppwriteException('Missing required parameter: "collectionId"');
@@ -1080,9 +1080,9 @@ public function getIndex(string $collectionId, string $key): array
10801080
* @param string $collectionId
10811081
* @param string $key
10821082
* @throws AppwriteException
1083-
* @return string
1083+
* @return array
10841084
*/
1085-
public function deleteIndex(string $collectionId, string $key): string
1085+
public function deleteIndex(string $collectionId, string $key): array
10861086
{
10871087
if (!isset($collectionId)) {
10881088
throw new AppwriteException('Missing required parameter: "collectionId"');

src/Appwrite/Services/Functions.php

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ public function update(string $functionId, string $name, array $execute, array $
242242
*
243243
* @param string $functionId
244244
* @throws AppwriteException
245-
* @return string
245+
* @return array
246246
*/
247-
public function delete(string $functionId): string
247+
public function delete(string $functionId): array
248248
{
249249
if (!isset($functionId)) {
250250
throw new AppwriteException('Missing required parameter: "functionId"');
@@ -375,36 +375,40 @@ public function createDeployment(string $functionId, string $entrypoint, string
375375
return $this->client->call(Client::METHOD_POST, $path, [
376376
'content-type' => 'multipart/form-data',
377377
], $params);
378-
} else {
379-
$id = '';
380-
$handle = @fopen($code, "rb");
381-
$counter = 0;
382-
$headers = ['content-type' => 'multipart/form-data'];
383-
while (!feof($handle)) {
384-
$params['code'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
385-
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
386-
if(!empty($id)) {
387-
$headers['x-appwrite-id'] = $id;
388-
}
389-
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
390-
$counter++;
391-
if(empty($id)) {
392-
$id = $response['$id'];
393-
}
394-
if($onProgress !== null) {
395-
$end = min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size);
396-
$onProgress([
397-
'$id' => $response['$id'],
398-
'progress' => min(($counter+1) * Client::CHUNK_SIZE, $size) / $size * 100,
399-
'sizeUploaded' => $end + 1,
400-
'chunksTotal' => $response['chunksTotal'],
401-
'chunksUploaded' => $response['chunksUploaded']
402-
]);
403-
}
378+
}
379+
380+
$id = '';
381+
$counter = 0;
382+
383+
384+
$headers = ['content-type' => 'multipart/form-data'];
385+
$handle = @fopen($code, "rb");
386+
$start = $counter * Client::CHUNK_SIZE;
387+
while ($start < $size) {
388+
fseek($handle, $start);
389+
$params['code'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
390+
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
391+
if(!empty($id)) {
392+
$headers['x-appwrite-id'] = $id;
393+
}
394+
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
395+
$counter++;
396+
$start += Client::CHUNK_SIZE;
397+
if(empty($id)) {
398+
$id = $response['$id'];
399+
}
400+
if($onProgress !== null) {
401+
$onProgress([
402+
'$id' => $response['$id'],
403+
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
404+
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
405+
'chunksTotal' => $response['chunksTotal'],
406+
'chunksUploaded' => $response['chunksUploaded'],
407+
]);
404408
}
405-
@fclose($handle);
406-
return $response;
407409
}
410+
@fclose($handle);
411+
return $response;
408412
}
409413

410414
/**
@@ -473,9 +477,9 @@ public function updateDeployment(string $functionId, string $deploymentId): arra
473477
* @param string $functionId
474478
* @param string $deploymentId
475479
* @throws AppwriteException
476-
* @return string
480+
* @return array
477481
*/
478-
public function deleteDeployment(string $functionId, string $deploymentId): string
482+
public function deleteDeployment(string $functionId, string $deploymentId): array
479483
{
480484
if (!isset($functionId)) {
481485
throw new AppwriteException('Missing required parameter: "functionId"');

src/Appwrite/Services/Storage.php

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ public function updateBucket(string $bucketId, string $name, string $permission,
244244
*
245245
* @param string $bucketId
246246
* @throws AppwriteException
247-
* @return string
247+
* @return array
248248
*/
249-
public function deleteBucket(string $bucketId): string
249+
public function deleteBucket(string $bucketId): array
250250
{
251251
if (!isset($bucketId)) {
252252
throw new AppwriteException('Missing required parameter: "bucketId"');
@@ -387,36 +387,47 @@ public function createFile(string $bucketId, string $fileId, string $file, array
387387
return $this->client->call(Client::METHOD_POST, $path, [
388388
'content-type' => 'multipart/form-data',
389389
], $params);
390-
} else {
391-
$id = '';
392-
$handle = @fopen($file, "rb");
393-
$counter = 0;
394-
$headers = ['content-type' => 'multipart/form-data'];
395-
while (!feof($handle)) {
396-
$params['file'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
397-
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
398-
if(!empty($id)) {
399-
$headers['x-appwrite-id'] = $id;
400-
}
401-
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
402-
$counter++;
403-
if(empty($id)) {
404-
$id = $response['$id'];
405-
}
406-
if($onProgress !== null) {
407-
$end = min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size);
408-
$onProgress([
409-
'$id' => $response['$id'],
410-
'progress' => min(($counter+1) * Client::CHUNK_SIZE, $size) / $size * 100,
411-
'sizeUploaded' => $end + 1,
412-
'chunksTotal' => $response['chunksTotal'],
413-
'chunksUploaded' => $response['chunksUploaded']
414-
]);
390+
}
391+
392+
$id = '';
393+
$counter = 0;
394+
395+
if($fileId != 'unique()') {
396+
try {
397+
$response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . fileId), headers);
398+
$counter = $response['chunksUploaded'] ?? 0;
399+
} catch(\Exception $e) {
415400
}
416401
}
417-
@fclose($handle);
418-
return $response;
402+
403+
$headers = ['content-type' => 'multipart/form-data'];
404+
$handle = @fopen($file, "rb");
405+
$start = $counter * Client::CHUNK_SIZE;
406+
while ($start < $size) {
407+
fseek($handle, $start);
408+
$params['file'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
409+
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
410+
if(!empty($id)) {
411+
$headers['x-appwrite-id'] = $id;
412+
}
413+
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
414+
$counter++;
415+
$start += Client::CHUNK_SIZE;
416+
if(empty($id)) {
417+
$id = $response['$id'];
418+
}
419+
if($onProgress !== null) {
420+
$onProgress([
421+
'$id' => $response['$id'],
422+
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
423+
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
424+
'chunksTotal' => $response['chunksTotal'],
425+
'chunksUploaded' => $response['chunksUploaded'],
426+
]);
427+
}
419428
}
429+
@fclose($handle);
430+
return $response;
420431
}
421432

422433
/**
@@ -496,9 +507,9 @@ public function updateFile(string $bucketId, string $fileId, array $read = null,
496507
* @param string $bucketId
497508
* @param string $fileId
498509
* @throws AppwriteException
499-
* @return string
510+
* @return array
500511
*/
501-
public function deleteFile(string $bucketId, string $fileId): string
512+
public function deleteFile(string $bucketId, string $fileId): array
502513
{
503514
if (!isset($bucketId)) {
504515
throw new AppwriteException('Missing required parameter: "bucketId"');

src/Appwrite/Services/Teams.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ public function update(string $teamId, string $name): array
167167
*
168168
* @param string $teamId
169169
* @throws AppwriteException
170-
* @return string
170+
* @return array
171171
*/
172-
public function delete(string $teamId): string
172+
public function delete(string $teamId): array
173173
{
174174
if (!isset($teamId)) {
175175
throw new AppwriteException('Missing required parameter: "teamId"');
@@ -384,9 +384,9 @@ public function updateMembershipRoles(string $teamId, string $membershipId, arra
384384
* @param string $teamId
385385
* @param string $membershipId
386386
* @throws AppwriteException
387-
* @return string
387+
* @return array
388388
*/
389-
public function deleteMembership(string $teamId, string $membershipId): string
389+
public function deleteMembership(string $teamId, string $membershipId): array
390390
{
391391
if (!isset($teamId)) {
392392
throw new AppwriteException('Missing required parameter: "teamId"');

src/Appwrite/Services/Users.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public function get(string $userId): array
137137
*
138138
* @param string $userId
139139
* @throws AppwriteException
140-
* @return string
140+
* @return array
141141
*/
142-
public function delete(string $userId): string
142+
public function delete(string $userId): array
143143
{
144144
if (!isset($userId)) {
145145
throw new AppwriteException('Missing required parameter: "userId"');
@@ -369,9 +369,9 @@ public function getSessions(string $userId): array
369369
*
370370
* @param string $userId
371371
* @throws AppwriteException
372-
* @return string
372+
* @return array
373373
*/
374-
public function deleteSessions(string $userId): string
374+
public function deleteSessions(string $userId): array
375375
{
376376
if (!isset($userId)) {
377377
throw new AppwriteException('Missing required parameter: "userId"');
@@ -393,9 +393,9 @@ public function deleteSessions(string $userId): string
393393
* @param string $userId
394394
* @param string $sessionId
395395
* @throws AppwriteException
396-
* @return string
396+
* @return array
397397
*/
398-
public function deleteSession(string $userId, string $sessionId): string
398+
public function deleteSession(string $userId, string $sessionId): array
399399
{
400400
if (!isset($userId)) {
401401
throw new AppwriteException('Missing required parameter: "userId"');

0 commit comments

Comments
 (0)