Skip to content

Commit 383a4ca

Browse files
committed
get chunk upload working
1 parent c0b2691 commit 383a4ca

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

package-lock.json

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"dependencies": {
3636
"cross-fetch": "3.1.5",
3737
"isomorphic-form-data": "2.0.0",
38-
"react-native": "^0.73.5"
38+
"react-native": "^0.73.5",
39+
"react-native-fs": "^2.20.0"
3940
},
4041
"jsdelivr": "dist/iife/sdk.js",
4142
"unpkg": "dist/iife/sdk.js"

src/services/storage.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Service } from '../service';
22
import { AppwriteException, Client } from '../client';
33
import type { Models } from '../models';
44
import type { UploadProgress, Payload } from '../client';
5+
import fs from 'react-native-fs'
56

67
export class Storage extends Service {
78

@@ -68,12 +69,12 @@ export class Storage extends Service {
6869
*
6970
* @param {string} bucketId
7071
* @param {string} fileId
71-
* @param {File} file
72+
* @param {any} file
7273
* @param {string[]} permissions
7374
* @throws {AppwriteException}
7475
* @returns {Promise}
7576
*/
76-
async createFile(bucketId: string, fileId: string, file: File, permissions?: string[], onProgress = (progress: UploadProgress) => {}): Promise<Models.File> {
77+
async createFile(bucketId: string, fileId: string, file: any, permissions?: string[], onProgress = (progress: UploadProgress) => {}): Promise<Models.File> {
7778
if (typeof bucketId === 'undefined') {
7879
throw new AppwriteException('Missing required parameter: "bucketId"');
7980
}
@@ -103,10 +104,6 @@ export class Storage extends Service {
103104

104105
const uri = new URL(this.client.config.endpoint + apiPath);
105106

106-
if(!(file instanceof File)) {
107-
throw new AppwriteException('Parameter "file" has to be a File.');
108-
}
109-
110107
const size = file.size;
111108

112109
if (size <= Service.CHUNK_SIZE) {
@@ -137,8 +134,9 @@ export class Storage extends Service {
137134
apiHeaders['x-appwrite-id'] = response.$id;
138135
}
139136

140-
const chunk = file.slice(offset, end + 1);
141-
payload['file'] = new File([chunk], file.name);
137+
let chunk = await fs.read(file.uri, Service.CHUNK_SIZE, offset, 'base64');
138+
139+
payload['file'] = {uri: `data:${file.type};base64,${chunk}`, name: file.name, type: file.type};
142140
response = await this.client.call('post', uri, apiHeaders, payload);
143141

144142
if (onProgress) {

0 commit comments

Comments
 (0)