Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Oct 21, 2022
1 parent 11bd407 commit 463d72d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 56 deletions.
26 changes: 0 additions & 26 deletions src/app/service-code-way/uploader-ext.class.ts

This file was deleted.

43 changes: 13 additions & 30 deletions src/app/service-code-way/uploaderx-s3.class.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { store, UploaderX } from 'ngx-uploadx';
export interface S3Multipart {
partSize: number;
name: string;
UploadId: string;
partsUrls: string[];
Parts?: Part[];
}
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;

Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -58,27 +55,13 @@ 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 ??= [];
return Math.min(this.s3.Parts.length * this.s3.partSize, this.size);
}
return super.getOffset();
}

private async setMetadata(url: string): Promise<S3Multipart> {
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;
}
}

0 comments on commit 463d72d

Please sign in to comment.