@webiny/app-file-manager-s3 6.0.0-beta.0 → 6.0.0-rc.1
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 +1 -1
- package/MultiPartUploadAPI.js +1 -5
- package/MultiPartUploadAPI.js.map +1 -1
- package/MultiPartUploadGraphQLAPI.d.ts +2 -2
- package/MultiPartUploadGraphQLAPI.js +71 -47
- package/MultiPartUploadGraphQLAPI.js.map +1 -1
- package/MultiPartUploadStrategy.d.ts +2 -2
- package/MultiPartUploadStrategy.js +38 -57
- package/MultiPartUploadStrategy.js.map +1 -1
- package/MultiPartUploader.d.ts +2 -2
- package/MultiPartUploader.js +143 -193
- package/MultiPartUploader.js.map +1 -1
- package/README.md +11 -1
- package/SimpleUploadStrategy.d.ts +2 -2
- package/SimpleUploadStrategy.js +53 -65
- package/SimpleUploadStrategy.js.map +1 -1
- package/graphql.js +22 -11
- package/graphql.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.js +14 -30
- package/index.js.map +1 -1
- package/package.json +8 -14
package/MultiPartUploadAPI.d.ts
CHANGED
package/MultiPartUploadAPI.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["MultiPartUploadAPI.ts"],"sourcesContent":["import { UploadedFile } from \"@webiny/app/types\";\n\nexport interface CreateUploadParams {\n data: {\n size: number;\n name: string;\n type: string;\n };\n numberOfParts: number;\n}\n\nexport interface CompleteUploadParams {\n fileKey: string;\n uploadId: string;\n}\n\nexport interface FilePart {\n partNumber: number;\n url: string;\n}\n\nexport interface MultiPartUpload {\n file: UploadedFile;\n uploadId: string;\n parts: FilePart[];\n}\n\nexport interface MultiPartUploadAPI {\n createUpload(params: CreateUploadParams): Promise<MultiPartUpload>;\n completeUpload(params: CompleteUploadParams): Promise<boolean>;\n}\n"],"mappings":"","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":[],"sources":["MultiPartUploadAPI.ts"],"sourcesContent":["import type { UploadedFile } from \"@webiny/app/types.js\";\n\nexport interface CreateUploadParams {\n data: {\n size: number;\n name: string;\n type: string;\n };\n numberOfParts: number;\n}\n\nexport interface CompleteUploadParams {\n fileKey: string;\n uploadId: string;\n}\n\nexport interface FilePart {\n partNumber: number;\n url: string;\n}\n\nexport interface MultiPartUpload {\n file: UploadedFile;\n uploadId: string;\n parts: FilePart[];\n}\n\nexport interface MultiPartUploadAPI {\n createUpload(params: CreateUploadParams): Promise<MultiPartUpload>;\n completeUpload(params: CompleteUploadParams): Promise<boolean>;\n}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { UploadOptions } from "@webiny/app/types";
|
|
2
|
-
import { CompleteUploadParams, CreateUploadParams, MultiPartUpload, MultiPartUploadAPI } from "./MultiPartUploadAPI";
|
|
1
|
+
import type { UploadOptions } from "@webiny/app/types.js";
|
|
2
|
+
import type { CompleteUploadParams, CreateUploadParams, MultiPartUpload, MultiPartUploadAPI } from "./MultiPartUploadAPI.js";
|
|
3
3
|
export declare class MultiPartUploadGraphQLAPI implements MultiPartUploadAPI {
|
|
4
4
|
private client;
|
|
5
5
|
constructor(client: UploadOptions["apolloClient"]);
|
|
@@ -1,53 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.MultiPartUploadGraphQLAPI = void 0;
|
|
8
|
-
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral"));
|
|
9
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
10
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
11
|
-
var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
|
|
12
|
-
var _templateObject, _templateObject2;
|
|
13
|
-
var MultiPartUploadGraphQLAPI = exports.MultiPartUploadGraphQLAPI = /*#__PURE__*/function () {
|
|
14
|
-
function MultiPartUploadGraphQLAPI(client) {
|
|
15
|
-
(0, _classCallCheck2.default)(this, MultiPartUploadGraphQLAPI);
|
|
1
|
+
import gql from "graphql-tag";
|
|
2
|
+
export class MultiPartUploadGraphQLAPI {
|
|
3
|
+
constructor(client) {
|
|
16
4
|
this.client = client;
|
|
17
5
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
6
|
+
async createUpload(params) {
|
|
7
|
+
const {
|
|
8
|
+
data,
|
|
9
|
+
errors
|
|
10
|
+
} = await this.client.mutate({
|
|
11
|
+
mutation: CREATE_UPLOAD,
|
|
12
|
+
variables: params
|
|
13
|
+
});
|
|
14
|
+
if (!data) {
|
|
15
|
+
console.error(errors);
|
|
16
|
+
throw new Error(`Failed to initialize a multi-part file upload!`);
|
|
17
|
+
}
|
|
18
|
+
return data.fileManager.createMultiPartUpload.data;
|
|
19
|
+
}
|
|
20
|
+
async completeUpload(params) {
|
|
21
|
+
const {
|
|
22
|
+
data,
|
|
23
|
+
errors
|
|
24
|
+
} = await this.client.mutate({
|
|
25
|
+
mutation: COMPLETE_UPLOAD,
|
|
26
|
+
variables: params
|
|
27
|
+
});
|
|
28
|
+
if (!data) {
|
|
29
|
+
console.error(errors);
|
|
30
|
+
throw new Error(`Failed to complete a multi-part file upload!`);
|
|
31
|
+
}
|
|
32
|
+
return data.fileManager.completeMultiPartUpload.data;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const CREATE_UPLOAD = gql`
|
|
36
|
+
mutation CreateMultiPartUpload($data: PreSignedPostPayloadInput!, $numberOfParts: Number!) {
|
|
37
|
+
fileManager {
|
|
38
|
+
createMultiPartUpload(data: $data, numberOfParts: $numberOfParts) {
|
|
39
|
+
data {
|
|
40
|
+
file {
|
|
41
|
+
id
|
|
42
|
+
key
|
|
43
|
+
name
|
|
44
|
+
size
|
|
45
|
+
type
|
|
46
|
+
}
|
|
47
|
+
uploadId
|
|
48
|
+
parts {
|
|
49
|
+
partNumber
|
|
50
|
+
url
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
error {
|
|
54
|
+
code
|
|
55
|
+
message
|
|
56
|
+
data
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
32
60
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return data.fileManager.completeMultiPartUpload.data;
|
|
61
|
+
`;
|
|
62
|
+
const COMPLETE_UPLOAD = gql`
|
|
63
|
+
mutation CompleteMultiPartUpload($fileKey: String!, $uploadId: String!) {
|
|
64
|
+
fileManager {
|
|
65
|
+
completeMultiPartUpload(fileKey: $fileKey, uploadId: $uploadId) {
|
|
66
|
+
data
|
|
67
|
+
error {
|
|
68
|
+
code
|
|
69
|
+
message
|
|
70
|
+
data
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
47
74
|
}
|
|
48
|
-
|
|
49
|
-
}();
|
|
50
|
-
var CREATE_UPLOAD = (0, _graphqlTag.default)(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["\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"])));
|
|
51
|
-
var COMPLETE_UPLOAD = (0, _graphqlTag.default)(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2.default)(["\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"])));
|
|
75
|
+
`;
|
|
52
76
|
|
|
53
77
|
//# sourceMappingURL=MultiPartUploadGraphQLAPI.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["gql","MultiPartUploadGraphQLAPI","constructor","client","createUpload","params","data","errors","mutate","mutation","CREATE_UPLOAD","variables","console","error","Error","fileManager","createMultiPartUpload","completeUpload","COMPLETE_UPLOAD","completeMultiPartUpload"],"sources":["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"],"mappings":"AACA,OAAOA,GAAG,MAAM,aAAa;AAQ7B,OAAO,MAAMC,yBAAyB,CAA+B;EAGjEC,WAAWA,CAACC,MAAqC,EAAE;IAC/C,IAAI,CAACA,MAAM,GAAGA,MAAM;EACxB;EAEA,MAAMC,YAAYA,CAACC,MAA0B,EAA4B;IACrE,MAAM;MAAEC,IAAI;MAAEC;IAAO,CAAC,GAAG,MAAM,IAAI,CAACJ,MAAM,CAACK,MAAM,CAAuB;MACpEC,QAAQ,EAAEC,aAAa;MACvBC,SAAS,EAAEN;IACf,CAAC,CAAC;IAEF,IAAI,CAACC,IAAI,EAAE;MACPM,OAAO,CAACC,KAAK,CAACN,MAAM,CAAC;MACrB,MAAM,IAAIO,KAAK,CAAC,gDAAgD,CAAC;IACrE;IAEA,OAAOR,IAAI,CAACS,WAAW,CAACC,qBAAqB,CAACV,IAAI;EACtD;EAEA,MAAMW,cAAcA,CAACZ,MAA4B,EAAoB;IACjE,MAAM;MAAEC,IAAI;MAAEC;IAAO,CAAC,GAAG,MAAM,IAAI,CAACJ,MAAM,CAACK,MAAM,CAAyB;MACtEC,QAAQ,EAAES,eAAe;MACzBP,SAAS,EAAEN;IACf,CAAC,CAAC;IAEF,IAAI,CAACC,IAAI,EAAE;MACPM,OAAO,CAACC,KAAK,CAACN,MAAM,CAAC;MACrB,MAAM,IAAIO,KAAK,CAAC,8CAA8C,CAAC;IACnE;IAEA,OAAOR,IAAI,CAACS,WAAW,CAACI,uBAAuB,CAACb,IAAI;EACxD;AACJ;AAEA,MAAMI,aAAa,GAAGV,GAAG;AACzB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AAeD,MAAMkB,eAAe,GAAGlB,GAAG;AAC3B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { UploadedFile, UploadOptions } from "@webiny/app/types";
|
|
2
|
-
import { FileUploadStrategy } from "./index";
|
|
1
|
+
import type { UploadedFile, UploadOptions } from "@webiny/app/types.js";
|
|
2
|
+
import type { FileUploadStrategy } from "./index.js";
|
|
3
3
|
export declare class MultiPartUploadStrategy implements FileUploadStrategy {
|
|
4
4
|
upload(file: File, options: UploadOptions): Promise<UploadedFile>;
|
|
5
5
|
private detectChunkSize;
|
|
@@ -1,66 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var MultiPartUploadStrategy = exports.MultiPartUploadStrategy = /*#__PURE__*/function () {
|
|
13
|
-
function MultiPartUploadStrategy() {
|
|
14
|
-
(0, _classCallCheck2.default)(this, MultiPartUploadStrategy);
|
|
15
|
-
}
|
|
16
|
-
return (0, _createClass2.default)(MultiPartUploadStrategy, [{
|
|
17
|
-
key: "upload",
|
|
18
|
-
value: async function upload(file, options) {
|
|
19
|
-
var api = new _MultiPartUploadGraphQLAPI.MultiPartUploadGraphQLAPI(options.apolloClient);
|
|
20
|
-
var uploader = new _MultiPartUploader.MultiPartUploader(api, {
|
|
21
|
-
chunkSize: this.detectChunkSize(),
|
|
22
|
-
parallelUploads: this.detectParallelChunks()
|
|
23
|
-
});
|
|
24
|
-
if (options.onProgress) {
|
|
25
|
-
uploader.onProgress(options.onProgress);
|
|
26
|
-
}
|
|
27
|
-
return uploader.uploadFile(file);
|
|
1
|
+
import { MultiPartUploader } from "./MultiPartUploader.js";
|
|
2
|
+
import { MultiPartUploadGraphQLAPI } from "./MultiPartUploadGraphQLAPI.js";
|
|
3
|
+
export 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) {
|
|
11
|
+
uploader.onProgress(options.onProgress);
|
|
28
12
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
13
|
+
return uploader.uploadFile(file);
|
|
14
|
+
}
|
|
15
|
+
detectChunkSize() {
|
|
16
|
+
const envSize = parseInt(process.env.WEBINY_FILE_UPLOAD_CHUNK_SIZE || "0");
|
|
33
17
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
18
|
+
// For dev purposes, we take this global var into consideration.
|
|
19
|
+
if (process.env.NODE_ENV === "development") {
|
|
20
|
+
// @ts-expect-error
|
|
21
|
+
const windowSize = window["fmUploadChunkSize"];
|
|
22
|
+
if (windowSize) {
|
|
23
|
+
return windowSize;
|
|
41
24
|
}
|
|
42
|
-
|
|
43
|
-
// As a last resort, we check the baked in value, or fall back to 50MB chunk size.
|
|
44
|
-
return envSize || 50;
|
|
45
25
|
}
|
|
46
|
-
}, {
|
|
47
|
-
key: "detectParallelChunks",
|
|
48
|
-
value: function detectParallelChunks() {
|
|
49
|
-
var envChunks = parseInt(process.env.WEBINY_FILE_UPLOAD_PARALLEL_CHUNKS || "0");
|
|
50
26
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return windowChunks;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
27
|
+
// As a last resort, we check the baked in value, or fall back to 50MB chunk size.
|
|
28
|
+
return envSize || 50;
|
|
29
|
+
}
|
|
30
|
+
detectParallelChunks() {
|
|
31
|
+
const envChunks = parseInt(process.env.WEBINY_FILE_UPLOAD_PARALLEL_CHUNKS || "0");
|
|
59
32
|
|
|
60
|
-
|
|
61
|
-
|
|
33
|
+
// For dev purposes, we take this global var into consideration.
|
|
34
|
+
if (process.env.NODE_ENV === "development") {
|
|
35
|
+
// @ts-expect-error
|
|
36
|
+
const windowChunks = window["fmUploadParallelChunks"];
|
|
37
|
+
if (windowChunks) {
|
|
38
|
+
return windowChunks;
|
|
39
|
+
}
|
|
62
40
|
}
|
|
63
|
-
|
|
64
|
-
|
|
41
|
+
|
|
42
|
+
// As a last resort, we check the baked in value, or fall back to 5 parallel chunks.
|
|
43
|
+
return envChunks || 5;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
65
46
|
|
|
66
47
|
//# sourceMappingURL=MultiPartUploadStrategy.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["MultiPartUploader","MultiPartUploadGraphQLAPI","MultiPartUploadStrategy","upload","file","options","api","apolloClient","uploader","chunkSize","detectChunkSize","parallelUploads","detectParallelChunks","onProgress","uploadFile","envSize","parseInt","process","env","WEBINY_FILE_UPLOAD_CHUNK_SIZE","NODE_ENV","windowSize","window","envChunks","WEBINY_FILE_UPLOAD_PARALLEL_CHUNKS","windowChunks"],"sources":["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"],"mappings":"AAEA,SAASA,iBAAiB;AAC1B,SAASC,yBAAyB;AAElC,OAAO,MAAMC,uBAAuB,CAA+B;EAC/D,MAAMC,MAAMA,CAACC,IAAU,EAAEC,OAAsB,EAAyB;IACpE,MAAMC,GAAG,GAAG,IAAIL,yBAAyB,CAACI,OAAO,CAACE,YAAY,CAAC;IAE/D,MAAMC,QAAQ,GAAG,IAAIR,iBAAiB,CAACM,GAAG,EAAE;MACxCG,SAAS,EAAE,IAAI,CAACC,eAAe,CAAC,CAAC;MACjCC,eAAe,EAAE,IAAI,CAACC,oBAAoB,CAAC;IAC/C,CAAC,CAAC;IAEF,IAAIP,OAAO,CAACQ,UAAU,EAAE;MACpBL,QAAQ,CAACK,UAAU,CAACR,OAAO,CAACQ,UAAU,CAAC;IAC3C;IACA,OAAOL,QAAQ,CAACM,UAAU,CAACV,IAAI,CAAC;EACpC;EAEQM,eAAeA,CAAA,EAAW;IAC9B,MAAMK,OAAO,GAAGC,QAAQ,CAACC,OAAO,CAACC,GAAG,CAACC,6BAA6B,IAAI,GAAG,CAAC;;IAE1E;IACA,IAAIF,OAAO,CAACC,GAAG,CAACE,QAAQ,KAAK,aAAa,EAAE;MACxC;MACA,MAAMC,UAAU,GAAGC,MAAM,CAAC,mBAAmB,CAAC;MAC9C,IAAID,UAAU,EAAE;QACZ,OAAOA,UAAU;MACrB;IACJ;;IAEA;IACA,OAAON,OAAO,IAAI,EAAE;EACxB;EAEQH,oBAAoBA,CAAA,EAAW;IACnC,MAAMW,SAAS,GAAGP,QAAQ,CAACC,OAAO,CAACC,GAAG,CAACM,kCAAkC,IAAI,GAAG,CAAC;;IAEjF;IACA,IAAIP,OAAO,CAACC,GAAG,CAACE,QAAQ,KAAK,aAAa,EAAE;MACxC;MACA,MAAMK,YAAY,GAAGH,MAAM,CAAC,wBAAwB,CAAC;MACrD,IAAIG,YAAY,EAAE;QACd,OAAOA,YAAY;MACvB;IACJ;;IAEA;IACA,OAAOF,SAAS,IAAI,CAAC;EACzB;AACJ","ignoreList":[]}
|
package/MultiPartUploader.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MultiPartUploadAPI } from "./MultiPartUploadAPI";
|
|
1
|
+
import type { MultiPartUploadAPI } from "./MultiPartUploadAPI.js";
|
|
2
2
|
interface MultiPartUploaderOptions {
|
|
3
3
|
/**
|
|
4
4
|
* Chunk size in MB. Must be >=5MB (enforced by AWS).
|
|
@@ -30,7 +30,7 @@ export declare class MultiPartUploader {
|
|
|
30
30
|
private onErrorFn;
|
|
31
31
|
private onProgressFn;
|
|
32
32
|
constructor(api: MultiPartUploadAPI, options?: MultiPartUploaderOptions);
|
|
33
|
-
uploadFile(file: File): Promise<import("@webiny/app/types").UploadedFile>;
|
|
33
|
+
uploadFile(file: File): Promise<import("@webiny/app/types.js").UploadedFile>;
|
|
34
34
|
onProgress(onProgress: OnProgressCallback): this;
|
|
35
35
|
onError(onError: OnErrorCallback): this;
|
|
36
36
|
private complete;
|
package/MultiPartUploader.js
CHANGED
|
@@ -1,215 +1,165 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
})
|
|
7
|
-
exports.MultiPartUploader = void 0;
|
|
8
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
9
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
10
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
11
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
12
|
-
var _pRetry = _interopRequireDefault(require("p-retry"));
|
|
13
|
-
var MultiPartUploader = exports.MultiPartUploader = /*#__PURE__*/function () {
|
|
14
|
-
function MultiPartUploader(api) {
|
|
15
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
16
|
-
(0, _classCallCheck2.default)(this, MultiPartUploader);
|
|
17
|
-
(0, _defineProperty2.default)(this, "activeConnections", new Map());
|
|
18
|
-
(0, _defineProperty2.default)(this, "progressTracker", new Map());
|
|
19
|
-
(0, _defineProperty2.default)(this, "onErrorFn", function (error) {
|
|
20
|
-
return console.error(error);
|
|
21
|
-
});
|
|
1
|
+
import pRetry from "p-retry";
|
|
2
|
+
export class MultiPartUploader {
|
|
3
|
+
activeConnections = new Map();
|
|
4
|
+
progressTracker = new Map();
|
|
5
|
+
onErrorFn = error => console.error(error);
|
|
6
|
+
constructor(api, options = {}) {
|
|
22
7
|
this.api = api;
|
|
23
|
-
|
|
24
|
-
|
|
8
|
+
const chunkSize = options.chunkSize || 50;
|
|
9
|
+
const parallelUploads = options.parallelUploads || 5;
|
|
25
10
|
this.chunkSize = Math.max(1024 * 1024 * chunkSize, 1024 * 1024 * 5);
|
|
26
11
|
this.parallelUploads = Math.min(parallelUploads, 15);
|
|
27
12
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
numberOfParts: numberOfParts
|
|
45
|
-
});
|
|
13
|
+
async uploadFile(file) {
|
|
14
|
+
this.file = file;
|
|
15
|
+
const numberOfParts = Math.ceil(file.size / this.chunkSize);
|
|
16
|
+
try {
|
|
17
|
+
/**
|
|
18
|
+
* Initialize the file upload on AWS S3.
|
|
19
|
+
*/
|
|
20
|
+
this.upload = await this.api.createUpload({
|
|
21
|
+
data: {
|
|
22
|
+
name: file.name,
|
|
23
|
+
size: file.size,
|
|
24
|
+
type: file.type
|
|
25
|
+
},
|
|
26
|
+
numberOfParts
|
|
27
|
+
});
|
|
46
28
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
await this.complete(error);
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
29
|
+
/**
|
|
30
|
+
* Run the defined number of parallel uploads. Each thread will continue to process parts
|
|
31
|
+
* for as long as there are parts to upload. The promise will resolve once there's no more parts to upload.
|
|
32
|
+
*/
|
|
33
|
+
const threads = Math.min(numberOfParts, this.parallelUploads);
|
|
34
|
+
await Promise.all(Array.from({
|
|
35
|
+
length: threads
|
|
36
|
+
}).map(() => this.uploadNextPart()));
|
|
37
|
+
await this.complete();
|
|
38
|
+
return this.upload.file;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
await this.complete(error);
|
|
41
|
+
throw error;
|
|
63
42
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
43
|
+
}
|
|
44
|
+
onProgress(onProgress) {
|
|
45
|
+
this.onProgressFn = onProgress;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
onError(onError) {
|
|
49
|
+
this.onErrorFn = onError;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
async complete(error) {
|
|
53
|
+
if (error) {
|
|
54
|
+
this.onErrorFn(error);
|
|
55
|
+
return;
|
|
69
56
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.onErrorFn
|
|
74
|
-
return this;
|
|
57
|
+
try {
|
|
58
|
+
await this.sendCompleteRequest();
|
|
59
|
+
} catch (error) {
|
|
60
|
+
this.onErrorFn(error);
|
|
75
61
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
62
|
+
}
|
|
63
|
+
async sendCompleteRequest() {
|
|
64
|
+
this.assertIsDefined(this.upload, `Upload must be created before calling "sendCompleteRequest"!`);
|
|
65
|
+
return this.api.completeUpload({
|
|
66
|
+
fileKey: this.upload.file.key,
|
|
67
|
+
uploadId: this.upload.uploadId
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
progressListener(part, event) {
|
|
71
|
+
if (!this.file) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.progressTracker.set(part.partNumber, event.loaded);
|
|
75
|
+
const uploaded = Array.from(this.progressTracker.values()).reduce((sum = 0, value) => sum + value);
|
|
76
|
+
const uploadedSize = Math.min(uploaded, this.file.size);
|
|
77
|
+
if (this.onProgressFn) {
|
|
83
78
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
this.onProgressFn({
|
|
80
|
+
sent: uploadedSize,
|
|
81
|
+
total: this.file.size,
|
|
82
|
+
percentage: Math.round(uploadedSize / this.file.size * 100)
|
|
83
|
+
});
|
|
84
|
+
} catch (err) {
|
|
85
|
+
console.error(`Error executing the "onProgress" callback`, err);
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return this.api.completeUpload({
|
|
94
|
-
fileKey: this.upload.file.key,
|
|
95
|
-
uploadId: this.upload.uploadId
|
|
96
|
-
});
|
|
88
|
+
}
|
|
89
|
+
async uploadNextPart() {
|
|
90
|
+
if (!this.upload) {
|
|
91
|
+
return;
|
|
97
92
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (!this.file) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
this.progressTracker.set(part.partNumber, event.loaded);
|
|
105
|
-
var uploaded = Array.from(this.progressTracker.values()).reduce(function () {
|
|
106
|
-
var sum = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
107
|
-
var value = arguments.length > 1 ? arguments[1] : undefined;
|
|
108
|
-
return sum + value;
|
|
109
|
-
});
|
|
110
|
-
var uploadedSize = Math.min(uploaded, this.file.size);
|
|
111
|
-
if (this.onProgressFn) {
|
|
112
|
-
try {
|
|
113
|
-
this.onProgressFn({
|
|
114
|
-
sent: uploadedSize,
|
|
115
|
-
total: this.file.size,
|
|
116
|
-
percentage: Math.round(uploadedSize / this.file.size * 100)
|
|
117
|
-
});
|
|
118
|
-
} catch (err) {
|
|
119
|
-
console.error("Error executing the \"onProgress\" callback", err);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
93
|
+
const part = this.upload.parts.shift();
|
|
94
|
+
if (!part) {
|
|
95
|
+
return;
|
|
122
96
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
97
|
+
return executeWithRetry(() => this.uploadPart(part)).then(() => this.uploadNextPart());
|
|
98
|
+
}
|
|
99
|
+
uploadPart(part) {
|
|
100
|
+
this.assertIsDefined(this.upload, `Upload must be created before calling "sendCompleteRequest"!`);
|
|
101
|
+
this.assertIsDefined(this.file, `File must be set before calling "uploadPart"!`);
|
|
102
|
+
const sentSize = (part.partNumber - 1) * this.chunkSize;
|
|
103
|
+
const nextChunkSize = Math.min(sentSize + this.chunkSize, this.file.size);
|
|
104
|
+
const chunk = this.file.slice(sentSize, nextChunkSize, this.upload.file.type);
|
|
105
|
+
console.log(`Chunk for part #${part.partNumber}`, chunk.size);
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
const throwXHRError = error => {
|
|
108
|
+
this.activeConnections.delete(part.partNumber);
|
|
109
|
+
reject(error);
|
|
110
|
+
};
|
|
111
|
+
if (!window.navigator.onLine) {
|
|
112
|
+
return reject(new Error("Browser is offline!"));
|
|
133
113
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
var throwXHRError = function throwXHRError(error) {
|
|
152
|
-
_this3.activeConnections.delete(part.partNumber);
|
|
153
|
-
reject(error);
|
|
154
|
-
};
|
|
155
|
-
if (!window.navigator.onLine) {
|
|
156
|
-
return reject(new Error("Browser is offline!"));
|
|
157
|
-
}
|
|
158
|
-
var xhr = new XMLHttpRequest();
|
|
159
|
-
_this3.activeConnections.set(part.partNumber, xhr);
|
|
160
|
-
var abortXHR = function abortXHR() {
|
|
161
|
-
return xhr.abort();
|
|
162
|
-
};
|
|
163
|
-
xhr.upload.addEventListener("progress", function (event) {
|
|
164
|
-
return _this3.progressListener(part, event);
|
|
165
|
-
});
|
|
166
|
-
window.addEventListener("offline", abortXHR);
|
|
167
|
-
var removeListeners = function removeListeners() {
|
|
168
|
-
window.removeEventListener("offline", abortXHR);
|
|
169
|
-
};
|
|
170
|
-
xhr.open("PUT", part.url);
|
|
171
|
-
xhr.onreadystatechange = function () {
|
|
172
|
-
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
173
|
-
try {
|
|
174
|
-
_this3.activeConnections.delete(part.partNumber);
|
|
175
|
-
window.removeEventListener("offline", abortXHR);
|
|
176
|
-
resolve(xhr.status);
|
|
177
|
-
} catch (err) {
|
|
178
|
-
console.error("Error in \"onreadystatechange\"", err);
|
|
179
|
-
}
|
|
114
|
+
const xhr = new XMLHttpRequest();
|
|
115
|
+
this.activeConnections.set(part.partNumber, xhr);
|
|
116
|
+
const abortXHR = () => xhr.abort();
|
|
117
|
+
xhr.upload.addEventListener("progress", event => this.progressListener(part, event));
|
|
118
|
+
window.addEventListener("offline", abortXHR);
|
|
119
|
+
const removeListeners = () => {
|
|
120
|
+
window.removeEventListener("offline", abortXHR);
|
|
121
|
+
};
|
|
122
|
+
xhr.open("PUT", part.url);
|
|
123
|
+
xhr.onreadystatechange = () => {
|
|
124
|
+
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
125
|
+
try {
|
|
126
|
+
this.activeConnections.delete(part.partNumber);
|
|
127
|
+
window.removeEventListener("offline", abortXHR);
|
|
128
|
+
resolve(xhr.status);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
console.error(`Error in "onreadystatechange"`, err);
|
|
180
131
|
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
throw new Error(message);
|
|
202
|
-
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
xhr.onerror = () => {
|
|
135
|
+
removeListeners();
|
|
136
|
+
throwXHRError(new Error(`Failed to upload file part #${part.partNumber}`));
|
|
137
|
+
};
|
|
138
|
+
xhr.ontimeout = () => {
|
|
139
|
+
removeListeners();
|
|
140
|
+
throwXHRError(new Error(`Request timed out for file part #${part.partNumber}!`));
|
|
141
|
+
};
|
|
142
|
+
xhr.onabort = () => {
|
|
143
|
+
removeListeners();
|
|
144
|
+
throwXHRError(new Error(`Upload was cancelled for part #${part.partNumber}!`));
|
|
145
|
+
};
|
|
146
|
+
xhr.send(chunk);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
assertIsDefined(upload, message) {
|
|
150
|
+
if (!upload) {
|
|
151
|
+
throw new Error(message);
|
|
203
152
|
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return (
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const executeWithRetry = (execute, options) => {
|
|
156
|
+
return pRetry(execute, {
|
|
208
157
|
maxRetryTime: 300000,
|
|
209
158
|
retries: 5,
|
|
210
159
|
minTimeout: 1500,
|
|
211
|
-
maxTimeout: 30000
|
|
212
|
-
|
|
160
|
+
maxTimeout: 30000,
|
|
161
|
+
...options
|
|
162
|
+
});
|
|
213
163
|
};
|
|
214
164
|
|
|
215
165
|
//# sourceMappingURL=MultiPartUploader.js.map
|
package/MultiPartUploader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_pRetry","_interopRequireDefault","require","MultiPartUploader","exports","api","options","arguments","length","undefined","_classCallCheck2","default","_defineProperty2","Map","error","console","chunkSize","parallelUploads","Math","max","min","_createClass2","key","value","uploadFile","file","_this","numberOfParts","ceil","size","upload","createUpload","data","name","type","threads","Promise","all","Array","from","map","uploadNextPart","complete","onProgress","onProgressFn","onError","onErrorFn","sendCompleteRequest","assertIsDefined","completeUpload","fileKey","uploadId","progressListener","part","event","progressTracker","set","partNumber","loaded","uploaded","values","reduce","sum","uploadedSize","sent","total","percentage","round","err","_this2","parts","shift","executeWithRetry","uploadPart","then","_this3","sentSize","nextChunkSize","chunk","slice","log","concat","resolve","reject","throwXHRError","activeConnections","delete","window","navigator","onLine","Error","xhr","XMLHttpRequest","abortXHR","abort","addEventListener","removeListeners","removeEventListener","open","url","onreadystatechange","readyState","status","onerror","ontimeout","onabort","send","message","execute","pRetry","_objectSpread2","maxRetryTime","retries","minTimeout","maxTimeout"],"sources":["MultiPartUploader.ts"],"sourcesContent":["import pRetry from \"p-retry\";\nimport { MultiPartUpload, MultiPartUploadAPI, FilePart } from \"./MultiPartUploadAPI\";\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"],"mappings":";;;;;;;;;;;AAAA,IAAAA,OAAA,GAAAC,sBAAA,CAAAC,OAAA;AAA6B,IAsBhBC,iBAAiB,GAAAC,OAAA,CAAAD,iBAAA;EAW1B,SAAAA,kBAAYE,GAAuB,EAA0C;IAAA,IAAxCC,OAAiC,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;IAAA,IAAAG,gBAAA,CAAAC,OAAA,QAAAR,iBAAA;IAAA,IAAAS,gBAAA,CAAAD,OAAA,6BAPT,IAAIE,GAAG,CAAC,CAAC;IAAA,IAAAD,gBAAA,CAAAD,OAAA,2BACnB,IAAIE,GAAG,CAAC,CAAC;IAAA,IAAAD,gBAAA,CAAAD,OAAA,qBAG5B,UAAAG,KAAK;MAAA,OAAIC,OAAO,CAACD,KAAK,CAACA,KAAK,CAAC;IAAA;IAI9D,IAAI,CAACT,GAAG,GAAGA,GAAG;IACd,IAAMW,SAAS,GAAGV,OAAO,CAACU,SAAS,IAAI,EAAE;IACzC,IAAMC,eAAe,GAAGX,OAAO,CAACW,eAAe,IAAI,CAAC;IAEpD,IAAI,CAACD,SAAS,GAAGE,IAAI,CAACC,GAAG,CAAC,IAAI,GAAG,IAAI,GAAGH,SAAS,EAAE,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC;IACnE,IAAI,CAACC,eAAe,GAAGC,IAAI,CAACE,GAAG,CAACH,eAAe,EAAE,EAAE,CAAC;EACxD;EAAC,WAAAI,aAAA,CAAAV,OAAA,EAAAR,iBAAA;IAAAmB,GAAA;IAAAC,KAAA,EAED,eAAAC,WAAiBC,IAAU,EAAE;MAAA,IAAAC,KAAA;MAC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ACiB,OAAO,CAAC;MAC5B;IACJ;EAAC;AAAA;AAGL,IAAMnC,gBAAgB,GAAG,SAAnBA,gBAAgBA,CAAIoC,OAAmB,EAAEtG,OAAsC,EAAK;EACtF,OAAO,IAAAuG,eAAM,EAACD,OAAO,MAAAE,cAAA,CAAAnG,OAAA;IACjBoG,YAAY,EAAE,MAAM;IACpBC,OAAO,EAAE,CAAC;IACVC,UAAU,EAAE,IAAI;IAChBC,UAAU,EAAE;EAAK,GACd5G,OAAO,CACb,CAAC;AACN,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["pRetry","MultiPartUploader","activeConnections","Map","progressTracker","onErrorFn","error","console","constructor","api","options","chunkSize","parallelUploads","Math","max","min","uploadFile","file","numberOfParts","ceil","size","upload","createUpload","data","name","type","threads","Promise","all","Array","from","length","map","uploadNextPart","complete","onProgress","onProgressFn","onError","sendCompleteRequest","assertIsDefined","completeUpload","fileKey","key","uploadId","progressListener","part","event","set","partNumber","loaded","uploaded","values","reduce","sum","value","uploadedSize","sent","total","percentage","round","err","parts","shift","executeWithRetry","uploadPart","then","sentSize","nextChunkSize","chunk","slice","log","resolve","reject","throwXHRError","delete","window","navigator","onLine","Error","xhr","XMLHttpRequest","abortXHR","abort","addEventListener","removeListeners","removeEventListener","open","url","onreadystatechange","readyState","status","onerror","ontimeout","onabort","send","message","execute","maxRetryTime","retries","minTimeout","maxTimeout"],"sources":["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"],"mappings":"AAAA,OAAOA,MAAM,MAAM,SAAS;AAsB5B,OAAO,MAAMC,iBAAiB,CAAC;EAIVC,iBAAiB,GAAgC,IAAIC,GAAG,CAAC,CAAC;EAC1DC,eAAe,GAAwB,IAAID,GAAG,CAAC,CAAC;EAGzDE,SAAS,GAAoBC,KAAK,IAAIC,OAAO,CAACD,KAAK,CAACA,KAAK,CAAC;EAGlEE,WAAWA,CAACC,GAAuB,EAAEC,OAAiC,GAAG,CAAC,CAAC,EAAE;IACzE,IAAI,CAACD,GAAG,GAAGA,GAAG;IACd,MAAME,SAAS,GAAGD,OAAO,CAACC,SAAS,IAAI,EAAE;IACzC,MAAMC,eAAe,GAAGF,OAAO,CAACE,eAAe,IAAI,CAAC;IAEpD,IAAI,CAACD,SAAS,GAAGE,IAAI,CAACC,GAAG,CAAC,IAAI,GAAG,IAAI,GAAGH,SAAS,EAAE,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC;IACnE,IAAI,CAACC,eAAe,GAAGC,IAAI,CAACE,GAAG,CAACH,eAAe,EAAE,EAAE,CAAC;EACxD;EAEA,MAAMI,UAAUA,CAACC,IAAU,EAAE;IACzB,IAAI,CAACA,IAAI,GAAGA,IAAI;IAChB,MAAMC,aAAa,GAA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eAAe,CAAC,CAAC;QACjBX,aAAa,CAAC,IAAIK,KAAK,CAAC,+BAA+BjC,IAAI,CAACG,UAAU,EAAE,CAAC,CAAC;MAC9E,CAAC;MACD+B,GAAG,CAACa,SAAS,GAAG,MAAM;QAClBR,eAAe,CAAC,CAAC;QACjBX,aAAa,CAAC,IAAIK,KAAK,CAAC,oCAAoCjC,IAAI,CAACG,UAAU,GAAG,CAAC,CAAC;MACpF,CAAC;MACD+B,GAAG,CAACc,OAAO,GAAG,MAAM;QAChBT,eAAe,CAAC,CAAC;QACjBX,aAAa,CAAC,IAAIK,KAAK,CAAC,kCAAkCjC,IAAI,CAACG,UAAU,GAAG,CAAC,CAAC;MAClF,CAAC;MACD+B,GAAG,CAACe,IAAI,CAAC1B,KAAK,CAAC;IACnB,CAAC,CAAC;EACN;EAEQ7B,eAAeA,CAAIlB,MAAS,EAAE0E,OAAe,EAAoC;IACrF,IAAI,CAAC1E,MAAM,EAAE;MACT,MAAM,IAAIyD,KAAK,CAACiB,OAAO,CAAC;IAC5B;EACJ;AACJ;AAEA,MAAMhC,gBAAgB,GAAGA,CAACiC,OAAmB,EAAEtF,OAAsC,KAAK;EACtF,OAAOV,MAAM,CAACgG,OAAO,EAAE;IACnBC,YAAY,EAAE,MAAM;IACpBC,OAAO,EAAE,CAAC;IACVC,UAAU,EAAE,IAAI;IAChBC,UAAU,EAAE,KAAK;IACjB,GAAG1F;EACP,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
|
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._
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { UploadedFile, UploadOptions } from "@webiny/app/types";
|
|
2
|
-
import { FileUploadStrategy } from "./index";
|
|
1
|
+
import type { UploadedFile, UploadOptions } from "@webiny/app/types.js";
|
|
2
|
+
import type { FileUploadStrategy } from "./index.js";
|
|
3
3
|
declare global {
|
|
4
4
|
interface File {
|
|
5
5
|
key?: string;
|
package/SimpleUploadStrategy.js
CHANGED
|
@@ -1,71 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var apolloClient = _ref.apolloClient,
|
|
19
|
-
onProgress = _ref.onProgress;
|
|
20
|
-
// 1. GET PreSignedPostPayload
|
|
21
|
-
var response = await apolloClient.query({
|
|
22
|
-
query: _graphql.GET_PRE_SIGNED_POST_PAYLOAD,
|
|
23
|
-
fetchPolicy: "no-cache",
|
|
24
|
-
variables: {
|
|
25
|
-
data: {
|
|
26
|
-
size: file.size,
|
|
27
|
-
name: file.name,
|
|
28
|
-
type: file.type,
|
|
29
|
-
key: file.key,
|
|
30
|
-
keyPrefix: file.keyPrefix
|
|
31
|
-
}
|
|
1
|
+
import { GET_PRE_SIGNED_POST_PAYLOAD } from "./graphql.js";
|
|
2
|
+
export class SimpleUploadStrategy {
|
|
3
|
+
async upload(file, {
|
|
4
|
+
apolloClient,
|
|
5
|
+
onProgress
|
|
6
|
+
}) {
|
|
7
|
+
// 1. GET PreSignedPostPayload
|
|
8
|
+
const response = await apolloClient.query({
|
|
9
|
+
query: GET_PRE_SIGNED_POST_PAYLOAD,
|
|
10
|
+
fetchPolicy: "no-cache",
|
|
11
|
+
variables: {
|
|
12
|
+
data: {
|
|
13
|
+
size: file.size,
|
|
14
|
+
name: file.name,
|
|
15
|
+
type: file.type,
|
|
16
|
+
key: file.key,
|
|
17
|
+
keyPrefix: file.keyPrefix
|
|
32
18
|
}
|
|
33
|
-
});
|
|
34
|
-
var getPreSignedPostPayload = response.data.fileManager.getPreSignedPostPayload;
|
|
35
|
-
if (getPreSignedPostPayload.error) {
|
|
36
|
-
console.error(getPreSignedPostPayload);
|
|
37
|
-
throw Error(getPreSignedPostPayload.error);
|
|
38
19
|
}
|
|
20
|
+
});
|
|
21
|
+
const {
|
|
22
|
+
getPreSignedPostPayload
|
|
23
|
+
} = response.data.fileManager;
|
|
24
|
+
if (getPreSignedPostPayload.error) {
|
|
25
|
+
console.error(getPreSignedPostPayload);
|
|
26
|
+
throw Error(getPreSignedPostPayload.error);
|
|
27
|
+
}
|
|
39
28
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
formData.append("file", file);
|
|
47
|
-
var xhr = new window.XMLHttpRequest();
|
|
48
|
-
xhr.upload.addEventListener("progress", function (event) {
|
|
49
|
-
if (onProgress) {
|
|
50
|
-
onProgress({
|
|
51
|
-
sent: event.loaded,
|
|
52
|
-
total: file.size,
|
|
53
|
-
percentage: event.loaded / file.size * 100
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}, false);
|
|
57
|
-
xhr.open("POST", getPreSignedPostPayload.data.data.url, true);
|
|
58
|
-
xhr.send(formData);
|
|
59
|
-
xhr.onload = function () {
|
|
60
|
-
if (this.status === 204) {
|
|
61
|
-
resolve(getPreSignedPostPayload.data.file);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
reject(this.responseText);
|
|
65
|
-
};
|
|
29
|
+
// 2. upload file to S3
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const formData = new window.FormData();
|
|
32
|
+
Object.keys(getPreSignedPostPayload.data.data.fields).forEach(key => {
|
|
33
|
+
formData.append(key, getPreSignedPostPayload.data.data.fields[key]);
|
|
66
34
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
35
|
+
formData.append("file", file);
|
|
36
|
+
const xhr = new window.XMLHttpRequest();
|
|
37
|
+
xhr.upload.addEventListener("progress", event => {
|
|
38
|
+
if (onProgress) {
|
|
39
|
+
onProgress({
|
|
40
|
+
sent: event.loaded,
|
|
41
|
+
total: file.size,
|
|
42
|
+
percentage: event.loaded / file.size * 100
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}, false);
|
|
46
|
+
xhr.open("POST", getPreSignedPostPayload.data.data.url, true);
|
|
47
|
+
xhr.send(formData);
|
|
48
|
+
xhr.onload = function () {
|
|
49
|
+
if (this.status === 204) {
|
|
50
|
+
resolve(getPreSignedPostPayload.data.file);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
reject(this.responseText);
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
70
58
|
|
|
71
59
|
//# sourceMappingURL=SimpleUploadStrategy.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["GET_PRE_SIGNED_POST_PAYLOAD","SimpleUploadStrategy","upload","file","apolloClient","onProgress","response","query","fetchPolicy","variables","data","size","name","type","key","keyPrefix","getPreSignedPostPayload","fileManager","error","console","Error","Promise","resolve","reject","formData","window","FormData","Object","keys","fields","forEach","append","xhr","XMLHttpRequest","addEventListener","event","sent","loaded","total","percentage","open","url","send","onload","status","responseText"],"sources":["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"],"mappings":"AACA,SAASA,2BAA2B;AAUpC,OAAO,MAAMC,oBAAoB,CAA+B;EAC5D,MAAMC,MAAMA,CAACC,IAAU,EAAE;IAAEC,YAAY;IAAEC;EAA0B,CAAC,EAAyB;IACzF;IACA,MAAMC,QAAQ,GAAG,MAAMF,YAAY,CAACG,KAAK,CAAC;MACtCA,KAAK,EAAEP,2BAA2B;MAClCQ,WAAW,EAAE,UAAU;MACvBC,SAAS,EAAE;QACPC,IAAI,EAAE;UACFC,IAAI,EAAER,IAAI,CAACQ,IAAI;UACfC,IAAI,EAAET,IAAI,CAACS,IAAI;UACfC,IAAI,EAAEV,IAAI,CAACU,IAAI;UACfC,GAAG,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AE,IAAI,CAAC;MAC7DT,GAAG,CAACU,IAAI,CAAClB,QAAQ,CAAC;MAClBQ,GAAG,CAACW,MAAM,GAAG,YAAY;QACrB,IAAI,IAAI,CAACC,MAAM,KAAK,GAAG,EAAE;UACrBtB,OAAO,CAACN,uBAAuB,CAACN,IAAI,CAACP,IAAI,CAAC;UAC1C;QACJ;QAEAoB,MAAM,CAAC,IAAI,CAACsB,YAAY,CAAC;MAC7B,CAAC;IACL,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
|
package/graphql.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import gql from "graphql-tag";
|
|
2
|
+
export const GET_PRE_SIGNED_POST_PAYLOAD = gql`
|
|
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
|
+
`;
|
|
12
23
|
|
|
13
24
|
//# sourceMappingURL=graphql.js.map
|
package/graphql.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["gql","GET_PRE_SIGNED_POST_PAYLOAD"],"sources":["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"],"mappings":"AAAA,OAAOA,GAAG,MAAM,aAAa;AAE7B,OAAO,MAAMC,2BAA2B,GAAGD,GAAG;AAC9C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC","ignoreList":[]}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,34 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var S3FileUploader = /*#__PURE__*/function () {
|
|
15
|
-
function S3FileUploader() {
|
|
16
|
-
(0, _classCallCheck2.default)(this, S3FileUploader);
|
|
17
|
-
(0, _defineProperty2.default)(this, "type", "file-uploader");
|
|
18
|
-
(0, _defineProperty2.default)(this, "name", "file-uploader");
|
|
1
|
+
import { SimpleUploadStrategy } from "./SimpleUploadStrategy.js";
|
|
2
|
+
import { MultiPartUploadStrategy } from "./MultiPartUploadStrategy.js";
|
|
3
|
+
export default () => {
|
|
4
|
+
class S3FileUploader {
|
|
5
|
+
type = "file-uploader";
|
|
6
|
+
name = "file-uploader";
|
|
7
|
+
upload(file, options) {
|
|
8
|
+
// Use "simple" strategy for files smaller than ~100MB
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
const multiPartThreshold = window["fmUploadMultiPartThreshold"] ?? 100;
|
|
11
|
+
const simple = file.size < multiPartThreshold * 1024 * 1024;
|
|
12
|
+
const strategy = simple ? new SimpleUploadStrategy() : new MultiPartUploadStrategy();
|
|
13
|
+
return strategy.upload(file, options);
|
|
19
14
|
}
|
|
20
|
-
|
|
21
|
-
key: "upload",
|
|
22
|
-
value: function upload(file, options) {
|
|
23
|
-
// Use "simple" strategy for files smaller than ~100MB
|
|
24
|
-
// @ts-expect-error
|
|
25
|
-
var multiPartThreshold = window["fmUploadMultiPartThreshold"] ?? 100;
|
|
26
|
-
var simple = file.size < multiPartThreshold * 1024 * 1024;
|
|
27
|
-
var strategy = simple ? new _SimpleUploadStrategy.SimpleUploadStrategy() : new _MultiPartUploadStrategy.MultiPartUploadStrategy();
|
|
28
|
-
return strategy.upload(file, options);
|
|
29
|
-
}
|
|
30
|
-
}]);
|
|
31
|
-
}();
|
|
15
|
+
}
|
|
32
16
|
return new S3FileUploader();
|
|
33
17
|
};
|
|
34
18
|
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["SimpleUploadStrategy","MultiPartUploadStrategy","S3FileUploader","type","name","upload","file","options","multiPartThreshold","window","simple","size","strategy"],"sources":["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"],"mappings":"AACA,SAASA,oBAAoB;AAC7B,SAASC,uBAAuB;AAMhC,eAAe,MAA0B;EACrC,MAAMC,cAAc,CAA+B;IAC/BC,IAAI,GAAG,eAAe;IACtBC,IAAI,GAAG,eAAe;IAEtCC,MAAMA,CAACC,IAAU,EAAEC,OAAsB,EAAE;MACvC;MACA;MACA,MAAMC,kBAAkB,GAAGC,MAAM,CAAC,4BAA4B,CAAC,IAAI,GAAG;MACtE,MAAMC,MAAM,GAAGJ,IAAI,CAACK,IAAI,GAAGH,kBAAkB,GAAG,IAAI,GAAG,IAAI;MAE3D,MAAMI,QAA4B,GAAGF,MAAM,GACrC,IAAIV,oBAAoB,CAAC,CAAC,GAC1B,IAAIC,uBAAuB,CAAC,CAAC;MAEnC,OAAOW,QAAQ,CAACP,MAAM,CAACC,IAAI,EAAEC,OAAO,CAAC;IACzC;EACJ;EAEA,OAAO,IAAIL,cAAc,CAAC,CAAC;AAC/B,CAAC","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/app-file-manager-s3",
|
|
3
|
-
"version": "6.0.0-
|
|
3
|
+
"version": "6.0.0-rc.1",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
@@ -10,25 +11,18 @@
|
|
|
10
11
|
"author": "Webiny Ltd",
|
|
11
12
|
"license": "MIT",
|
|
12
13
|
"dependencies": {
|
|
13
|
-
"@webiny/app": "6.0.0-
|
|
14
|
+
"@webiny/app": "6.0.0-rc.1",
|
|
14
15
|
"graphql-tag": "2.12.6",
|
|
15
|
-
"p-retry": "
|
|
16
|
+
"p-retry": "7.1.1"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
|
-
"@
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"@webiny/project-utils": "6.0.0-beta.0",
|
|
22
|
-
"rimraf": "5.0.5",
|
|
23
|
-
"typescript": "4.7.4"
|
|
19
|
+
"@webiny/build-tools": "6.0.0-rc.1",
|
|
20
|
+
"rimraf": "6.1.3",
|
|
21
|
+
"typescript": "5.9.3"
|
|
24
22
|
},
|
|
25
23
|
"publishConfig": {
|
|
26
24
|
"access": "public",
|
|
27
25
|
"directory": "dist"
|
|
28
26
|
},
|
|
29
|
-
"
|
|
30
|
-
"build": "yarn webiny run build",
|
|
31
|
-
"watch": "yarn webiny run watch"
|
|
32
|
-
},
|
|
33
|
-
"gitHead": "aa8dbfbbd5ad13ec271adba6f2431e02991a300f"
|
|
27
|
+
"gitHead": "36d702721ff9ed39fb21d6f5fe7922a2a8716e63"
|
|
34
28
|
}
|