From 463d72d19060a978d00bf8b24c6f023a376cb6e7 Mon Sep 17 00:00:00 2001 From: kukhariev Date: Fri, 21 Oct 2022 20:52:33 +0300 Subject: [PATCH] refactor: cleanup --- .../service-code-way/uploader-ext.class.ts | 26 ----------- .../service-code-way/uploaderx-s3.class.ts | 43 ++++++------------- 2 files changed, 13 insertions(+), 56 deletions(-) delete mode 100644 src/app/service-code-way/uploader-ext.class.ts diff --git a/src/app/service-code-way/uploader-ext.class.ts b/src/app/service-code-way/uploader-ext.class.ts deleted file mode 100644 index 02ffd9b9..00000000 --- a/src/app/service-code-way/uploader-ext.class.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { resolveUrl, Uploader } from 'ngx-uploadx'; - -export class UploaderExt extends Uploader { - async getFileUrl(): Promise { - this.offset = 0; - const formData: FormData = new FormData(); - formData.set('metadata', JSON.stringify(this.metadata)); - formData.append('file', this.file, this.file.name); - await this.request({ - method: 'POST', - body: formData, - url: this.endpoint - }); - this.offset = this.size; - const location = this.getValueFromResponse('location'); - return location ? resolveUrl(location, this.endpoint) : ''; - } - - async sendFileContent(): Promise { - return this.size; - } - - async getOffset(): Promise { - return 0; - } -} diff --git a/src/app/service-code-way/uploaderx-s3.class.ts b/src/app/service-code-way/uploaderx-s3.class.ts index 6ec5fd7e..1e396806 100644 --- a/src/app/service-code-way/uploaderx-s3.class.ts +++ b/src/app/service-code-way/uploaderx-s3.class.ts @@ -1,8 +1,6 @@ import { store, UploaderX } from 'ngx-uploadx'; export interface S3Multipart { partSize: number; - name: string; - UploadId: string; partsUrls: string[]; Parts?: Part[]; } @@ -10,6 +8,12 @@ interface Part { ETag: string; PartNumber: number; } + +/** + * Added `upload directly to S3` support + * + * [server example](https://github.com/kukhariev/node-uploadx/blob/master/examples/s3-direct.ts) + */ export class UploaderXS3 extends UploaderX { s3 = {} as S3Multipart; @@ -19,10 +23,7 @@ export class UploaderXS3 extends UploaderX { this.s3 = { ...this.response }; this.s3.Parts ??= []; store.set(url, JSON.stringify(this.s3)); - this.offset = this.s3.Parts.length * this.s3.partSize; - if (this.s3?.partsUrls.length === this.s3?.Parts.length) { - await this.setMetadata(this.url); - } + this.offset = Math.min(this.s3.Parts.length * this.s3.partSize, this.size); } return url; } @@ -31,22 +32,18 @@ export class UploaderXS3 extends UploaderX { if (this.s3.partsUrls) { this.s3.Parts ??= []; const i = this.s3.Parts.length; + const partUrl = this.s3.partsUrls[i]; const { body, end } = this.getChunk(this.offset, this.s3.partSize); - await this.request({ - method: 'PUT', - body, - url: this.s3.partsUrls[i], - skipAuthorization: true - }); - + await this.request({ method: 'PUT', body, url: partUrl, skipAuthorization: true }); const ETag = this.getValueFromResponse('etag'); if (!ETag) { throw Error('No access to ETag in response, check CORS configuration!'); } const part: Part = { ETag, PartNumber: i + 1 }; this.s3.Parts.push(part); + this.offset = end; if (end === this.size) { - await this.setMetadata(this.url); + await this.update(this.s3); } return end; } else { @@ -58,8 +55,8 @@ export class UploaderXS3 extends UploaderX { const _s3 = store.get(this.url); if (_s3) { this.s3 = JSON.parse(_s3); - await this.setMetadata(this.url); - if (this.response.UploadId) { + await this.update(this.s3); + if (this.response?.partsUrls) { this.s3 = { ...this.response }; } this.s3.Parts ??= []; @@ -67,18 +64,4 @@ export class UploaderXS3 extends UploaderX { } return super.getOffset(); } - - private async setMetadata(url: string): Promise { - const body = JSON.stringify(this.s3); - await this.request({ - method: 'PATCH', - headers: { 'Content-Type': 'application/json; charset=utf-8' }, - body, - url - }); - if (this.response?.partsUrls) { - this.s3 = { ...this.response }; - } - return this.s3; - } }