Skip to content

Conversation

lohanidamodar
Copy link
Member

@lohanidamodar lohanidamodar commented Feb 19, 2022

createFile: async (bucketId: string, fileId: string, file: File, read?: string[], write?: string[], onProgress = (progress: any) => {}): Promise<Models.File> => {
            if (typeof bucketId === 'undefined') {
                throw new AppwriteException('Missing required parameter: "bucketId"');
            }

            if (typeof fileId === 'undefined') {
                throw new AppwriteException('Missing required parameter: "fileId"');
            }

            if (typeof file === 'undefined') {
                throw new AppwriteException('Missing required parameter: "file"');
            }

            let path = '/storage/buckets/{bucketId}/files'.replace('{bucketId}', bucketId);
            let payload: Payload = {};

            if (typeof fileId !== 'undefined') {
                payload['fileId'] = fileId;
            }

            if (typeof file !== 'undefined') {
                payload['file'] = file;
            }

            if (typeof read !== 'undefined') {
                payload['read'] = read;
            }

            if (typeof write !== 'undefined') {
                payload['write'] = write;
            }

            const uri = new URL(this.config.endpoint + path);
            const size = file.size;

            if (size <= Appwrite.CHUNK_SIZE) {
                return await this.call('post', uri, {
            
                    'content-type': 'multipart/form-data',
                }, payload);
            }
            let id = undefined;
            let response = undefined;

            const headers: { [header: string]: string } = {
                'content-type': 'multipart/form-data',
            }

            let counter = 0;
            const totalCounters = Math.ceil(size / Appwrite.CHUNK_SIZE);
            if(fileId != 'unique()') {
                try {
                    response = await this.call('GET', new URL(this.config.endpoint + path + fileId), headers);
                    counter = response.chunksUploaded;
                } catch(e) {
                }
            }

            for (counter; counter < totalCounters; counter++) {
                const start = (counter * Appwrite.CHUNK_SIZE);
                const end = Math.min((((counter * Appwrite.CHUNK_SIZE) + Appwrite.CHUNK_SIZE) - 1), size);

                headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size

                if (id) {
                    headers['x-appwrite-id'] = id;
                }

                const stream = file.slice(start, end + 1);
                payload['file'] = new File([stream], file.name);

                response = await this.call('post', uri, headers, payload);

                if (!id) {
                    id = response['$id'];
                }

                if (onProgress) {
                    onProgress({
                        $id: response.$id,
                        progress: Math.min((counter + 1) * Appwrite.CHUNK_SIZE, size) / size * 100,
                        sizeUpploaded: end+1,
                        chunksTotal: response.chunksTotal,
                        chunksUploaded: response.chunksUploaded
                    });
                }
            }

            return response;
        }

Copy link
Contributor

@Meldiron Meldiron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@eldadfux eldadfux merged commit 7c668ac into feat-preps-for-0.13 Feb 22, 2022
@lohanidamodar lohanidamodar deleted the feat-resumable-upload-web branch June 5, 2022 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants