@webiny/app-file-manager-s3 6.0.0-alpha.0 → 6.0.0-alpha.2
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.map +1 -1
- package/MultiPartUploadGraphQLAPI.d.ts +2 -2
- package/MultiPartUploadGraphQLAPI.js.map +1 -1
- package/MultiPartUploadStrategy.d.ts +2 -2
- package/MultiPartUploadStrategy.js.map +1 -1
- package/MultiPartUploader.d.ts +1 -1
- package/MultiPartUploader.js.map +1 -1
- package/SimpleUploadStrategy.d.ts +2 -2
- package/SimpleUploadStrategy.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.js.map +1 -1
- package/package.json +4 -4
package/MultiPartUploadAPI.d.ts
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\";\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";
|
|
2
|
+
import type { CompleteUploadParams, CreateUploadParams, MultiPartUpload, MultiPartUploadAPI } from "./MultiPartUploadAPI";
|
|
3
3
|
export declare class MultiPartUploadGraphQLAPI implements MultiPartUploadAPI {
|
|
4
4
|
private client;
|
|
5
5
|
constructor(client: UploadOptions["apolloClient"]);
|
|
@@ -1 +1 @@
|
|
|
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 { UploadOptions } from \"@webiny/app/types\";\nimport gql from \"graphql-tag\";\nimport {\n CompleteUploadParams,\n CreateUploadParams,\n MultiPartUpload,\n MultiPartUploadAPI\n} from \"~/MultiPartUploadAPI\";\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
|
+
{"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\";\nimport gql from \"graphql-tag\";\nimport type {\n CompleteUploadParams,\n CreateUploadParams,\n MultiPartUpload,\n MultiPartUploadAPI\n} from \"~/MultiPartUploadAPI\";\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";
|
|
2
|
+
import type { FileUploadStrategy } from "./index";
|
|
3
3
|
export declare class MultiPartUploadStrategy implements FileUploadStrategy {
|
|
4
4
|
upload(file: File, options: UploadOptions): Promise<UploadedFile>;
|
|
5
5
|
private detectChunkSize;
|
|
@@ -1 +1 @@
|
|
|
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 { UploadedFile, UploadOptions } from \"@webiny/app/types\";\nimport { FileUploadStrategy } from \"~/index\";\nimport { MultiPartUploader } from \"~/MultiPartUploader\";\nimport { MultiPartUploadGraphQLAPI } from \"~/MultiPartUploadGraphQLAPI\";\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":[]}
|
|
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\";\nimport type { FileUploadStrategy } from \"~/index\";\nimport { MultiPartUploader } from \"~/MultiPartUploader\";\nimport { MultiPartUploadGraphQLAPI } from \"~/MultiPartUploadGraphQLAPI\";\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
package/MultiPartUploader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 { 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,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,GAAGL,IAAI,CAACM,IAAI,CAACF,IAAI,CAACG,IAAI,GAAG,IAAI,CAACT,SAAS,CAAC;IAE3D,I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,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":[]}
|
|
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\";\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,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,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":[]}
|
|
@@ -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";
|
|
2
|
+
import type { FileUploadStrategy } from "./index";
|
|
3
3
|
declare global {
|
|
4
4
|
interface File {
|
|
5
5
|
key?: string;
|
|
@@ -1 +1 @@
|
|
|
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 { UploadedFile, UploadOptions } from \"@webiny/app/types\";\nimport { GET_PRE_SIGNED_POST_PAYLOAD } from \"./graphql\";\nimport { FileUploadStrategy } from \"~/index\";\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,EAAEX,IAAI,CAACW,GAAG;UACbC,SAAS,EAAEZ,IAAI,CAACY;QACpB;MACJ;IACJ,CAAC,CAAC;IAEF,MAAM;MAAEC;IAAwB,CAAC,GAAGV,QAAQ,CAACI,IAAI,CAACO,WAAW;IAC7D,IAAID,uBAAuB,CAACE,KAAK,EAAE;MAC/BC,OAAO,CAACD,KAAK,CAACF,uBAAuB,CAAC;MACtC,MAAMI,KAAK,CAACJ,uBAAuB,CAACE,KAAK,CAAC;IAC9C;;IAEA;IACA,OAAO,IAAIG,OAAO,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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\";\nimport { GET_PRE_SIGNED_POST_PAYLOAD } from \"./graphql\";\nimport type { FileUploadStrategy } from \"~/index\";\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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MAAM,CAACQ,cAAc,CAAC,CAAC;MACvCD,GAAG,CAAC9B,MAAM,CAACgC,gBAAgB,CACvB,UAAU,EACVC,KAAK,IAAI;QACL,IAAI9B,UAAU,EAAE;UACZA,UAAU,CAAC;YACP+B,IAAI,EAAED,KAAK,CAACE,MAAM;YAClBC,KAAK,EAAEnC,IAAI,CAACQ,IAAI;YAChB4B,UAAU,EAAGJ,KAAK,CAACE,MAAM,GAAGlC,IAAI,CAACQ,IAAI,GAAI;UAC7C,CAAC,CAAC;QACN;MACJ,CAAC,EACD,KACJ,CAAC;MACDqB,GAAG,CAACQ,IAAI,CAAC,MAAM,EAAExB,uBAAuB,CAACN,IAAI,CAACA,IAAI,CAAC+B,GAAG,EAAE,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/index.d.ts
CHANGED
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["SimpleUploadStrategy","MultiPartUploadStrategy","S3FileUploader","type","name","upload","file","options","multiPartThreshold","window","simple","size","strategy"],"sources":["index.ts"],"sourcesContent":["import { FileUploaderPlugin, UploadOptions } from \"@webiny/app/types\";\nimport { SimpleUploadStrategy } from \"~/SimpleUploadStrategy\";\nimport { MultiPartUploadStrategy } from \"~/MultiPartUploadStrategy\";\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":[]}
|
|
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\";\nimport { SimpleUploadStrategy } from \"~/SimpleUploadStrategy\";\nimport { MultiPartUploadStrategy } from \"~/MultiPartUploadStrategy\";\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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/app-file-manager-s3",
|
|
3
|
-
"version": "6.0.0-alpha.
|
|
3
|
+
"version": "6.0.0-alpha.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
"author": "Webiny Ltd",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@webiny/app": "6.0.0-alpha.
|
|
13
|
+
"@webiny/app": "6.0.0-alpha.2",
|
|
14
14
|
"graphql-tag": "2.12.6",
|
|
15
15
|
"p-retry": "4.6.2"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@webiny/project-utils": "6.0.0-alpha.
|
|
18
|
+
"@webiny/project-utils": "6.0.0-alpha.2",
|
|
19
19
|
"rimraf": "6.0.1",
|
|
20
20
|
"typescript": "5.3.3"
|
|
21
21
|
},
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"build": "node ../cli/bin.js run build",
|
|
28
28
|
"watch": "node ../cli/bin.js run watch"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "7c9e8fbfd62a57ece5f880dbad6c864636b0355e"
|
|
31
31
|
}
|