@webiny/app-file-manager-s3 0.0.0-unstable.78f581c1d2 → 0.0.0-unstable.7be00a75a9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MultiPartUploadAPI.d.ts +26 -0
- package/MultiPartUploadAPI.js +0 -0
- package/MultiPartUploadGraphQLAPI.d.ts +8 -0
- package/MultiPartUploadGraphQLAPI.js +72 -0
- package/MultiPartUploadGraphQLAPI.js.map +1 -0
- package/MultiPartUploadStrategy.d.ts +7 -0
- package/MultiPartUploadStrategy.js +32 -0
- package/MultiPartUploadStrategy.js.map +1 -0
- package/MultiPartUploader.d.ts +43 -0
- package/MultiPartUploader.js +139 -0
- package/MultiPartUploader.js.map +1 -0
- package/README.md +11 -1
- package/SimpleUploadStrategy.d.ts +11 -0
- package/SimpleUploadStrategy.js +47 -0
- package/SimpleUploadStrategy.js.map +1 -0
- package/graphql.d.ts +1 -0
- package/graphql.js +25 -0
- package/graphql.js.map +1 -0
- package/index.d.ts +5 -2
- package/index.js +18 -101
- package/index.js.map +1 -1
- package/package.json +17 -19
- package/types.d.ts +0 -10
- package/types.js +0 -5
- package/types.js.map +0 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { UploadedFile } from "@webiny/app/types.js";
|
|
2
|
+
export interface CreateUploadParams {
|
|
3
|
+
data: {
|
|
4
|
+
size: number;
|
|
5
|
+
name: string;
|
|
6
|
+
type: string;
|
|
7
|
+
};
|
|
8
|
+
numberOfParts: number;
|
|
9
|
+
}
|
|
10
|
+
export interface CompleteUploadParams {
|
|
11
|
+
fileKey: string;
|
|
12
|
+
uploadId: string;
|
|
13
|
+
}
|
|
14
|
+
export interface FilePart {
|
|
15
|
+
partNumber: number;
|
|
16
|
+
url: string;
|
|
17
|
+
}
|
|
18
|
+
export interface MultiPartUpload {
|
|
19
|
+
file: UploadedFile;
|
|
20
|
+
uploadId: string;
|
|
21
|
+
parts: FilePart[];
|
|
22
|
+
}
|
|
23
|
+
export interface MultiPartUploadAPI {
|
|
24
|
+
createUpload(params: CreateUploadParams): Promise<MultiPartUpload>;
|
|
25
|
+
completeUpload(params: CompleteUploadParams): Promise<boolean>;
|
|
26
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { UploadOptions } from "@webiny/app/types.js";
|
|
2
|
+
import type { CompleteUploadParams, CreateUploadParams, MultiPartUpload, MultiPartUploadAPI } from "./MultiPartUploadAPI.js";
|
|
3
|
+
export declare class MultiPartUploadGraphQLAPI implements MultiPartUploadAPI {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(client: UploadOptions["apolloClient"]);
|
|
6
|
+
createUpload(params: CreateUploadParams): Promise<MultiPartUpload>;
|
|
7
|
+
completeUpload(params: CompleteUploadParams): Promise<boolean>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import graphql_tag from "graphql-tag";
|
|
2
|
+
class MultiPartUploadGraphQLAPI {
|
|
3
|
+
constructor(client){
|
|
4
|
+
this.client = client;
|
|
5
|
+
}
|
|
6
|
+
async createUpload(params) {
|
|
7
|
+
const { data, errors } = await this.client.mutate({
|
|
8
|
+
mutation: CREATE_UPLOAD,
|
|
9
|
+
variables: params
|
|
10
|
+
});
|
|
11
|
+
if (!data) {
|
|
12
|
+
console.error(errors);
|
|
13
|
+
throw new Error("Failed to initialize a multi-part file upload!");
|
|
14
|
+
}
|
|
15
|
+
return data.fileManager.createMultiPartUpload.data;
|
|
16
|
+
}
|
|
17
|
+
async completeUpload(params) {
|
|
18
|
+
const { data, errors } = await this.client.mutate({
|
|
19
|
+
mutation: COMPLETE_UPLOAD,
|
|
20
|
+
variables: params
|
|
21
|
+
});
|
|
22
|
+
if (!data) {
|
|
23
|
+
console.error(errors);
|
|
24
|
+
throw new Error("Failed to complete a multi-part file upload!");
|
|
25
|
+
}
|
|
26
|
+
return data.fileManager.completeMultiPartUpload.data;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const CREATE_UPLOAD = graphql_tag`
|
|
30
|
+
mutation CreateMultiPartUpload($data: PreSignedPostPayloadInput!, $numberOfParts: Number!) {
|
|
31
|
+
fileManager {
|
|
32
|
+
createMultiPartUpload(data: $data, numberOfParts: $numberOfParts) {
|
|
33
|
+
data {
|
|
34
|
+
file {
|
|
35
|
+
id
|
|
36
|
+
key
|
|
37
|
+
name
|
|
38
|
+
size
|
|
39
|
+
type
|
|
40
|
+
}
|
|
41
|
+
uploadId
|
|
42
|
+
parts {
|
|
43
|
+
partNumber
|
|
44
|
+
url
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
error {
|
|
48
|
+
code
|
|
49
|
+
message
|
|
50
|
+
data
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
const COMPLETE_UPLOAD = graphql_tag`
|
|
57
|
+
mutation CompleteMultiPartUpload($fileKey: String!, $uploadId: String!) {
|
|
58
|
+
fileManager {
|
|
59
|
+
completeMultiPartUpload(fileKey: $fileKey, uploadId: $uploadId) {
|
|
60
|
+
data
|
|
61
|
+
error {
|
|
62
|
+
code
|
|
63
|
+
message
|
|
64
|
+
data
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
export { MultiPartUploadGraphQLAPI };
|
|
71
|
+
|
|
72
|
+
//# sourceMappingURL=MultiPartUploadGraphQLAPI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultiPartUploadGraphQLAPI.js","sources":["../src/MultiPartUploadGraphQLAPI.ts"],"sourcesContent":["import type { UploadOptions } from \"@webiny/app/types.js\";\nimport gql from \"graphql-tag\";\nimport type {\n CompleteUploadParams,\n CreateUploadParams,\n MultiPartUpload,\n MultiPartUploadAPI\n} from \"~/MultiPartUploadAPI.js\";\n\nexport class MultiPartUploadGraphQLAPI implements MultiPartUploadAPI {\n private client: UploadOptions[\"apolloClient\"];\n\n constructor(client: UploadOptions[\"apolloClient\"]) {\n this.client = client;\n }\n\n async createUpload(params: CreateUploadParams): Promise<MultiPartUpload> {\n const { data, errors } = await this.client.mutate<CreateUploadResponse>({\n mutation: CREATE_UPLOAD,\n variables: params\n });\n\n if (!data) {\n console.error(errors);\n throw new Error(`Failed to initialize a multi-part file upload!`);\n }\n\n return data.fileManager.createMultiPartUpload.data;\n }\n\n async completeUpload(params: CompleteUploadParams): Promise<boolean> {\n const { data, errors } = await this.client.mutate<CompleteUploadResponse>({\n mutation: COMPLETE_UPLOAD,\n variables: params\n });\n\n if (!data) {\n console.error(errors);\n throw new Error(`Failed to complete a multi-part file upload!`);\n }\n\n return data.fileManager.completeMultiPartUpload.data;\n }\n}\n\nconst CREATE_UPLOAD = gql`\n mutation CreateMultiPartUpload($data: PreSignedPostPayloadInput!, $numberOfParts: Number!) {\n fileManager {\n createMultiPartUpload(data: $data, numberOfParts: $numberOfParts) {\n data {\n file {\n id\n key\n name\n size\n type\n }\n uploadId\n parts {\n partNumber\n url\n }\n }\n error {\n code\n message\n data\n }\n }\n }\n }\n`;\n\ninterface CreateUploadResponse {\n fileManager: {\n createMultiPartUpload: {\n data: MultiPartUpload;\n error: {\n code: string;\n message: string;\n data: Record<string, any>;\n };\n };\n };\n}\n\nconst COMPLETE_UPLOAD = gql`\n mutation CompleteMultiPartUpload($fileKey: String!, $uploadId: String!) {\n fileManager {\n completeMultiPartUpload(fileKey: $fileKey, uploadId: $uploadId) {\n data\n error {\n code\n message\n data\n }\n }\n }\n }\n`;\n\ninterface CompleteUploadResponse {\n fileManager: {\n completeMultiPartUpload: {\n data: boolean;\n error: {\n code: string;\n message: string;\n data: Record<string, any>;\n };\n };\n };\n}\n"],"names":["MultiPartUploadGraphQLAPI","client","params","data","errors","CREATE_UPLOAD","console","Error","COMPLETE_UPLOAD","gql"],"mappings":";AASO,MAAMA;IAGT,YAAYC,MAAqC,CAAE;QAC/C,IAAI,CAAC,MAAM,GAAGA;IAClB;IAEA,MAAM,aAAaC,MAA0B,EAA4B;QACrE,MAAM,EAAEC,IAAI,EAAEC,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAuB;YACpE,UAAUC;YACV,WAAWH;QACf;QAEA,IAAI,CAACC,MAAM;YACPG,QAAQ,KAAK,CAACF;YACd,MAAM,IAAIG,MAAM;QACpB;QAEA,OAAOJ,KAAK,WAAW,CAAC,qBAAqB,CAAC,IAAI;IACtD;IAEA,MAAM,eAAeD,MAA4B,EAAoB;QACjE,MAAM,EAAEC,IAAI,EAAEC,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAyB;YACtE,UAAUI;YACV,WAAWN;QACf;QAEA,IAAI,CAACC,MAAM;YACPG,QAAQ,KAAK,CAACF;YACd,MAAM,IAAIG,MAAM;QACpB;QAEA,OAAOJ,KAAK,WAAW,CAAC,uBAAuB,CAAC,IAAI;IACxD;AACJ;AAEA,MAAME,gBAAgBI,WAAG,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;AA0B1B,CAAC;AAeD,MAAMD,kBAAkBC,WAAG,CAAC;;;;;;;;;;;;;AAa5B,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { UploadedFile, UploadOptions } from "@webiny/app/types.js";
|
|
2
|
+
import type { FileUploadStrategy } from "./index.js";
|
|
3
|
+
export declare class MultiPartUploadStrategy implements FileUploadStrategy {
|
|
4
|
+
upload(file: File, options: UploadOptions): Promise<UploadedFile>;
|
|
5
|
+
private detectChunkSize;
|
|
6
|
+
private detectParallelChunks;
|
|
7
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { MultiPartUploader } from "./MultiPartUploader.js";
|
|
2
|
+
import { MultiPartUploadGraphQLAPI } from "./MultiPartUploadGraphQLAPI.js";
|
|
3
|
+
class MultiPartUploadStrategy {
|
|
4
|
+
async upload(file, options) {
|
|
5
|
+
const api = new MultiPartUploadGraphQLAPI(options.apolloClient);
|
|
6
|
+
const uploader = new MultiPartUploader(api, {
|
|
7
|
+
chunkSize: this.detectChunkSize(),
|
|
8
|
+
parallelUploads: this.detectParallelChunks()
|
|
9
|
+
});
|
|
10
|
+
if (options.onProgress) uploader.onProgress(options.onProgress);
|
|
11
|
+
return uploader.uploadFile(file);
|
|
12
|
+
}
|
|
13
|
+
detectChunkSize() {
|
|
14
|
+
const envSize = parseInt(process.env.WEBINY_FILE_UPLOAD_CHUNK_SIZE || "0");
|
|
15
|
+
if ("development" === process.env.NODE_ENV) {
|
|
16
|
+
const windowSize = window["fmUploadChunkSize"];
|
|
17
|
+
if (windowSize) return windowSize;
|
|
18
|
+
}
|
|
19
|
+
return envSize || 50;
|
|
20
|
+
}
|
|
21
|
+
detectParallelChunks() {
|
|
22
|
+
const envChunks = parseInt(process.env.WEBINY_FILE_UPLOAD_PARALLEL_CHUNKS || "0");
|
|
23
|
+
if ("development" === process.env.NODE_ENV) {
|
|
24
|
+
const windowChunks = window["fmUploadParallelChunks"];
|
|
25
|
+
if (windowChunks) return windowChunks;
|
|
26
|
+
}
|
|
27
|
+
return envChunks || 5;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export { MultiPartUploadStrategy };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=MultiPartUploadStrategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultiPartUploadStrategy.js","sources":["../src/MultiPartUploadStrategy.ts"],"sourcesContent":["import type { UploadedFile, UploadOptions } from \"@webiny/app/types.js\";\nimport type { FileUploadStrategy } from \"~/index.js\";\nimport { MultiPartUploader } from \"~/MultiPartUploader.js\";\nimport { MultiPartUploadGraphQLAPI } from \"~/MultiPartUploadGraphQLAPI.js\";\n\nexport class MultiPartUploadStrategy implements FileUploadStrategy {\n async upload(file: File, options: UploadOptions): Promise<UploadedFile> {\n const api = new MultiPartUploadGraphQLAPI(options.apolloClient);\n\n const uploader = new MultiPartUploader(api, {\n chunkSize: this.detectChunkSize(),\n parallelUploads: this.detectParallelChunks()\n });\n\n if (options.onProgress) {\n uploader.onProgress(options.onProgress);\n }\n return uploader.uploadFile(file);\n }\n\n private detectChunkSize(): number {\n const envSize = parseInt(process.env.WEBINY_FILE_UPLOAD_CHUNK_SIZE || \"0\");\n\n // For dev purposes, we take this global var into consideration.\n if (process.env.NODE_ENV === \"development\") {\n // @ts-expect-error\n const windowSize = window[\"fmUploadChunkSize\"];\n if (windowSize) {\n return windowSize;\n }\n }\n\n // As a last resort, we check the baked in value, or fall back to 50MB chunk size.\n return envSize || 50;\n }\n\n private detectParallelChunks(): number {\n const envChunks = parseInt(process.env.WEBINY_FILE_UPLOAD_PARALLEL_CHUNKS || \"0\");\n\n // For dev purposes, we take this global var into consideration.\n if (process.env.NODE_ENV === \"development\") {\n // @ts-expect-error\n const windowChunks = window[\"fmUploadParallelChunks\"];\n if (windowChunks) {\n return windowChunks;\n }\n }\n\n // As a last resort, we check the baked in value, or fall back to 5 parallel chunks.\n return envChunks || 5;\n }\n}\n"],"names":["MultiPartUploadStrategy","file","options","api","MultiPartUploadGraphQLAPI","uploader","MultiPartUploader","envSize","parseInt","process","windowSize","window","envChunks","windowChunks"],"mappings":";;AAKO,MAAMA;IACT,MAAM,OAAOC,IAAU,EAAEC,OAAsB,EAAyB;QACpE,MAAMC,MAAM,IAAIC,0BAA0BF,QAAQ,YAAY;QAE9D,MAAMG,WAAW,IAAIC,kBAAkBH,KAAK;YACxC,WAAW,IAAI,CAAC,eAAe;YAC/B,iBAAiB,IAAI,CAAC,oBAAoB;QAC9C;QAEA,IAAID,QAAQ,UAAU,EAClBG,SAAS,UAAU,CAACH,QAAQ,UAAU;QAE1C,OAAOG,SAAS,UAAU,CAACJ;IAC/B;IAEQ,kBAA0B;QAC9B,MAAMM,UAAUC,SAASC,QAAQ,GAAG,CAAC,6BAA6B,IAAI;QAGtE,IAAIA,AAAyB,kBAAzBA,QAAQ,GAAG,CAAC,QAAQ,EAAoB;YAExC,MAAMC,aAAaC,MAAM,CAAC,oBAAoB;YAC9C,IAAID,YACA,OAAOA;QAEf;QAGA,OAAOH,WAAW;IACtB;IAEQ,uBAA+B;QACnC,MAAMK,YAAYJ,SAASC,QAAQ,GAAG,CAAC,kCAAkC,IAAI;QAG7E,IAAIA,AAAyB,kBAAzBA,QAAQ,GAAG,CAAC,QAAQ,EAAoB;YAExC,MAAMI,eAAeF,MAAM,CAAC,yBAAyB;YACrD,IAAIE,cACA,OAAOA;QAEf;QAGA,OAAOD,aAAa;IACxB;AACJ"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { MultiPartUploadAPI } from "./MultiPartUploadAPI.js";
|
|
2
|
+
interface MultiPartUploaderOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Chunk size in MB. Must be >=5MB (enforced by AWS).
|
|
5
|
+
*/
|
|
6
|
+
chunkSize?: number;
|
|
7
|
+
/**
|
|
8
|
+
* Number of parallel uploads. Must be >=5 && <=15.
|
|
9
|
+
*/
|
|
10
|
+
parallelUploads?: number;
|
|
11
|
+
}
|
|
12
|
+
interface OnProgressCallback {
|
|
13
|
+
(params: {
|
|
14
|
+
sent: number;
|
|
15
|
+
total: number;
|
|
16
|
+
percentage: number;
|
|
17
|
+
}): void;
|
|
18
|
+
}
|
|
19
|
+
interface OnErrorCallback {
|
|
20
|
+
(error: Error): void;
|
|
21
|
+
}
|
|
22
|
+
export declare class MultiPartUploader {
|
|
23
|
+
private readonly api;
|
|
24
|
+
private readonly chunkSize;
|
|
25
|
+
private readonly parallelUploads;
|
|
26
|
+
private readonly activeConnections;
|
|
27
|
+
private readonly progressTracker;
|
|
28
|
+
private upload;
|
|
29
|
+
private file;
|
|
30
|
+
private onErrorFn;
|
|
31
|
+
private onProgressFn;
|
|
32
|
+
constructor(api: MultiPartUploadAPI, options?: MultiPartUploaderOptions);
|
|
33
|
+
uploadFile(file: File): Promise<import("@webiny/app/types.js").UploadedFile>;
|
|
34
|
+
onProgress(onProgress: OnProgressCallback): this;
|
|
35
|
+
onError(onError: OnErrorCallback): this;
|
|
36
|
+
private complete;
|
|
37
|
+
private sendCompleteRequest;
|
|
38
|
+
private progressListener;
|
|
39
|
+
private uploadNextPart;
|
|
40
|
+
private uploadPart;
|
|
41
|
+
private assertIsDefined;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import p_retry from "p-retry";
|
|
2
|
+
class MultiPartUploader {
|
|
3
|
+
constructor(api, options = {}){
|
|
4
|
+
this.activeConnections = new Map();
|
|
5
|
+
this.progressTracker = new Map();
|
|
6
|
+
this.onErrorFn = (error)=>console.error(error);
|
|
7
|
+
this.api = api;
|
|
8
|
+
const chunkSize = options.chunkSize || 50;
|
|
9
|
+
const parallelUploads = options.parallelUploads || 5;
|
|
10
|
+
this.chunkSize = Math.max(1048576 * chunkSize, 5242880);
|
|
11
|
+
this.parallelUploads = Math.min(parallelUploads, 15);
|
|
12
|
+
}
|
|
13
|
+
async uploadFile(file) {
|
|
14
|
+
this.file = file;
|
|
15
|
+
const numberOfParts = Math.ceil(file.size / this.chunkSize);
|
|
16
|
+
try {
|
|
17
|
+
this.upload = await this.api.createUpload({
|
|
18
|
+
data: {
|
|
19
|
+
name: file.name,
|
|
20
|
+
size: file.size,
|
|
21
|
+
type: file.type
|
|
22
|
+
},
|
|
23
|
+
numberOfParts
|
|
24
|
+
});
|
|
25
|
+
const threads = Math.min(numberOfParts, this.parallelUploads);
|
|
26
|
+
await Promise.all(Array.from({
|
|
27
|
+
length: threads
|
|
28
|
+
}).map(()=>this.uploadNextPart()));
|
|
29
|
+
await this.complete();
|
|
30
|
+
return this.upload.file;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
await this.complete(error);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
onProgress(onProgress) {
|
|
37
|
+
this.onProgressFn = onProgress;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
onError(onError) {
|
|
41
|
+
this.onErrorFn = onError;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
async complete(error) {
|
|
45
|
+
if (error) return void this.onErrorFn(error);
|
|
46
|
+
try {
|
|
47
|
+
await this.sendCompleteRequest();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
this.onErrorFn(error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async sendCompleteRequest() {
|
|
53
|
+
this.assertIsDefined(this.upload, 'Upload must be created before calling "sendCompleteRequest"!');
|
|
54
|
+
return this.api.completeUpload({
|
|
55
|
+
fileKey: this.upload.file.key,
|
|
56
|
+
uploadId: this.upload.uploadId
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
progressListener(part, event) {
|
|
60
|
+
if (!this.file) return;
|
|
61
|
+
this.progressTracker.set(part.partNumber, event.loaded);
|
|
62
|
+
const uploaded = Array.from(this.progressTracker.values()).reduce((sum = 0, value)=>sum + value);
|
|
63
|
+
const uploadedSize = Math.min(uploaded, this.file.size);
|
|
64
|
+
if (this.onProgressFn) try {
|
|
65
|
+
this.onProgressFn({
|
|
66
|
+
sent: uploadedSize,
|
|
67
|
+
total: this.file.size,
|
|
68
|
+
percentage: Math.round(uploadedSize / this.file.size * 100)
|
|
69
|
+
});
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error('Error executing the "onProgress" callback', err);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async uploadNextPart() {
|
|
75
|
+
if (!this.upload) return;
|
|
76
|
+
const part = this.upload.parts.shift();
|
|
77
|
+
if (!part) return;
|
|
78
|
+
return executeWithRetry(()=>this.uploadPart(part)).then(()=>this.uploadNextPart());
|
|
79
|
+
}
|
|
80
|
+
uploadPart(part) {
|
|
81
|
+
this.assertIsDefined(this.upload, 'Upload must be created before calling "sendCompleteRequest"!');
|
|
82
|
+
this.assertIsDefined(this.file, 'File must be set before calling "uploadPart"!');
|
|
83
|
+
const sentSize = (part.partNumber - 1) * this.chunkSize;
|
|
84
|
+
const nextChunkSize = Math.min(sentSize + this.chunkSize, this.file.size);
|
|
85
|
+
const chunk = this.file.slice(sentSize, nextChunkSize, this.upload.file.type);
|
|
86
|
+
console.log(`Chunk for part #${part.partNumber}`, chunk.size);
|
|
87
|
+
return new Promise((resolve, reject)=>{
|
|
88
|
+
const throwXHRError = (error)=>{
|
|
89
|
+
this.activeConnections.delete(part.partNumber);
|
|
90
|
+
reject(error);
|
|
91
|
+
};
|
|
92
|
+
if (!window.navigator.onLine) return reject(new Error("Browser is offline!"));
|
|
93
|
+
const xhr = new XMLHttpRequest();
|
|
94
|
+
this.activeConnections.set(part.partNumber, xhr);
|
|
95
|
+
const abortXHR = ()=>xhr.abort();
|
|
96
|
+
xhr.upload.addEventListener("progress", (event)=>this.progressListener(part, event));
|
|
97
|
+
window.addEventListener("offline", abortXHR);
|
|
98
|
+
const removeListeners = ()=>{
|
|
99
|
+
window.removeEventListener("offline", abortXHR);
|
|
100
|
+
};
|
|
101
|
+
xhr.open("PUT", part.url);
|
|
102
|
+
xhr.onreadystatechange = ()=>{
|
|
103
|
+
if (4 === xhr.readyState && 200 === xhr.status) try {
|
|
104
|
+
this.activeConnections.delete(part.partNumber);
|
|
105
|
+
window.removeEventListener("offline", abortXHR);
|
|
106
|
+
resolve(xhr.status);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.error('Error in "onreadystatechange"', err);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
xhr.onerror = ()=>{
|
|
112
|
+
removeListeners();
|
|
113
|
+
throwXHRError(new Error(`Failed to upload file part #${part.partNumber}`));
|
|
114
|
+
};
|
|
115
|
+
xhr.ontimeout = ()=>{
|
|
116
|
+
removeListeners();
|
|
117
|
+
throwXHRError(new Error(`Request timed out for file part #${part.partNumber}!`));
|
|
118
|
+
};
|
|
119
|
+
xhr.onabort = ()=>{
|
|
120
|
+
removeListeners();
|
|
121
|
+
throwXHRError(new Error(`Upload was cancelled for part #${part.partNumber}!`));
|
|
122
|
+
};
|
|
123
|
+
xhr.send(chunk);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
assertIsDefined(upload, message) {
|
|
127
|
+
if (!upload) throw new Error(message);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const executeWithRetry = (execute, options)=>p_retry(execute, {
|
|
131
|
+
maxRetryTime: 300000,
|
|
132
|
+
retries: 5,
|
|
133
|
+
minTimeout: 1500,
|
|
134
|
+
maxTimeout: 30000,
|
|
135
|
+
...options
|
|
136
|
+
});
|
|
137
|
+
export { MultiPartUploader };
|
|
138
|
+
|
|
139
|
+
//# sourceMappingURL=MultiPartUploader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultiPartUploader.js","sources":["../src/MultiPartUploader.ts"],"sourcesContent":["import pRetry from \"p-retry\";\nimport type { MultiPartUpload, MultiPartUploadAPI, FilePart } from \"./MultiPartUploadAPI.js\";\n\ninterface MultiPartUploaderOptions {\n /**\n * Chunk size in MB. Must be >=5MB (enforced by AWS).\n */\n chunkSize?: number;\n /**\n * Number of parallel uploads. Must be >=5 && <=15.\n */\n parallelUploads?: number;\n}\n\ninterface OnProgressCallback {\n (params: { sent: number; total: number; percentage: number }): void;\n}\n\ninterface OnErrorCallback {\n (error: Error): void;\n}\n\nexport class MultiPartUploader {\n private readonly api: MultiPartUploadAPI;\n private readonly chunkSize: number;\n private readonly parallelUploads: number;\n private readonly activeConnections: Map<number, XMLHttpRequest> = new Map();\n private readonly progressTracker: Map<number, number> = new Map();\n private upload: MultiPartUpload | undefined;\n private file: File | undefined;\n private onErrorFn: OnErrorCallback = error => console.error(error);\n private onProgressFn: OnProgressCallback | undefined;\n\n constructor(api: MultiPartUploadAPI, options: MultiPartUploaderOptions = {}) {\n this.api = api;\n const chunkSize = options.chunkSize || 50;\n const parallelUploads = options.parallelUploads || 5;\n\n this.chunkSize = Math.max(1024 * 1024 * chunkSize, 1024 * 1024 * 5);\n this.parallelUploads = Math.min(parallelUploads, 15);\n }\n\n async uploadFile(file: File) {\n this.file = file;\n const numberOfParts = Math.ceil(file.size / this.chunkSize);\n\n try {\n /**\n * Initialize the file upload on AWS S3.\n */\n this.upload = await this.api.createUpload({\n data: { name: file.name, size: file.size, type: file.type },\n numberOfParts\n });\n\n /**\n * Run the defined number of parallel uploads. Each thread will continue to process parts\n * for as long as there are parts to upload. The promise will resolve once there's no more parts to upload.\n */\n const threads = Math.min(numberOfParts, this.parallelUploads);\n await Promise.all(Array.from({ length: threads }).map(() => this.uploadNextPart()));\n\n await this.complete();\n\n return this.upload.file;\n } catch (error) {\n await this.complete(error);\n throw error;\n }\n }\n\n onProgress(onProgress: OnProgressCallback) {\n this.onProgressFn = onProgress;\n return this;\n }\n\n onError(onError: OnErrorCallback) {\n this.onErrorFn = onError;\n return this;\n }\n\n private async complete(error?: Error) {\n if (error) {\n this.onErrorFn(error);\n return;\n }\n\n try {\n await this.sendCompleteRequest();\n } catch (error) {\n this.onErrorFn(error);\n }\n }\n\n private async sendCompleteRequest() {\n this.assertIsDefined(\n this.upload,\n `Upload must be created before calling \"sendCompleteRequest\"!`\n );\n\n return this.api.completeUpload({\n fileKey: this.upload.file.key,\n uploadId: this.upload.uploadId\n });\n }\n\n private progressListener(part: FilePart, event: ProgressEvent<XMLHttpRequestEventTarget>) {\n if (!this.file) {\n return;\n }\n\n this.progressTracker.set(part.partNumber, event.loaded);\n\n const uploaded = Array.from(this.progressTracker.values()).reduce(\n (sum = 0, value) => sum + value\n );\n\n const uploadedSize = Math.min(uploaded, this.file.size);\n\n if (this.onProgressFn) {\n try {\n this.onProgressFn({\n sent: uploadedSize,\n total: this.file.size,\n percentage: Math.round((uploadedSize / this.file.size) * 100)\n });\n } catch (err) {\n console.error(`Error executing the \"onProgress\" callback`, err);\n }\n }\n }\n\n private async uploadNextPart(): Promise<void> {\n if (!this.upload) {\n return;\n }\n\n const part = this.upload.parts.shift();\n\n if (!part) {\n return;\n }\n\n return executeWithRetry(() => this.uploadPart(part)).then(() => this.uploadNextPart());\n }\n\n private uploadPart(part: FilePart) {\n this.assertIsDefined(\n this.upload,\n `Upload must be created before calling \"sendCompleteRequest\"!`\n );\n\n this.assertIsDefined(this.file, `File must be set before calling \"uploadPart\"!`);\n\n const sentSize = (part.partNumber - 1) * this.chunkSize;\n const nextChunkSize = Math.min(sentSize + this.chunkSize, this.file.size);\n const chunk = this.file.slice(sentSize, nextChunkSize, this.upload.file.type);\n console.log(`Chunk for part #${part.partNumber}`, chunk.size);\n\n return new Promise((resolve, reject) => {\n const throwXHRError = (error: Error) => {\n this.activeConnections.delete(part.partNumber);\n reject(error);\n };\n\n if (!window.navigator.onLine) {\n return reject(new Error(\"Browser is offline!\"));\n }\n\n const xhr = new XMLHttpRequest();\n this.activeConnections.set(part.partNumber, xhr);\n const abortXHR = () => xhr.abort();\n\n xhr.upload.addEventListener(\"progress\", event => this.progressListener(part, event));\n\n window.addEventListener(\"offline\", abortXHR);\n const removeListeners = () => {\n window.removeEventListener(\"offline\", abortXHR);\n };\n\n xhr.open(\"PUT\", part.url);\n\n xhr.onreadystatechange = () => {\n if (xhr.readyState === 4 && xhr.status === 200) {\n try {\n this.activeConnections.delete(part.partNumber);\n window.removeEventListener(\"offline\", abortXHR);\n resolve(xhr.status);\n } catch (err) {\n console.error(`Error in \"onreadystatechange\"`, err);\n }\n }\n };\n\n xhr.onerror = () => {\n removeListeners();\n throwXHRError(new Error(`Failed to upload file part #${part.partNumber}`));\n };\n xhr.ontimeout = () => {\n removeListeners();\n throwXHRError(new Error(`Request timed out for file part #${part.partNumber}!`));\n };\n xhr.onabort = () => {\n removeListeners();\n throwXHRError(new Error(`Upload was cancelled for part #${part.partNumber}!`));\n };\n xhr.send(chunk);\n });\n }\n\n private assertIsDefined<T>(upload: T, message: string): asserts upload is NonNullable<T> {\n if (!upload) {\n throw new Error(message);\n }\n }\n}\n\nconst executeWithRetry = (execute: () => void, options?: Parameters<typeof pRetry>[1]) => {\n return pRetry(execute, {\n maxRetryTime: 300000,\n retries: 5,\n minTimeout: 1500,\n maxTimeout: 30000,\n ...options\n });\n};\n"],"names":["MultiPartUploader","api","options","Map","error","console","chunkSize","parallelUploads","Math","file","numberOfParts","threads","Promise","Array","onProgress","onError","part","event","uploaded","sum","value","uploadedSize","err","executeWithRetry","sentSize","nextChunkSize","chunk","resolve","reject","throwXHRError","window","Error","xhr","XMLHttpRequest","abortXHR","removeListeners","upload","message","execute","pRetry"],"mappings":";AAsBO,MAAMA;IAWT,YAAYC,GAAuB,EAAEC,UAAoC,CAAC,CAAC,CAAE;aAP5D,iBAAiB,GAAgC,IAAIC;aACrD,eAAe,GAAwB,IAAIA;aAGpD,SAAS,GAAoBC,CAAAA,QAASC,QAAQ,KAAK,CAACD;QAIxD,IAAI,CAAC,GAAG,GAAGH;QACX,MAAMK,YAAYJ,QAAQ,SAAS,IAAI;QACvC,MAAMK,kBAAkBL,QAAQ,eAAe,IAAI;QAEnD,IAAI,CAAC,SAAS,GAAGM,KAAK,GAAG,CAAC,UAAcF,WAAW;QACnD,IAAI,CAAC,eAAe,GAAGE,KAAK,GAAG,CAACD,iBAAiB;IACrD;IAEA,MAAM,WAAWE,IAAU,EAAE;QACzB,IAAI,CAAC,IAAI,GAAGA;QACZ,MAAMC,gBAAgBF,KAAK,IAAI,CAACC,KAAK,IAAI,GAAG,IAAI,CAAC,SAAS;QAE1D,IAAI;YAIA,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC;gBACtC,MAAM;oBAAE,MAAMA,KAAK,IAAI;oBAAE,MAAMA,KAAK,IAAI;oBAAE,MAAMA,KAAK,IAAI;gBAAC;gBAC1DC;YACJ;YAMA,MAAMC,UAAUH,KAAK,GAAG,CAACE,eAAe,IAAI,CAAC,eAAe;YAC5D,MAAME,QAAQ,GAAG,CAACC,MAAM,IAAI,CAAC;gBAAE,QAAQF;YAAQ,GAAG,GA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QACZ,GAAGpC,OAAO;IACd"}
|
package/README.md
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
# @webiny/app-file-manager-s3
|
|
1
|
+
# @webiny/app-file-manager-s3
|
|
2
|
+
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
6
|
+
|
|
7
|
+
📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { UploadedFile, UploadOptions } from "@webiny/app/types.js";
|
|
2
|
+
import type { FileUploadStrategy } from "./index.js";
|
|
3
|
+
declare global {
|
|
4
|
+
interface File {
|
|
5
|
+
key?: string;
|
|
6
|
+
keyPrefix?: string;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare class SimpleUploadStrategy implements FileUploadStrategy {
|
|
10
|
+
upload(file: File, { apolloClient, onProgress }: UploadOptions): Promise<UploadedFile>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { GET_PRE_SIGNED_POST_PAYLOAD } from "./graphql.js";
|
|
2
|
+
class SimpleUploadStrategy {
|
|
3
|
+
async upload(file, { apolloClient, onProgress }) {
|
|
4
|
+
const response = await apolloClient.query({
|
|
5
|
+
query: GET_PRE_SIGNED_POST_PAYLOAD,
|
|
6
|
+
fetchPolicy: "no-cache",
|
|
7
|
+
variables: {
|
|
8
|
+
data: {
|
|
9
|
+
size: file.size,
|
|
10
|
+
name: file.name,
|
|
11
|
+
type: file.type,
|
|
12
|
+
key: file.key,
|
|
13
|
+
keyPrefix: file.keyPrefix
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
const { getPreSignedPostPayload } = response.data.fileManager;
|
|
18
|
+
if (getPreSignedPostPayload.error) {
|
|
19
|
+
console.error(getPreSignedPostPayload);
|
|
20
|
+
throw Error(getPreSignedPostPayload.error);
|
|
21
|
+
}
|
|
22
|
+
return new Promise((resolve, reject)=>{
|
|
23
|
+
const formData = new window.FormData();
|
|
24
|
+
Object.keys(getPreSignedPostPayload.data.data.fields).forEach((key)=>{
|
|
25
|
+
formData.append(key, getPreSignedPostPayload.data.data.fields[key]);
|
|
26
|
+
});
|
|
27
|
+
formData.append("file", file);
|
|
28
|
+
const xhr = new window.XMLHttpRequest();
|
|
29
|
+
xhr.upload.addEventListener("progress", (event)=>{
|
|
30
|
+
if (onProgress) onProgress({
|
|
31
|
+
sent: event.loaded,
|
|
32
|
+
total: file.size,
|
|
33
|
+
percentage: event.loaded / file.size * 100
|
|
34
|
+
});
|
|
35
|
+
}, false);
|
|
36
|
+
xhr.open("POST", getPreSignedPostPayload.data.data.url, true);
|
|
37
|
+
xhr.send(formData);
|
|
38
|
+
xhr.onload = function() {
|
|
39
|
+
if (204 === this.status) return void resolve(getPreSignedPostPayload.data.file);
|
|
40
|
+
reject(this.responseText);
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export { SimpleUploadStrategy };
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=SimpleUploadStrategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SimpleUploadStrategy.js","sources":["../src/SimpleUploadStrategy.ts"],"sourcesContent":["import type { UploadedFile, UploadOptions } from \"@webiny/app/types.js\";\nimport { GET_PRE_SIGNED_POST_PAYLOAD } from \"./graphql.js\";\nimport type { FileUploadStrategy } from \"~/index.js\";\n\ndeclare global {\n interface File {\n key?: string;\n keyPrefix?: string;\n }\n}\n\nexport class SimpleUploadStrategy implements FileUploadStrategy {\n async upload(file: File, { apolloClient, onProgress }: UploadOptions): Promise<UploadedFile> {\n // 1. GET PreSignedPostPayload\n const response = await apolloClient.query({\n query: GET_PRE_SIGNED_POST_PAYLOAD,\n fetchPolicy: \"no-cache\",\n variables: {\n data: {\n size: file.size,\n name: file.name,\n type: file.type,\n key: file.key,\n keyPrefix: file.keyPrefix\n }\n }\n });\n\n const { getPreSignedPostPayload } = response.data.fileManager;\n if (getPreSignedPostPayload.error) {\n console.error(getPreSignedPostPayload);\n throw Error(getPreSignedPostPayload.error);\n }\n\n // 2. upload file to S3\n return new Promise((resolve, reject) => {\n const formData = new window.FormData();\n Object.keys(getPreSignedPostPayload.data.data.fields).forEach(key => {\n formData.append(key, getPreSignedPostPayload.data.data.fields[key]);\n });\n\n formData.append(\"file\", file);\n\n const xhr = new window.XMLHttpRequest();\n xhr.upload.addEventListener(\n \"progress\",\n event => {\n if (onProgress) {\n onProgress({\n sent: event.loaded,\n total: file.size,\n percentage: (event.loaded / file.size) * 100\n });\n }\n },\n false\n );\n xhr.open(\"POST\", getPreSignedPostPayload.data.data.url, true);\n xhr.send(formData);\n xhr.onload = function () {\n if (this.status === 204) {\n resolve(getPreSignedPostPayload.data.file);\n return;\n }\n\n reject(this.responseText);\n };\n });\n }\n}\n"],"names":["SimpleUploadStrategy","file","apolloClient","onProgress","response","GET_PRE_SIGNED_POST_PAYLOAD","getPreSignedPostPayload","console","Error","Promise","resolve","reject","formData","window","Object","key","xhr","event"],"mappings":";AAWO,MAAMA;IACT,MAAM,OAAOC,IAAU,EAAE,EAAEC,YAAY,EAAEC,UAAU,EAAiB,EAAyB;QAEzF,MAAMC,WAAW,MAAMF,aAAa,KAAK,CAAC;YACtC,OAAOG;YACP,aAAa;YACb,WAAW;gBACP,MAAM;oBACF,MAAMJ,KAAK,IAAI;oBACf,MAAMA,KAAK,IAAI;oBACf,MAAMA,KAAK,IAAI;oBACf,KAAKA,KAAK,GAAG;oBACb,WAAWA,KAAK,SAAS;gBAC7B;YACJ;QACJ;QAEA,MAAM,EAAEK,uBAAuB,EAAE,GAAGF,SAAS,IAAI,CAAC,WAAW;QAC7D,IAAIE,wBAAwB,KAAK,EAAE;YAC/BC,QAAQ,KAAK,CAACD;YACd,MAAME,MAAMF,wBAAwB,KAAK;QAC7C;QAGA,OAAO,IAAIG,QAAQ,CAACC,SAASC;YACzB,MAAMC,WAAW,IAAIC,OAAO,QAAQ;YACpCC,OAAO,IAAI,CAACR,wBAAwB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAACS,CAAAA;gBAC1DH,SAAS,MAAM,CAACG,KAAKT,wBAAwB,IAAI,CAAC,IAAI,CAAC,MAAM,CAACS,IAAI;YACtE;YAEAH,SAAS,MAAM,CAAC,QAAQX;YAExB,MAAMe,MAAM,IAAIH,OAAO,cAAc;YACrCG,IAAI,MAAM,CAAC,gBAAgB,CACvB,YACAC,CAAAA;gBACI,IAAId,YACAA,WAAW;oBACP,MAAMc,MAAM,MAAM;oBAClB,OAAOhB,KAAK,IAAI;oBAChB,YAAagB,MAAM,MAAM,GAAGhB,KAAK,IAAI,GAAI;gBAC7C;YAER,GACA;YAEJe,IAAI,IAAI,CAAC,QAAQV,wBAAwB,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YACxDU,IAAI,IAAI,CAACJ;YACTI,IAAI,MAAM,GAAG;gBACT,IAAI,AAAgB,QAAhB,IAAI,CAAC,MAAM,EAAU,YACrBN,QAAQJ,wBAAwB,IAAI,CAAC,IAAI;gBAI7CK,OAAO,IAAI,CAAC,YAAY;YAC5B;QACJ;IACJ;AACJ"}
|
package/graphql.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const GET_PRE_SIGNED_POST_PAYLOAD: import("graphql").DocumentNode;
|
package/graphql.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import graphql_tag from "graphql-tag";
|
|
2
|
+
const GET_PRE_SIGNED_POST_PAYLOAD = graphql_tag`
|
|
3
|
+
query getPreSignedPostPayload($data: PreSignedPostPayloadInput!) {
|
|
4
|
+
fileManager {
|
|
5
|
+
getPreSignedPostPayload(data: $data) {
|
|
6
|
+
data {
|
|
7
|
+
data
|
|
8
|
+
file {
|
|
9
|
+
id
|
|
10
|
+
type
|
|
11
|
+
name
|
|
12
|
+
size
|
|
13
|
+
key
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
error {
|
|
17
|
+
message
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
`;
|
|
23
|
+
export { GET_PRE_SIGNED_POST_PAYLOAD };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=graphql.js.map
|
package/graphql.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql.js","sources":["../src/graphql.ts"],"sourcesContent":["import gql from \"graphql-tag\";\n\nexport const GET_PRE_SIGNED_POST_PAYLOAD = gql`\n query getPreSignedPostPayload($data: PreSignedPostPayloadInput!) {\n fileManager {\n getPreSignedPostPayload(data: $data) {\n data {\n data\n file {\n id\n type\n name\n size\n key\n }\n }\n error {\n message\n }\n }\n }\n }\n`;\n"],"names":["GET_PRE_SIGNED_POST_PAYLOAD","gql"],"mappings":";AAEO,MAAMA,8BAA8BC,WAAG,CAAC;;;;;;;;;;;;;;;;;;;;AAoB/C,CAAC"}
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { FileUploaderPlugin } from "@webiny/app/types.js";
|
|
2
|
+
export interface FileUploadStrategy {
|
|
3
|
+
upload: FileUploaderPlugin["upload"];
|
|
4
|
+
}
|
|
5
|
+
declare const _default: () => FileUploaderPlugin;
|
|
3
6
|
export default _default;
|
package/index.js
CHANGED
|
@@ -1,103 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var _templateObject;
|
|
19
|
-
|
|
20
|
-
var GET_PRE_SIGNED_POST_PAYLOAD = (0, _graphqlTag.default)(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["\n query getPreSignedPostPayload($data: PreSignedPostPayloadInput!) {\n fileManager {\n getPreSignedPostPayload(data: $data) {\n data {\n data\n file {\n type\n name\n size\n key\n }\n }\n error {\n message\n }\n }\n }\n }\n"])));
|
|
21
|
-
|
|
22
|
-
var _default = function _default() {
|
|
23
|
-
return {
|
|
24
|
-
type: "app-file-manager-storage",
|
|
25
|
-
name: "app-file-manager-storage",
|
|
26
|
-
upload: function () {
|
|
27
|
-
var _upload = (0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee(file, _ref) {
|
|
28
|
-
var apolloClient, response, getPreSignedPostPayload;
|
|
29
|
-
return (0, _regeneratorRuntime2.default)().wrap(function _callee$(_context) {
|
|
30
|
-
while (1) {
|
|
31
|
-
switch (_context.prev = _context.next) {
|
|
32
|
-
case 0:
|
|
33
|
-
apolloClient = _ref.apolloClient;
|
|
34
|
-
_context.next = 3;
|
|
35
|
-
return apolloClient.query({
|
|
36
|
-
query: GET_PRE_SIGNED_POST_PAYLOAD,
|
|
37
|
-
fetchPolicy: "no-cache",
|
|
38
|
-
variables: {
|
|
39
|
-
data: {
|
|
40
|
-
size: file.size,
|
|
41
|
-
name: file.name,
|
|
42
|
-
type: file.type
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
case 3:
|
|
48
|
-
response = _context.sent;
|
|
49
|
-
getPreSignedPostPayload = response.data.fileManager.getPreSignedPostPayload;
|
|
50
|
-
|
|
51
|
-
if (!getPreSignedPostPayload.error) {
|
|
52
|
-
_context.next = 8;
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
console.log(getPreSignedPostPayload); // eslint-disable-line
|
|
57
|
-
|
|
58
|
-
return _context.abrupt("return");
|
|
59
|
-
|
|
60
|
-
case 8:
|
|
61
|
-
_context.next = 10;
|
|
62
|
-
return new Promise(function (resolve, reject) {
|
|
63
|
-
var formData = new window.FormData();
|
|
64
|
-
Object.keys(getPreSignedPostPayload.data.data.fields).forEach(function (key) {
|
|
65
|
-
formData.append(key, getPreSignedPostPayload.data.data.fields[key]);
|
|
66
|
-
});
|
|
67
|
-
formData.append("file", file);
|
|
68
|
-
var xhr = new window.XMLHttpRequest(); // eslint-disable-line
|
|
69
|
-
|
|
70
|
-
xhr.open("POST", getPreSignedPostPayload.data.data.url, true);
|
|
71
|
-
xhr.send(formData);
|
|
72
|
-
|
|
73
|
-
xhr.onload = function () {
|
|
74
|
-
if (this.status === 204) {
|
|
75
|
-
resolve(getPreSignedPostPayload.data.file);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
reject(this.responseText);
|
|
80
|
-
};
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
case 10:
|
|
84
|
-
return _context.abrupt("return", _context.sent);
|
|
85
|
-
|
|
86
|
-
case 11:
|
|
87
|
-
case "end":
|
|
88
|
-
return _context.stop();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}, _callee);
|
|
92
|
-
}));
|
|
93
|
-
|
|
94
|
-
function upload(_x, _x2) {
|
|
95
|
-
return _upload.apply(this, arguments);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return upload;
|
|
99
|
-
}()
|
|
100
|
-
};
|
|
1
|
+
import { SimpleUploadStrategy } from "./SimpleUploadStrategy.js";
|
|
2
|
+
import { MultiPartUploadStrategy } from "./MultiPartUploadStrategy.js";
|
|
3
|
+
const src = ()=>{
|
|
4
|
+
class S3FileUploader {
|
|
5
|
+
upload(file, options) {
|
|
6
|
+
const multiPartThreshold = window["fmUploadMultiPartThreshold"] ?? 100;
|
|
7
|
+
const simple = file.size < 1024 * multiPartThreshold * 1024;
|
|
8
|
+
const strategy = simple ? new SimpleUploadStrategy() : new MultiPartUploadStrategy();
|
|
9
|
+
return strategy.upload(file, options);
|
|
10
|
+
}
|
|
11
|
+
constructor(){
|
|
12
|
+
this.type = "file-uploader";
|
|
13
|
+
this.name = "file-uploader";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return new S3FileUploader();
|
|
101
17
|
};
|
|
18
|
+
export default src;
|
|
102
19
|
|
|
103
|
-
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import type { FileUploaderPlugin, UploadOptions } from \"@webiny/app/types.js\";\nimport { SimpleUploadStrategy } from \"~/SimpleUploadStrategy.js\";\nimport { MultiPartUploadStrategy } from \"~/MultiPartUploadStrategy.js\";\n\nexport interface FileUploadStrategy {\n upload: FileUploaderPlugin[\"upload\"];\n}\n\nexport default (): FileUploaderPlugin => {\n class S3FileUploader implements FileUploaderPlugin {\n public readonly type = \"file-uploader\";\n public readonly name = \"file-uploader\";\n\n upload(file: File, options: UploadOptions) {\n // Use \"simple\" strategy for files smaller than ~100MB\n // @ts-expect-error\n const multiPartThreshold = window[\"fmUploadMultiPartThreshold\"] ?? 100;\n const simple = file.size < multiPartThreshold * 1024 * 1024;\n\n const strategy: FileUploadStrategy = simple\n ? new SimpleUploadStrategy()\n : new MultiPartUploadStrategy();\n\n return strategy.upload(file, options);\n }\n }\n\n return new S3FileUploader();\n};\n"],"names":["S3FileUploader","file","options","multiPartThreshold","window","simple","strategy","SimpleUploadStrategy","MultiPartUploadStrategy"],"mappings":";;AAQA,YAAgB;IACZ,MAAMA;QAIF,OAAOC,IAAU,EAAEC,OAAsB,EAAE;YAGvC,MAAMC,qBAAqBC,MAAM,CAAC,6BAA6B,IAAI;YACnE,MAAMC,SAASJ,KAAK,IAAI,GAAGE,AAAqB,OAArBA,qBAA4B;YAEvD,MAAMG,WAA+BD,SAC/B,IAAIE,yBACJ,IAAIC;YAEV,OAAOF,SAAS,MAAM,CAACL,MAAMC;QACjC;;iBAdgB,IAAI,GAAG;iBACP,IAAI,GAAG;;IAc3B;IAEA,OAAO,IAAIF;AACf"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/app-file-manager-s3",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.0.0-unstable.7be00a75a9",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./index.js",
|
|
7
|
+
"./*": "./*"
|
|
8
|
+
},
|
|
5
9
|
"repository": {
|
|
6
10
|
"type": "git",
|
|
7
11
|
"url": "https://github.com/webiny/webiny-js.git"
|
|
@@ -10,26 +14,20 @@
|
|
|
10
14
|
"author": "Webiny Ltd",
|
|
11
15
|
"license": "MIT",
|
|
12
16
|
"dependencies": {
|
|
13
|
-
"@webiny/
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"graphql-tag": "2.12.6"
|
|
17
|
+
"@webiny/app": "0.0.0-unstable.7be00a75a9",
|
|
18
|
+
"graphql-tag": "2.12.6",
|
|
19
|
+
"p-retry": "8.0.0"
|
|
17
20
|
},
|
|
18
21
|
"devDependencies": {
|
|
19
|
-
"@
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"@webiny/project-utils": "^0.0.0-unstable.78f581c1d2",
|
|
23
|
-
"rimraf": "^3.0.2",
|
|
24
|
-
"typescript": "4.7.4"
|
|
22
|
+
"@webiny/build-tools": "0.0.0-unstable.7be00a75a9",
|
|
23
|
+
"rimraf": "6.1.3",
|
|
24
|
+
"typescript": "6.0.3"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
|
-
"access": "public"
|
|
28
|
-
"directory": "dist"
|
|
29
|
-
},
|
|
30
|
-
"scripts": {
|
|
31
|
-
"build": "yarn webiny run build",
|
|
32
|
-
"watch": "yarn webiny run watch"
|
|
27
|
+
"access": "public"
|
|
33
28
|
},
|
|
34
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "b8aec8a1be3f25c3b428b357fe1e352c7cbff9ae",
|
|
30
|
+
"webiny": {
|
|
31
|
+
"publishFrom": "dist"
|
|
32
|
+
}
|
|
35
33
|
}
|
package/types.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/// <reference types="web" />
|
|
2
|
-
import { Plugin } from "@webiny/plugins/types";
|
|
3
|
-
import { ApolloClient } from "apollo-client";
|
|
4
|
-
export declare type AppFileManagerStorageS3 = Plugin & {
|
|
5
|
-
type: "app-file-manager-storage";
|
|
6
|
-
name: "app-file-manager-storage";
|
|
7
|
-
upload(file: File, options: {
|
|
8
|
-
apolloClient: ApolloClient<Record<string, any>>;
|
|
9
|
-
}): Promise<any>;
|
|
10
|
-
};
|
package/types.js
DELETED
package/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins/types\";\nimport { ApolloClient } from \"apollo-client\";\n\nexport type AppFileManagerStorageS3 = Plugin & {\n type: \"app-file-manager-storage\";\n name: \"app-file-manager-storage\";\n upload(file: File, options: { apolloClient: ApolloClient<Record<string, any>> }): Promise<any>;\n};\n"],"mappings":""}
|