@webiny/api-file-manager-s3 0.0.0-unstable.a4f3e4ea8b → 0.0.0-unstable.a85ef555b2
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/FileManagerS3Feature.d.ts +9 -0
- package/FileManagerS3Feature.js +27 -0
- package/FileManagerS3Feature.js.map +1 -0
- package/assetDelivery/assetDeliveryConfig.d.ts +1 -1
- package/assetDelivery/assetDeliveryConfig.js +0 -2
- package/assetDelivery/assetDeliveryConfig.js.map +1 -1
- package/assetDelivery/createAssetDelivery.js +1 -1
- package/assetDelivery/createAssetDelivery.js.map +1 -1
- package/assetDelivery/feature.js +4 -6
- package/assetDelivery/feature.js.map +1 -1
- package/assetDelivery/index.d.ts +2 -1
- package/assetDelivery/index.js +2 -1
- package/assetDelivery/s3/LazySharpTransform.d.ts +34 -0
- package/assetDelivery/s3/LazySharpTransform.js +34 -0
- package/assetDelivery/s3/LazySharpTransform.js.map +1 -0
- package/assetDelivery/s3/S3AssetResolver.d.ts +9 -10
- package/assetDelivery/s3/S3AssetResolver.js +8 -10
- package/assetDelivery/s3/S3AssetResolver.js.map +1 -1
- package/assetDelivery/s3/S3ContentsReader.d.ts +5 -6
- package/assetDelivery/s3/S3ContentsReader.js +0 -3
- package/assetDelivery/s3/S3ContentsReader.js.map +1 -1
- package/assetDelivery/s3/S3ErrorAssetReply.d.ts +2 -3
- package/assetDelivery/s3/S3ErrorAssetReply.js +1 -4
- package/assetDelivery/s3/S3ErrorAssetReply.js.map +1 -1
- package/assetDelivery/s3/S3OutputStrategy.d.ts +6 -8
- package/assetDelivery/s3/S3OutputStrategy.js +5 -6
- package/assetDelivery/s3/S3OutputStrategy.js.map +1 -1
- package/assetDelivery/s3/S3RedirectAssetReply.d.ts +1 -1
- package/assetDelivery/s3/S3RedirectAssetReply.js +1 -1
- package/assetDelivery/s3/S3RedirectAssetReply.js.map +1 -1
- package/assetDelivery/s3/S3StreamAssetReply.d.ts +5 -0
- package/assetDelivery/s3/S3StreamAssetReply.js +17 -0
- package/assetDelivery/s3/S3StreamAssetReply.js.map +1 -0
- package/assetDelivery/s3/SharpTransform.d.ts +2 -2
- package/assetDelivery/s3/SharpTransform.js +20 -33
- package/assetDelivery/s3/SharpTransform.js.map +1 -1
- package/assetDelivery/s3/transformation/AssetKeyGenerator.d.ts +8 -0
- package/assetDelivery/s3/transformation/AssetKeyGenerator.js +21 -0
- package/assetDelivery/s3/transformation/AssetKeyGenerator.js.map +1 -0
- package/assetDelivery/s3/transformation/CallableContentsReader.d.ts +10 -0
- package/assetDelivery/s3/transformation/CallableContentsReader.js +11 -0
- package/assetDelivery/s3/transformation/CallableContentsReader.js.map +1 -0
- package/assetDelivery/s3/transformation/WidthCollection.d.ts +7 -0
- package/assetDelivery/s3/transformation/WidthCollection.js +19 -0
- package/assetDelivery/s3/transformation/WidthCollection.js.map +1 -0
- package/assetDelivery/s3/transformation/utils.d.ts +9 -0
- package/assetDelivery/s3/transformation/utils.js +31 -0
- package/assetDelivery/s3/transformation/utils.js.map +1 -0
- package/assetDelivery/threatDetection/ObjectKey.d.ts +7 -0
- package/assetDelivery/threatDetection/ObjectKey.js +18 -0
- package/assetDelivery/threatDetection/ObjectKey.js.map +1 -0
- package/assetDelivery/threatDetection/createThreatDetectionEventHandler.d.ts +21 -1
- package/assetDelivery/threatDetection/createThreatDetectionEventHandler.js +46 -31
- package/assetDelivery/threatDetection/createThreatDetectionEventHandler.js.map +1 -1
- package/assetDelivery/threatDetection/processThreatScanResult.js +4 -4
- package/assetDelivery/threatDetection/processThreatScanResult.js.map +1 -1
- package/enterprise/ApplyThreatScanning/CreateFileWithThreatScanDecorator.d.ts +3 -1
- package/enterprise/ApplyThreatScanning/CreateFileWithThreatScanDecorator.js +7 -2
- package/enterprise/ApplyThreatScanning/CreateFileWithThreatScanDecorator.js.map +1 -1
- package/features/DeleteFileFromBucket/DeleteS3FolderTask.d.ts +8 -3
- package/features/DeleteFileFromBucket/DeleteS3FolderTask.js +9 -2
- package/features/DeleteFileFromBucket/DeleteS3FolderTask.js.map +1 -1
- package/features/ExtractMetadata/ExtractMetadataHandler.d.ts +11 -1
- package/features/ExtractMetadata/ExtractMetadataHandler.js +25 -1
- package/features/ExtractMetadata/ExtractMetadataHandler.js.map +1 -0
- package/features/ExtractMetadata/ExtractMetadataTask.d.ts +24 -17
- package/features/ExtractMetadata/ExtractMetadataTask.js +31 -20
- package/features/ExtractMetadata/ExtractMetadataTask.js.map +1 -1
- package/features/FlushCache/FlushCacheOnFileDeleteHandler.js +1 -1
- package/features/FlushCache/FlushCacheOnFileDeleteHandler.js.map +1 -1
- package/features/FlushCache/FlushCacheOnFileUpdateHandler.js +1 -1
- package/features/FlushCache/FlushCacheOnFileUpdateHandler.js.map +1 -1
- package/features/FlushCache/InvalidateCacheTask.d.ts +10 -5
- package/features/FlushCache/InvalidateCacheTask.js +16 -7
- package/features/FlushCache/InvalidateCacheTask.js.map +1 -1
- package/features/GetFileContentsById/GetFileContentsByIdUseCase.d.ts +3 -3
- package/features/GetFileContentsById/GetFileContentsByIdUseCase.js +5 -4
- package/features/GetFileContentsById/GetFileContentsByIdUseCase.js.map +1 -1
- package/features/WriteFileMetadata/MetadataReader.d.ts +14 -0
- package/features/WriteFileMetadata/MetadataReader.js +13 -0
- package/features/WriteFileMetadata/MetadataReader.js.map +1 -0
- package/features/WriteFileMetadata/MetadataWriter.d.ts +10 -0
- package/features/WriteFileMetadata/MetadataWriter.js +28 -0
- package/features/WriteFileMetadata/MetadataWriter.js.map +1 -0
- package/features/WriteFileMetadata/WriteMetadataAfterBatchCreateHandler.d.ts +12 -0
- package/features/WriteFileMetadata/WriteMetadataAfterBatchCreateHandler.js +23 -0
- package/features/WriteFileMetadata/WriteMetadataAfterBatchCreateHandler.js.map +1 -0
- package/features/WriteFileMetadata/WriteMetadataAfterCreateHandler.d.ts +12 -0
- package/features/WriteFileMetadata/WriteMetadataAfterCreateHandler.js +25 -0
- package/features/WriteFileMetadata/WriteMetadataAfterCreateHandler.js.map +1 -0
- package/features/{GetUploadPayload → WriteFileMetadata}/feature.d.ts +1 -1
- package/features/WriteFileMetadata/feature.js +13 -0
- package/features/WriteFileMetadata/feature.js.map +1 -0
- package/graphql/S3GraphQLSchema.d.ts +10 -0
- package/graphql/S3GraphQLSchema.js +235 -0
- package/graphql/S3GraphQLSchema.js.map +1 -0
- package/graphql/checkPermissions.d.ts +5 -0
- package/graphql/checkPermissions.js +22 -0
- package/graphql/checkPermissions.js.map +1 -0
- package/index.d.ts +3 -3
- package/index.js +2 -32
- package/multiPartUpload/CompleteMultiPartUploadUseCase.d.ts +15 -0
- package/multiPartUpload/CompleteMultiPartUploadUseCase.js +55 -0
- package/multiPartUpload/CompleteMultiPartUploadUseCase.js.map +1 -0
- package/multiPartUpload/CreateMultiPartUploadUseCase.d.ts +20 -0
- package/multiPartUpload/CreateMultiPartUploadUseCase.js +34 -0
- package/multiPartUpload/CreateMultiPartUploadUseCase.js.map +1 -0
- package/package.json +24 -16
- package/types.d.ts +20 -2
- package/utils/FileExtension.d.ts +6 -0
- package/utils/FileExtension.js +16 -0
- package/utils/FileExtension.js.map +1 -0
- package/utils/FileKey.d.ts +11 -0
- package/utils/FileKey.js +33 -0
- package/utils/FileKey.js.map +1 -0
- package/utils/FileKey.test.d.ts +1 -0
- package/utils/FileKey.test.js +59 -0
- package/utils/FileKey.test.js.map +1 -0
- package/utils/FileNormalizer.d.ts +18 -0
- package/utils/FileNormalizer.js +41 -0
- package/utils/FileNormalizer.js.map +1 -0
- package/utils/FileUploadModifier.d.ts +36 -0
- package/utils/FileUploadModifier.js +41 -0
- package/utils/FileUploadModifier.js.map +1 -0
- package/utils/createFileNormalizerFromContext.d.ts +3 -0
- package/utils/createFileNormalizerFromContext.js +9 -0
- package/utils/createFileNormalizerFromContext.js.map +1 -0
- package/utils/getPresignedPostPayload.d.ts +4 -0
- package/utils/getPresignedPostPayload.js +43 -0
- package/utils/getPresignedPostPayload.js.map +1 -0
- package/utils/mimeTypes.d.ts +5 -0
- package/utils/mimeTypes.js +9 -0
- package/utils/mimeTypes.js.map +1 -0
- package/features/CompleteMultiPartUpload/CompleteMultiPartUploadUseCase.d.ts +0 -8
- package/features/CompleteMultiPartUpload/CompleteMultiPartUploadUseCase.js +0 -51
- package/features/CompleteMultiPartUpload/CompleteMultiPartUploadUseCase.js.map +0 -1
- package/features/CompleteMultiPartUpload/feature.d.ts +0 -4
- package/features/CompleteMultiPartUpload/feature.js +0 -11
- package/features/CompleteMultiPartUpload/feature.js.map +0 -1
- package/features/CreateMultiPartUpload/CreateMultiPartUploadUseCase.d.ts +0 -9
- package/features/CreateMultiPartUpload/CreateMultiPartUploadUseCase.js +0 -40
- package/features/CreateMultiPartUpload/CreateMultiPartUploadUseCase.js.map +0 -1
- package/features/CreateMultiPartUpload/feature.d.ts +0 -4
- package/features/CreateMultiPartUpload/feature.js +0 -11
- package/features/CreateMultiPartUpload/feature.js.map +0 -1
- package/features/GetUploadPayload/GetUploadPayloadUseCase.d.ts +0 -14
- package/features/GetUploadPayload/GetUploadPayloadUseCase.js +0 -54
- package/features/GetUploadPayload/GetUploadPayloadUseCase.js.map +0 -1
- package/features/GetUploadPayload/feature.js +0 -11
- package/features/GetUploadPayload/feature.js.map +0 -1
- package/index.js.map +0 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { UploadPartCommand, getSignedUrl } from "@webiny/aws-sdk/client-s3/index.js";
|
|
2
|
+
class CreateMultiPartUploadUseCase {
|
|
3
|
+
constructor(bucket, s3Client){
|
|
4
|
+
this.bucket = bucket;
|
|
5
|
+
this.s3 = s3Client;
|
|
6
|
+
}
|
|
7
|
+
async execute({ file, numberOfParts }) {
|
|
8
|
+
const s3Params = {
|
|
9
|
+
Bucket: this.bucket,
|
|
10
|
+
Key: file.key
|
|
11
|
+
};
|
|
12
|
+
const { UploadId } = await this.s3.createMultipartUpload(s3Params);
|
|
13
|
+
const parts = await Promise.all(Array.from({
|
|
14
|
+
length: numberOfParts
|
|
15
|
+
}).map((_, index)=>getSignedUrl(this.s3, new UploadPartCommand({
|
|
16
|
+
...s3Params,
|
|
17
|
+
UploadId,
|
|
18
|
+
PartNumber: index + 1
|
|
19
|
+
}), {
|
|
20
|
+
expiresIn: 86400
|
|
21
|
+
}).then((url)=>({
|
|
22
|
+
url,
|
|
23
|
+
partNumber: index + 1
|
|
24
|
+
}))));
|
|
25
|
+
return {
|
|
26
|
+
file,
|
|
27
|
+
uploadId: UploadId,
|
|
28
|
+
parts
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export { CreateMultiPartUploadUseCase };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=CreateMultiPartUploadUseCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiPartUpload/CreateMultiPartUploadUseCase.js","sources":["../../src/multiPartUpload/CreateMultiPartUploadUseCase.ts"],"sourcesContent":["import type { S3 } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { getSignedUrl, UploadPartCommand } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport type { FileData } from \"~/types.js\";\n\ninterface CreateMultiPartUploadParams {\n file: FileData;\n numberOfParts: number;\n}\n\nexport class CreateMultiPartUploadUseCase {\n private readonly s3: S3;\n private readonly bucket: string;\n\n constructor(bucket: string, s3Client: S3) {\n this.bucket = bucket;\n this.s3 = s3Client;\n }\n\n async execute({ file, numberOfParts }: CreateMultiPartUploadParams) {\n const s3Params = { Bucket: this.bucket, Key: file.key };\n\n const { UploadId } = await this.s3.createMultipartUpload(s3Params);\n\n const parts = await Promise.all(\n Array.from({ length: numberOfParts }).map((_, index) => {\n return getSignedUrl(\n this.s3,\n new UploadPartCommand({ ...s3Params, UploadId, PartNumber: index + 1 }),\n {\n // URL expires after 24 hours.\n expiresIn: 86400\n }\n ).then(url => ({\n url,\n partNumber: index + 1\n }));\n })\n );\n\n return {\n file,\n uploadId: UploadId,\n parts\n };\n }\n}\n"],"names":["CreateMultiPartUploadUseCase","bucket","s3Client","file","numberOfParts","s3Params","UploadId","parts","Promise","Array","_","index","getSignedUrl","UploadPartCommand","url"],"mappings":";AASO,MAAMA;IAIT,YAAYC,MAAc,EAAEC,QAAY,CAAE;QACtC,IAAI,CAAC,MAAM,GAAGD;QACd,IAAI,CAAC,EAAE,GAAGC;IACd;IAEA,MAAM,QAAQ,EAAEC,IAAI,EAAEC,aAAa,EAA+B,EAAE;QAChE,MAAMC,WAAW;YAAE,QAAQ,IAAI,CAAC,MAAM;YAAE,KAAKF,KAAK,GAAG;QAAC;QAEtD,MAAM,EAAEG,QAAQ,EAAE,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,qBAAqB,CAACD;QAEzD,MAAME,QAAQ,MAAMC,QAAQ,GAAG,CAC3BC,MAAM,IAAI,CAAC;YAAE,QAAQL;QAAc,GAAG,GAAG,CAAC,CAACM,GAAGC,QACnCC,aACH,IAAI,CAAC,EAAE,EACP,IAAIC,kBAAkB;gBAAE,GAAGR,QAAQ;gBAAEC;gBAAU,YAAYK,QAAQ;YAAE,IACrE;gBAEI,WAAW;YACf,GACF,IAAI,CAACG,CAAAA,MAAQ;oBACXA;oBACA,YAAYH,QAAQ;gBACxB;QAIR,OAAO;YACHR;YACA,UAAUG;YACVC;QACJ;IACJ;AACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-file-manager-s3",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.a85ef555b2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -14,26 +14,34 @@
|
|
|
14
14
|
"author": "Webiny Ltd",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@webiny/api": "0.0.0-unstable.
|
|
18
|
-
"@webiny/api-core": "0.0.0-unstable.
|
|
19
|
-
"@webiny/api-file-manager": "0.0.0-unstable.
|
|
20
|
-
"@webiny/api-
|
|
21
|
-
"@webiny/
|
|
22
|
-
"@webiny/
|
|
23
|
-
"@webiny/
|
|
24
|
-
"@webiny/
|
|
25
|
-
"@webiny/handler-aws": "0.0.0-unstable.
|
|
26
|
-
"@webiny/
|
|
27
|
-
"@webiny/
|
|
28
|
-
"@webiny/
|
|
29
|
-
"
|
|
17
|
+
"@webiny/api": "0.0.0-unstable.a85ef555b2",
|
|
18
|
+
"@webiny/api-core": "0.0.0-unstable.a85ef555b2",
|
|
19
|
+
"@webiny/api-file-manager": "0.0.0-unstable.a85ef555b2",
|
|
20
|
+
"@webiny/api-graphql": "0.0.0-unstable.a85ef555b2",
|
|
21
|
+
"@webiny/api-websockets": "0.0.0-unstable.a85ef555b2",
|
|
22
|
+
"@webiny/aws-sdk": "0.0.0-unstable.a85ef555b2",
|
|
23
|
+
"@webiny/background-tasks": "0.0.0-unstable.a85ef555b2",
|
|
24
|
+
"@webiny/di": "2.0.0",
|
|
25
|
+
"@webiny/event-handler-aws": "0.0.0-unstable.a85ef555b2",
|
|
26
|
+
"@webiny/event-handler-core": "0.0.0-unstable.a85ef555b2",
|
|
27
|
+
"@webiny/feature": "0.0.0-unstable.a85ef555b2",
|
|
28
|
+
"@webiny/handler": "0.0.0-unstable.a85ef555b2",
|
|
29
|
+
"@webiny/plugins": "0.0.0-unstable.a85ef555b2",
|
|
30
|
+
"@webiny/utils": "0.0.0-unstable.a85ef555b2",
|
|
31
|
+
"@webiny/validation": "0.0.0-unstable.a85ef555b2",
|
|
32
|
+
"exifreader": "4.43.0",
|
|
33
|
+
"mime": "4.1.0",
|
|
34
|
+
"object-hash": "3.0.0",
|
|
35
|
+
"p-map": "7.0.6",
|
|
36
|
+
"p-reduce": "3.0.0",
|
|
37
|
+
"sanitize-filename": "1.6.4",
|
|
30
38
|
"sharp": "0.35.3"
|
|
31
39
|
},
|
|
32
40
|
"devDependencies": {
|
|
33
|
-
"@webiny/build-tools": "0.0.0-unstable.
|
|
41
|
+
"@webiny/build-tools": "0.0.0-unstable.a85ef555b2",
|
|
34
42
|
"rimraf": "6.1.3",
|
|
35
43
|
"typescript": "7.0.2",
|
|
36
|
-
"vitest": "4.1.
|
|
44
|
+
"vitest": "4.1.11"
|
|
37
45
|
},
|
|
38
46
|
"publishConfig": {
|
|
39
47
|
"access": "public"
|
package/types.d.ts
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
import type { PresignedPost } from "@webiny/aws-sdk/client-s3/index.js";
|
|
1
2
|
import "@webiny/background-tasks/api/features/TaskController/augmentation.js";
|
|
2
|
-
export
|
|
3
|
-
|
|
3
|
+
export interface PresignedPostPayloadData {
|
|
4
|
+
name: string;
|
|
5
|
+
type: string;
|
|
6
|
+
size: number;
|
|
7
|
+
id?: string;
|
|
8
|
+
key?: string;
|
|
9
|
+
keyPrefix?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface FileData {
|
|
12
|
+
id: string;
|
|
13
|
+
key: string;
|
|
14
|
+
name: string;
|
|
15
|
+
size: number;
|
|
16
|
+
type: string;
|
|
17
|
+
}
|
|
18
|
+
export interface PresignedPostPayloadDataResponse {
|
|
19
|
+
data: PresignedPost;
|
|
20
|
+
file: FileData;
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { mimeTypes as external_mimeTypes_js_mimeTypes } from "./mimeTypes.js";
|
|
2
|
+
class FileExtension {
|
|
3
|
+
constructor(data){
|
|
4
|
+
this.data = data;
|
|
5
|
+
}
|
|
6
|
+
getValue() {
|
|
7
|
+
const name = (this.data.key || this.data.name).toLowerCase();
|
|
8
|
+
const maybeExt = name.split(".").pop();
|
|
9
|
+
const extensions = external_mimeTypes_js_mimeTypes[this.data.type];
|
|
10
|
+
if (extensions && !extensions.includes(maybeExt)) return extensions[0];
|
|
11
|
+
return "";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export { FileExtension };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=FileExtension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/FileExtension.js","sources":["../../src/utils/FileExtension.ts"],"sourcesContent":["import { mimeTypes } from \"~/utils/mimeTypes.js\";\nimport type { PresignedPostPayloadData } from \"~/types.js\";\n\nexport class FileExtension {\n private data: PresignedPostPayloadData;\n\n constructor(data: PresignedPostPayloadData) {\n this.data = data;\n }\n\n getValue() {\n const name = (this.data.key || this.data.name).toLowerCase();\n\n const maybeExt = name.split(\".\").pop() as string;\n const extensions = mimeTypes[this.data.type];\n if (extensions && !extensions.includes(maybeExt)) {\n return extensions[0];\n }\n\n return \"\";\n }\n}\n"],"names":["FileExtension","data","name","maybeExt","extensions","mimeTypes"],"mappings":";AAGO,MAAMA;IAGT,YAAYC,IAA8B,CAAE;QACxC,IAAI,CAAC,IAAI,GAAGA;IAChB;IAEA,WAAW;QACP,MAAMC,OAAQ,KAAI,CAAC,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,IAAG,EAAG,WAAW;QAE1D,MAAMC,WAAWD,KAAK,KAAK,CAAC,KAAK,GAAG;QACpC,MAAME,aAAaC,+BAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAC5C,IAAID,cAAc,CAACA,WAAW,QAAQ,CAACD,WACnC,OAAOC,UAAU,CAAC,EAAE;QAGxB,OAAO;IACX;AACJ"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PresignedPostPayloadData } from "../types.js";
|
|
2
|
+
export declare class FileKey {
|
|
3
|
+
private data;
|
|
4
|
+
private extension;
|
|
5
|
+
private key;
|
|
6
|
+
constructor(data: PresignedPostPayloadData);
|
|
7
|
+
getExtension(): string;
|
|
8
|
+
setKey(key: string): this;
|
|
9
|
+
toString(): string;
|
|
10
|
+
private getSanitizedKey;
|
|
11
|
+
}
|
package/utils/FileKey.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import sanitize_filename from "sanitize-filename";
|
|
2
|
+
import { FileExtension } from "./FileExtension.js";
|
|
3
|
+
class FileKey {
|
|
4
|
+
constructor(data){
|
|
5
|
+
this.data = data;
|
|
6
|
+
this.extension = new FileExtension(data);
|
|
7
|
+
this.key = this.getSanitizedKey();
|
|
8
|
+
}
|
|
9
|
+
getExtension() {
|
|
10
|
+
return this.extension.getValue();
|
|
11
|
+
}
|
|
12
|
+
setKey(key) {
|
|
13
|
+
this.key = key;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
toString() {
|
|
17
|
+
return [
|
|
18
|
+
this.data.keyPrefix,
|
|
19
|
+
this.data.id,
|
|
20
|
+
this.key
|
|
21
|
+
].filter(Boolean).join("/");
|
|
22
|
+
}
|
|
23
|
+
getSanitizedKey() {
|
|
24
|
+
const key = sanitize_filename(this.data.key || this.data.name).replace(/\s/g, "");
|
|
25
|
+
return [
|
|
26
|
+
key,
|
|
27
|
+
this.getExtension()
|
|
28
|
+
].filter(Boolean).join(".");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export { FileKey };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=FileKey.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/FileKey.js","sources":["../../src/utils/FileKey.ts"],"sourcesContent":["import sanitizeFilename from \"sanitize-filename\";\nimport type { PresignedPostPayloadData } from \"~/types.js\";\nimport { FileExtension } from \"~/utils/FileExtension.js\";\n\nexport class FileKey {\n private data: PresignedPostPayloadData;\n private extension: FileExtension;\n private key: string;\n\n constructor(data: PresignedPostPayloadData) {\n this.data = data;\n this.extension = new FileExtension(data);\n this.key = this.getSanitizedKey();\n }\n\n getExtension() {\n return this.extension.getValue();\n }\n\n setKey(key: string) {\n this.key = key;\n return this;\n }\n\n toString() {\n return [this.data.keyPrefix, this.data.id, this.key].filter(Boolean).join(\"/\");\n }\n\n private getSanitizedKey() {\n const key = sanitizeFilename(this.data.key || this.data.name).replace(/\\s/g, \"\");\n\n return [key, this.getExtension()].filter(Boolean).join(\".\");\n }\n}\n"],"names":["FileKey","data","FileExtension","key","Boolean","sanitizeFilename"],"mappings":";;AAIO,MAAMA;IAKT,YAAYC,IAA8B,CAAE;QACxC,IAAI,CAAC,IAAI,GAAGA;QACZ,IAAI,CAAC,SAAS,GAAG,IAAIC,cAAcD;QACnC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,eAAe;IACnC;IAEA,eAAe;QACX,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ;IAClC;IAEA,OAAOE,GAAW,EAAE;QAChB,IAAI,CAAC,GAAG,GAAGA;QACX,OAAO,IAAI;IACf;IAEA,WAAW;QACP,OAAO;YAAC,IAAI,CAAC,IAAI,CAAC,SAAS;YAAE,IAAI,CAAC,IAAI,CAAC,EAAE;YAAE,IAAI,CAAC,GAAG;SAAC,CAAC,MAAM,CAACC,SAAS,IAAI,CAAC;IAC9E;IAEQ,kBAAkB;QACtB,MAAMD,MAAME,kBAAiB,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO;QAE7E,OAAO;YAACF;YAAK,IAAI,CAAC,YAAY;SAAG,CAAC,MAAM,CAACC,SAAS,IAAI,CAAC;IAC3D;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { FileKey } from "./FileKey.js";
|
|
3
|
+
describe("FileKey", ()=>{
|
|
4
|
+
it("should generate a file key (extension is included in the name)", ()=>{
|
|
5
|
+
const fileKey = new FileKey({
|
|
6
|
+
size: 1071690,
|
|
7
|
+
name: "image-14.jpg",
|
|
8
|
+
type: "image/jpeg"
|
|
9
|
+
});
|
|
10
|
+
expect(fileKey.toString()).toEqual("image-14.jpg");
|
|
11
|
+
});
|
|
12
|
+
it("should generate a file key (extension derived from file type)", ()=>{
|
|
13
|
+
const fileKey = new FileKey({
|
|
14
|
+
size: 1071690,
|
|
15
|
+
name: "image-14",
|
|
16
|
+
type: "image/jpeg"
|
|
17
|
+
});
|
|
18
|
+
expect(fileKey.toString()).toEqual("image-14.jpg");
|
|
19
|
+
});
|
|
20
|
+
it("should generate a file key containing id", ()=>{
|
|
21
|
+
const fileKey = new FileKey({
|
|
22
|
+
size: 1071690,
|
|
23
|
+
name: "image-14.jpeg",
|
|
24
|
+
type: "image/jpeg",
|
|
25
|
+
id: "12345678"
|
|
26
|
+
});
|
|
27
|
+
expect(fileKey.toString()).toEqual("12345678/image-14.jpeg");
|
|
28
|
+
});
|
|
29
|
+
it("should generate a file key containing prefix", ()=>{
|
|
30
|
+
const fileKey = new FileKey({
|
|
31
|
+
size: 1071690,
|
|
32
|
+
name: "image-14.jpeg",
|
|
33
|
+
type: "image/jpeg",
|
|
34
|
+
keyPrefix: "prefix"
|
|
35
|
+
});
|
|
36
|
+
expect(fileKey.toString()).toEqual("prefix/image-14.jpeg");
|
|
37
|
+
});
|
|
38
|
+
it("should generate a file key containing id and prefix", ()=>{
|
|
39
|
+
const fileKey = new FileKey({
|
|
40
|
+
size: 1071690,
|
|
41
|
+
name: "image-14.jpeg",
|
|
42
|
+
type: "image/jpeg",
|
|
43
|
+
id: "12345678",
|
|
44
|
+
keyPrefix: "prefix"
|
|
45
|
+
});
|
|
46
|
+
expect(fileKey.toString()).toEqual("prefix/12345678/image-14.jpeg");
|
|
47
|
+
});
|
|
48
|
+
it("should use the key defined in the input", ()=>{
|
|
49
|
+
const fileKey = new FileKey({
|
|
50
|
+
size: 1071690,
|
|
51
|
+
name: "image",
|
|
52
|
+
type: "image/jpeg",
|
|
53
|
+
key: "image-14.jpg"
|
|
54
|
+
});
|
|
55
|
+
expect(fileKey.toString()).toEqual("image-14.jpg");
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=FileKey.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/FileKey.test.js","sources":["../../src/utils/FileKey.test.ts"],"sourcesContent":["import { describe, it, expect } from \"vitest\";\nimport { FileKey } from \"./FileKey.js\";\n\ndescribe(\"FileKey\", () => {\n it(\"should generate a file key (extension is included in the name)\", () => {\n const fileKey = new FileKey({\n size: 1071690,\n name: \"image-14.jpg\",\n type: \"image/jpeg\"\n });\n\n expect(fileKey.toString()).toEqual(\"image-14.jpg\");\n });\n\n it(\"should generate a file key (extension derived from file type)\", () => {\n const fileKey = new FileKey({\n size: 1071690,\n name: \"image-14\",\n type: \"image/jpeg\"\n });\n\n expect(fileKey.toString()).toEqual(\"image-14.jpg\");\n });\n\n it(\"should generate a file key containing id\", () => {\n const fileKey = new FileKey({\n size: 1071690,\n name: \"image-14.jpeg\",\n type: \"image/jpeg\",\n id: \"12345678\"\n });\n\n expect(fileKey.toString()).toEqual(\"12345678/image-14.jpeg\");\n });\n\n it(\"should generate a file key containing prefix\", () => {\n const fileKey = new FileKey({\n size: 1071690,\n name: \"image-14.jpeg\",\n type: \"image/jpeg\",\n keyPrefix: \"prefix\"\n });\n\n expect(fileKey.toString()).toEqual(\"prefix/image-14.jpeg\");\n });\n\n it(\"should generate a file key containing id and prefix\", () => {\n const fileKey = new FileKey({\n size: 1071690,\n name: \"image-14.jpeg\",\n type: \"image/jpeg\",\n id: \"12345678\",\n keyPrefix: \"prefix\"\n });\n\n expect(fileKey.toString()).toEqual(\"prefix/12345678/image-14.jpeg\");\n });\n\n it(\"should use the key defined in the input\", () => {\n const fileKey = new FileKey({\n size: 1071690,\n name: \"image\",\n type: \"image/jpeg\",\n key: \"image-14.jpg\"\n });\n\n expect(fileKey.toString()).toEqual(\"image-14.jpg\");\n });\n});\n"],"names":["describe","it","fileKey","FileKey","expect"],"mappings":";;AAGAA,SAAS,WAAW;IAChBC,GAAG,kEAAkE;QACjE,MAAMC,UAAU,IAAIC,QAAQ;YACxB,MAAM;YACN,MAAM;YACN,MAAM;QACV;QAEAC,OAAOF,QAAQ,QAAQ,IAAI,OAAO,CAAC;IACvC;IAEAD,GAAG,iEAAiE;QAChE,MAAMC,UAAU,IAAIC,QAAQ;YACxB,MAAM;YACN,MAAM;YACN,MAAM;QACV;QAEAC,OAAOF,QAAQ,QAAQ,IAAI,OAAO,CAAC;IACvC;IAEAD,GAAG,4CAA4C;QAC3C,MAAMC,UAAU,IAAIC,QAAQ;YACxB,MAAM;YACN,MAAM;YACN,MAAM;YACN,IAAI;QACR;QAEAC,OAAOF,QAAQ,QAAQ,IAAI,OAAO,CAAC;IACvC;IAEAD,GAAG,gDAAgD;QAC/C,MAAMC,UAAU,IAAIC,QAAQ;YACxB,MAAM;YACN,MAAM;YACN,MAAM;YACN,WAAW;QACf;QAEAC,OAAOF,QAAQ,QAAQ,IAAI,OAAO,CAAC;IACvC;IAEAD,GAAG,uDAAuD;QACtD,MAAMC,UAAU,IAAIC,QAAQ;YACxB,MAAM;YACN,MAAM;YACN,MAAM;YACN,IAAI;YACJ,WAAW;QACf;QAEAC,OAAOF,QAAQ,QAAQ,IAAI,OAAO,CAAC;IACvC;IAEAD,GAAG,2CAA2C;QAC1C,MAAMC,UAAU,IAAIC,QAAQ;YACxB,MAAM;YACN,MAAM;YACN,MAAM;YACN,KAAK;QACT;QAEAC,OAAOF,QAAQ,QAAQ,IAAI,OAAO,CAAC;IACvC;AACJ"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FileData, PresignedPostPayloadData } from "../types.js";
|
|
2
|
+
import type { FileModifier } from "./FileUploadModifier.js";
|
|
3
|
+
export interface FileToSign {
|
|
4
|
+
name: string;
|
|
5
|
+
key: string;
|
|
6
|
+
type: string;
|
|
7
|
+
size: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* FileNormalizer normalizes file data, before it's signed for upload to S3.
|
|
11
|
+
* It generates a unique file id, and makes sure that the file key includes the unique id.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export declare class FileNormalizer {
|
|
15
|
+
private readonly modifier;
|
|
16
|
+
constructor(modifier?: FileModifier);
|
|
17
|
+
normalizeFile(payload: PresignedPostPayloadData): Promise<FileData>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { mdbid } from "@webiny/utils";
|
|
2
|
+
import { FileKey } from "./FileKey.js";
|
|
3
|
+
import { mimeTypes as external_mimeTypes_js_mimeTypes } from "./mimeTypes.js";
|
|
4
|
+
const extensionToMime = new Map();
|
|
5
|
+
for (const [mime, extensions] of Object.entries(external_mimeTypes_js_mimeTypes))for (const ext of extensions)if (!extensionToMime.has(ext)) extensionToMime.set(ext, mime);
|
|
6
|
+
function resolveTypeFromName(name) {
|
|
7
|
+
const ext = name.slice(name.lastIndexOf(".") + 1).toLowerCase();
|
|
8
|
+
return extensionToMime.get(ext) || "application/octet-stream";
|
|
9
|
+
}
|
|
10
|
+
class FileNormalizer {
|
|
11
|
+
constructor(modifier){
|
|
12
|
+
this.modifier = modifier;
|
|
13
|
+
}
|
|
14
|
+
async normalizeFile(payload) {
|
|
15
|
+
const data = {
|
|
16
|
+
...payload,
|
|
17
|
+
id: payload.id || mdbid(),
|
|
18
|
+
type: payload.type || resolveTypeFromName(payload.name)
|
|
19
|
+
};
|
|
20
|
+
const key = new FileKey(data);
|
|
21
|
+
const defaultKey = key.toString();
|
|
22
|
+
const file = {
|
|
23
|
+
name: data.name,
|
|
24
|
+
type: data.type,
|
|
25
|
+
key: defaultKey,
|
|
26
|
+
size: data.size
|
|
27
|
+
};
|
|
28
|
+
if (this.modifier) Object.assign(file, await this.modifier(file));
|
|
29
|
+
if (defaultKey !== file.key) key.setKey(file.key);
|
|
30
|
+
return {
|
|
31
|
+
id: data.id,
|
|
32
|
+
key: key.toString(),
|
|
33
|
+
name: file.name,
|
|
34
|
+
size: file.size,
|
|
35
|
+
type: file.type
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export { FileNormalizer };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=FileNormalizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/FileNormalizer.js","sources":["../../src/utils/FileNormalizer.ts"],"sourcesContent":["import { mdbid } from \"@webiny/utils\";\nimport type { FileData, PresignedPostPayloadData } from \"~/types.js\";\nimport { FileKey } from \"~/utils/FileKey.js\";\nimport { mimeTypes } from \"~/utils/mimeTypes.js\";\nimport type { FileModifier } from \"./FileUploadModifier.js\";\n\nexport interface FileToSign {\n name: string;\n key: string;\n type: string;\n size: number;\n}\n\nconst extensionToMime = new Map<string, string>();\nfor (const [mime, extensions] of Object.entries(mimeTypes)) {\n for (const ext of extensions) {\n if (!extensionToMime.has(ext)) {\n extensionToMime.set(ext, mime);\n }\n }\n}\n\nfunction resolveTypeFromName(name: string): string {\n const ext = name.slice(name.lastIndexOf(\".\") + 1).toLowerCase();\n return extensionToMime.get(ext) || \"application/octet-stream\";\n}\n\n/**\n * FileNormalizer normalizes file data, before it's signed for upload to S3.\n * It generates a unique file id, and makes sure that the file key includes the unique id.\n *\n */\nexport class FileNormalizer {\n private readonly modifier: FileModifier | undefined;\n\n constructor(modifier?: FileModifier) {\n this.modifier = modifier;\n }\n\n async normalizeFile(payload: PresignedPostPayloadData): Promise<FileData> {\n const data = {\n ...payload,\n id: payload.id || mdbid(),\n type: payload.type || resolveTypeFromName(payload.name)\n };\n\n const key = new FileKey(data);\n const defaultKey = key.toString();\n\n const file: FileToSign = {\n name: data.name,\n type: data.type,\n key: defaultKey,\n size: data.size\n };\n\n if (this.modifier) {\n Object.assign(file, await this.modifier(file));\n }\n\n if (defaultKey !== file.key) {\n key.setKey(file.key);\n }\n\n return {\n id: data.id,\n key: key.toString(),\n name: file.name,\n size: file.size,\n type: file.type\n };\n }\n}\n"],"names":["extensionToMime","Map","mime","extensions","Object","mimeTypes","ext","resolveTypeFromName","name","FileNormalizer","modifier","payload","data","mdbid","key","FileKey","defaultKey","file"],"mappings":";;;AAaA,MAAMA,kBAAkB,IAAIC;AAC5B,KAAK,MAAM,CAACC,MAAMC,WAAW,IAAIC,OAAO,OAAO,CAACC,iCAC5C,KAAK,MAAMC,OAAOH,WACd,IAAI,CAACH,gBAAgB,GAAG,CAACM,MACrBN,gBAAgB,GAAG,CAACM,KAAKJ;AAKrC,SAASK,oBAAoBC,IAAY;IACrC,MAAMF,MAAME,KAAK,KAAK,CAACA,KAAK,WAAW,CAAC,OAAO,GAAG,WAAW;IAC7D,OAAOR,gBAAgB,GAAG,CAACM,QAAQ;AACvC;AAOO,MAAMG;IAGT,YAAYC,QAAuB,CAAE;QACjC,IAAI,CAAC,QAAQ,GAAGA;IACpB;IAEA,MAAM,cAAcC,OAAiC,EAAqB;QACtE,MAAMC,OAAO;YACT,GAAGD,OAAO;YACV,IAAIA,QAAQ,EAAE,IAAIE;YAClB,MAAMF,QAAQ,IAAI,IAAIJ,oBAAoBI,QAAQ,IAAI;QAC1D;QAEA,MAAMG,MAAM,IAAIC,QAAQH;QACxB,MAAMI,aAAaF,IAAI,QAAQ;QAE/B,MAAMG,OAAmB;YACrB,MAAML,KAAK,IAAI;YACf,MAAMA,KAAK,IAAI;YACf,KAAKI;YACL,MAAMJ,KAAK,IAAI;QACnB;QAEA,IAAI,IAAI,CAAC,QAAQ,EACbR,OAAO,MAAM,CAACa,MAAM,MAAM,IAAI,CAAC,QAAQ,CAACA;QAG5C,IAAID,eAAeC,KAAK,GAAG,EACvBH,IAAI,MAAM,CAACG,KAAK,GAAG;QAGvB,OAAO;YACH,IAAIL,KAAK,EAAE;YACX,KAAKE,IAAI,QAAQ;YACjB,MAAMG,KAAK,IAAI;YACf,MAAMA,KAAK,IAAI;YACf,MAAMA,KAAK,IAAI;QACnB;IACJ;AACJ"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Plugin } from "@webiny/plugins";
|
|
2
|
+
import type { FileToSign } from "../utils/FileNormalizer.js";
|
|
3
|
+
interface Setter<T> {
|
|
4
|
+
(param: T): T;
|
|
5
|
+
}
|
|
6
|
+
declare class FileUploadModifierBuilder {
|
|
7
|
+
private fileSetters;
|
|
8
|
+
setFile(setter: Setter<FileToSign>): void;
|
|
9
|
+
execute(file: FileToSign): Promise<FileToSign>;
|
|
10
|
+
}
|
|
11
|
+
interface FileUploadModifierCallbackParams {
|
|
12
|
+
modifier: {
|
|
13
|
+
setFile: FileUploadModifierBuilder["setFile"];
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export declare class FileUploadModifierPlugin extends Plugin {
|
|
17
|
+
static type: string;
|
|
18
|
+
private readonly cb;
|
|
19
|
+
constructor(cb: FileUploadModifierCallable);
|
|
20
|
+
execute(params: FileUploadModifierCallbackParams): void;
|
|
21
|
+
}
|
|
22
|
+
interface FileUploadModifierCallable {
|
|
23
|
+
(params: FileUploadModifierCallbackParams): void;
|
|
24
|
+
}
|
|
25
|
+
export declare const createFileUploadModifier: (cb: FileUploadModifierCallable) => FileUploadModifierPlugin;
|
|
26
|
+
/**
|
|
27
|
+
* DI multiple-abstraction for file-upload modifiers. Replaces the old
|
|
28
|
+
* `context.plugins.byType(FileUploadModifierPlugin.type)` lookup in createFileNormalizerFromContext.
|
|
29
|
+
* Extensions register modifiers via `container.registerInstance(FileUploadModifier, createFileUploadModifier(cb))`.
|
|
30
|
+
*/
|
|
31
|
+
export declare const FileUploadModifier: import("@webiny/di").Abstraction<FileUploadModifierPlugin>;
|
|
32
|
+
export interface FileModifier {
|
|
33
|
+
(file: FileToSign): Promise<Partial<FileToSign>> | Partial<FileToSign>;
|
|
34
|
+
}
|
|
35
|
+
export declare const createModifierFromPlugins: (plugins?: FileUploadModifierPlugin[]) => FileModifier;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import p_reduce from "p-reduce";
|
|
2
|
+
import { Plugin } from "@webiny/plugins";
|
|
3
|
+
import { createAbstraction } from "@webiny/feature/api";
|
|
4
|
+
class FileUploadModifierBuilder {
|
|
5
|
+
setFile(setter) {
|
|
6
|
+
this.fileSetters.push(setter);
|
|
7
|
+
}
|
|
8
|
+
execute(file) {
|
|
9
|
+
return p_reduce(this.fileSetters, async (file, setter)=>({
|
|
10
|
+
...file,
|
|
11
|
+
...await setter(file)
|
|
12
|
+
}), file);
|
|
13
|
+
}
|
|
14
|
+
constructor(){
|
|
15
|
+
this.fileSetters = [];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class FileUploadModifierPlugin extends Plugin {
|
|
19
|
+
static{
|
|
20
|
+
this.type = "fm.s3.uploadModifier";
|
|
21
|
+
}
|
|
22
|
+
constructor(cb){
|
|
23
|
+
super();
|
|
24
|
+
this.cb = cb;
|
|
25
|
+
}
|
|
26
|
+
execute(params) {
|
|
27
|
+
return this.cb(params);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const createFileUploadModifier = (cb)=>new FileUploadModifierPlugin(cb);
|
|
31
|
+
const FileUploadModifier = createAbstraction("FileUploadModifier");
|
|
32
|
+
const createModifierFromPlugins = (plugins = [])=>{
|
|
33
|
+
const modifier = new FileUploadModifierBuilder();
|
|
34
|
+
plugins.forEach((pl)=>pl.execute({
|
|
35
|
+
modifier
|
|
36
|
+
}));
|
|
37
|
+
return (file)=>modifier.execute(file);
|
|
38
|
+
};
|
|
39
|
+
export { FileUploadModifier, FileUploadModifierPlugin, createFileUploadModifier, createModifierFromPlugins };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=FileUploadModifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/FileUploadModifier.js","sources":["../../src/utils/FileUploadModifier.ts"],"sourcesContent":["import pReduce from \"p-reduce\";\nimport { Plugin } from \"@webiny/plugins\";\nimport { createAbstraction } from \"@webiny/feature/api\";\nimport type { FileToSign } from \"~/utils/FileNormalizer.js\";\n\ninterface Setter<T> {\n (param: T): T;\n}\n\nclass FileUploadModifierBuilder {\n private fileSetters: Setter<FileToSign>[] = [];\n\n setFile(setter: Setter<FileToSign>): void {\n this.fileSetters.push(setter);\n }\n\n execute(file: FileToSign) {\n return pReduce(\n this.fileSetters,\n async (file, setter) => {\n // We need to spread the original file, then add (potentially) partial changes.\n return { ...file, ...(await setter(file)) };\n },\n file\n );\n }\n}\n\ninterface FileUploadModifierCallbackParams {\n modifier: {\n setFile: FileUploadModifierBuilder[\"setFile\"];\n };\n}\n\nexport class FileUploadModifierPlugin extends Plugin {\n public static override type = \"fm.s3.uploadModifier\";\n private readonly cb: FileUploadModifierCallable;\n\n constructor(cb: FileUploadModifierCallable) {\n super();\n this.cb = cb;\n }\n\n execute(params: FileUploadModifierCallbackParams) {\n return this.cb(params);\n }\n}\n\ninterface FileUploadModifierCallable {\n (params: FileUploadModifierCallbackParams): void;\n}\n\nexport const createFileUploadModifier = (cb: FileUploadModifierCallable) => {\n return new FileUploadModifierPlugin(cb);\n};\n\n/**\n * DI multiple-abstraction for file-upload modifiers. Replaces the old\n * `context.plugins.byType(FileUploadModifierPlugin.type)` lookup in createFileNormalizerFromContext.\n * Extensions register modifiers via `container.registerInstance(FileUploadModifier, createFileUploadModifier(cb))`.\n */\nexport const FileUploadModifier = createAbstraction<FileUploadModifierPlugin>(\"FileUploadModifier\");\n\nexport interface FileModifier {\n (file: FileToSign): Promise<Partial<FileToSign>> | Partial<FileToSign>;\n}\n\nexport const createModifierFromPlugins = (\n plugins: FileUploadModifierPlugin[] = []\n): FileModifier => {\n const modifier = new FileUploadModifierBuilder();\n plugins.forEach(pl => pl.execute({ modifier }));\n\n return (file: FileToSign) => modifier.execute(file);\n};\n"],"names":["FileUploadModifierBuilder","setter","file","pReduce","FileUploadModifierPlugin","Plugin","cb","params","createFileUploadModifier","FileUploadModifier","createAbstraction","createModifierFromPlugins","plugins","modifier","pl"],"mappings":";;;AASA,MAAMA;IAGF,QAAQC,MAA0B,EAAQ;QACtC,IAAI,CAAC,WAAW,CAAC,IAAI,CAACA;IAC1B;IAEA,QAAQC,IAAgB,EAAE;QACtB,OAAOC,SACH,IAAI,CAAC,WAAW,EAChB,OAAOD,MAAMD,SAEF;gBAAE,GAAGC,IAAI;gBAAE,GAAI,MAAMD,OAAOC,KAAK;YAAE,IAE9CA;IAER;;aAfQ,WAAW,GAAyB,EAAE;;AAgBlD;AAQO,MAAME,iCAAiCC;;aACnB,IAAI,GAAG;;IAG9B,YAAYC,EAA8B,CAAE;QACxC,KAAK;QACL,IAAI,CAAC,EAAE,GAAGA;IACd;IAEA,QAAQC,MAAwC,EAAE;QAC9C,OAAO,IAAI,CAAC,EAAE,CAACA;IACnB;AACJ;AAMO,MAAMC,2BAA2B,CAACF,KAC9B,IAAIF,yBAAyBE;AAQjC,MAAMG,qBAAqBC,kBAA4C;AAMvE,MAAMC,4BAA4B,CACrCC,UAAsC,EAAE;IAExC,MAAMC,WAAW,IAAIb;IACrBY,QAAQ,OAAO,CAACE,CAAAA,KAAMA,GAAG,OAAO,CAAC;YAAED;QAAS;IAE5C,OAAO,CAACX,OAAqBW,SAAS,OAAO,CAACX;AAClD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FileNormalizer } from "./FileNormalizer.js";
|
|
2
|
+
import { FileUploadModifier, createModifierFromPlugins } from "./FileUploadModifier.js";
|
|
3
|
+
const createFileNormalizerFromContext = (context)=>{
|
|
4
|
+
const modifierPlugins = context.container.resolveAll(FileUploadModifier);
|
|
5
|
+
return new FileNormalizer(createModifierFromPlugins(modifierPlugins));
|
|
6
|
+
};
|
|
7
|
+
export { createFileNormalizerFromContext };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=createFileNormalizerFromContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/createFileNormalizerFromContext.js","sources":["../../src/utils/createFileNormalizerFromContext.ts"],"sourcesContent":["import type { Context } from \"@webiny/api/types.js\";\nimport { FileNormalizer } from \"~/utils/FileNormalizer.js\";\nimport { createModifierFromPlugins, FileUploadModifier } from \"~/utils/FileUploadModifier.js\";\n\nexport const createFileNormalizerFromContext = (context: Context) => {\n // Modifiers are registered as DI instances (FileUploadModifier) — was\n // context.plugins.byType(FileUploadModifierPlugin.type).\n const modifierPlugins = context.container.resolveAll(FileUploadModifier);\n\n return new FileNormalizer(createModifierFromPlugins(modifierPlugins));\n};\n"],"names":["createFileNormalizerFromContext","context","modifierPlugins","FileUploadModifier","FileNormalizer","createModifierFromPlugins"],"mappings":";;AAIO,MAAMA,kCAAkC,CAACC;IAG5C,MAAMC,kBAAkBD,QAAQ,SAAS,CAAC,UAAU,CAACE;IAErD,OAAO,IAAIC,eAAeC,0BAA0BH;AACxD"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FileData, PresignedPostPayloadDataResponse } from "../types.js";
|
|
2
|
+
import type { FileManagerSettings } from "@webiny/api-file-manager/domain/settings/types.js";
|
|
3
|
+
import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
|
|
4
|
+
export declare const getPresignedPostPayload: (file: FileData, settings: FileManagerSettings, tenantContext: TenantContext.Interface) => Promise<PresignedPostPayloadDataResponse>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { S3Client, createPresignedPost } from "@webiny/aws-sdk/client-s3/index.js";
|
|
2
|
+
import { validation } from "@webiny/validation";
|
|
3
|
+
const S3_BUCKET = process.env.S3_BUCKET;
|
|
4
|
+
const UPLOAD_MAX_FILE_SIZE_DEFAULT = 1099511627776;
|
|
5
|
+
const sanitizeFileSizeValue = (value, defaultValue)=>{
|
|
6
|
+
try {
|
|
7
|
+
validation.validateSync(value, "required,numeric,gte:0");
|
|
8
|
+
return value;
|
|
9
|
+
} catch {
|
|
10
|
+
return defaultValue;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const getPresignedPostPayload = async (file, settings, tenantContext)=>{
|
|
14
|
+
const uploadMinFileSize = sanitizeFileSizeValue(settings.uploadMinFileSize, 0);
|
|
15
|
+
const uploadMaxFileSize = sanitizeFileSizeValue(settings.uploadMaxFileSize, UPLOAD_MAX_FILE_SIZE_DEFAULT);
|
|
16
|
+
const tenant = tenantContext.getTenant();
|
|
17
|
+
const storageKey = `tenants/${tenant.id}/files/${file.key}`;
|
|
18
|
+
const params = {
|
|
19
|
+
Key: storageKey,
|
|
20
|
+
Expires: 60,
|
|
21
|
+
Bucket: S3_BUCKET,
|
|
22
|
+
Conditions: [
|
|
23
|
+
[
|
|
24
|
+
"content-length-range",
|
|
25
|
+
uploadMinFileSize,
|
|
26
|
+
uploadMaxFileSize
|
|
27
|
+
]
|
|
28
|
+
],
|
|
29
|
+
Fields: {
|
|
30
|
+
"Content-Type": file.type
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
if (params.Key.startsWith("/")) params.Key = params.Key.slice(1);
|
|
34
|
+
const s3 = new S3Client();
|
|
35
|
+
const payload = await createPresignedPost(s3, params);
|
|
36
|
+
return {
|
|
37
|
+
data: payload,
|
|
38
|
+
file
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export { getPresignedPostPayload };
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=getPresignedPostPayload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/getPresignedPostPayload.js","sources":["../../src/utils/getPresignedPostPayload.ts"],"sourcesContent":["import type { PresignedPostOptions } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { S3Client, createPresignedPost } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { validation } from \"@webiny/validation\";\nimport type { FileData, PresignedPostPayloadDataResponse } from \"~/types.js\";\nimport type { FileManagerSettings } from \"@webiny/api-file-manager/domain/settings/types.js\";\nimport { TenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/index.js\";\n\nconst S3_BUCKET = process.env.S3_BUCKET;\nconst UPLOAD_MAX_FILE_SIZE_DEFAULT = 1099511627776; // 1TB\n\nconst sanitizeFileSizeValue = (value: number, defaultValue: number): number => {\n try {\n validation.validateSync(value, \"required,numeric,gte:0\");\n return value;\n } catch {\n // TODO @ts-refactor No need to log the error?\n return defaultValue;\n }\n};\n\nexport const getPresignedPostPayload = async (\n file: FileData,\n settings: FileManagerSettings,\n tenantContext: TenantContext.Interface\n): Promise<PresignedPostPayloadDataResponse> => {\n const uploadMinFileSize = sanitizeFileSizeValue(settings.uploadMinFileSize, 0);\n const uploadMaxFileSize = sanitizeFileSizeValue(\n settings.uploadMaxFileSize,\n UPLOAD_MAX_FILE_SIZE_DEFAULT\n );\n\n const tenant = tenantContext.getTenant();\n const storageKey = `tenants/${tenant.id}/files/${file.key}`;\n\n const params = {\n Key: storageKey,\n Expires: 60,\n Bucket: S3_BUCKET as string,\n Conditions: [\n [\"content-length-range\", uploadMinFileSize, uploadMaxFileSize]\n ] as PresignedPostOptions[\"Conditions\"],\n Fields: {\n \"Content-Type\": file.type\n }\n };\n\n if (params.Key.startsWith(\"/\")) {\n params.Key = params.Key.slice(1);\n }\n\n const s3 = new S3Client();\n const payload = await createPresignedPost(s3, params);\n\n return {\n data: payload,\n file\n };\n};\n"],"names":["S3_BUCKET","process","UPLOAD_MAX_FILE_SIZE_DEFAULT","sanitizeFileSizeValue","value","defaultValue","validation","getPresignedPostPayload","file","settings","tenantContext","uploadMinFileSize","uploadMaxFileSize","tenant","storageKey","params","s3","S3Client","payload","createPresignedPost"],"mappings":";;AAOA,MAAMA,YAAYC,QAAQ,GAAG,CAAC,SAAS;AACvC,MAAMC,+BAA+B;AAErC,MAAMC,wBAAwB,CAACC,OAAeC;IAC1C,IAAI;QACAC,WAAW,YAAY,CAACF,OAAO;QAC/B,OAAOA;IACX,EAAE,OAAM;QAEJ,OAAOC;IACX;AACJ;AAEO,MAAME,0BAA0B,OACnCC,MACAC,UACAC;IAEA,MAAMC,oBAAoBR,sBAAsBM,SAAS,iBAAiB,EAAE;IAC5E,MAAMG,oBAAoBT,sBACtBM,SAAS,iBAAiB,EAC1BP;IAGJ,MAAMW,SAASH,cAAc,SAAS;IACtC,MAAMI,aAAa,CAAC,QAAQ,EAAED,OAAO,EAAE,CAAC,OAAO,EAAEL,KAAK,GAAG,EAAE;IAE3D,MAAMO,SAAS;QACX,KAAKD;QACL,SAAS;QACT,QAAQd;QACR,YAAY;YACR;gBAAC;gBAAwBW;gBAAmBC;aAAkB;SACjE;QACD,QAAQ;YACJ,gBAAgBJ,KAAK,IAAI;QAC7B;IACJ;IAEA,IAAIO,OAAO,GAAG,CAAC,UAAU,CAAC,MACtBA,OAAO,GAAG,GAAGA,OAAO,GAAG,CAAC,KAAK,CAAC;IAGlC,MAAMC,KAAK,IAAIC;IACf,MAAMC,UAAU,MAAMC,oBAAoBH,IAAID;IAE9C,OAAO;QACH,MAAMG;QACNV;IACJ;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/mimeTypes.js","sources":["../../src/utils/mimeTypes.ts"],"sourcesContent":["import vendorTypes from \"mime/types/other.js\";\nimport standardTypes from \"mime/types/standard.js\";\n\n/**\n * We need to get all extensions that are defined for a specific content type.\n * This operation is not available via the `mime` package, so we create such an object ourselves.\n */\nexport const mimeTypes: Record<string, string[]> = {\n ...vendorTypes,\n ...standardTypes\n};\n"],"names":["mimeTypes","vendorTypes","standardTypes"],"mappings":";;AAOO,MAAMA,sBAAsC;IAC/C,GAAGC,KAAW;IACd,GAAGC,QAAa;AACpB"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { CompleteMultiPartUploadUseCase } from "@webiny/api-file-manager/features/upload/CompleteMultiPartUpload/index.js";
|
|
2
|
-
declare class CompleteMultiPartUploadUseCaseImpl implements CompleteMultiPartUploadUseCase.Interface {
|
|
3
|
-
execute(params: CompleteMultiPartUploadUseCase.Params): Promise<void>;
|
|
4
|
-
}
|
|
5
|
-
export declare const CompleteMultiPartUploadUseCaseImplementation: typeof CompleteMultiPartUploadUseCaseImpl & {
|
|
6
|
-
__abstraction: import("@webiny/di").Abstraction<import("@webiny/api-file-manager/features/upload/CompleteMultiPartUpload/abstractions").ICompleteMultiPartUploadUseCase>;
|
|
7
|
-
};
|
|
8
|
-
export {};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { CompleteMultipartUploadCommand, ListPartsCommand, S3 } from "@webiny/aws-sdk/client-s3/index.js";
|
|
2
|
-
import { CompleteMultiPartUploadUseCase } from "@webiny/api-file-manager/features/upload/CompleteMultiPartUpload/index.js";
|
|
3
|
-
const EMPTY_MARKER_VALUES = [
|
|
4
|
-
void 0,
|
|
5
|
-
"0"
|
|
6
|
-
];
|
|
7
|
-
class CompleteMultiPartUploadUseCaseImpl {
|
|
8
|
-
async execute(params) {
|
|
9
|
-
const bucket = String(process.env.S3_BUCKET);
|
|
10
|
-
const s3Client = new S3({
|
|
11
|
-
region: process.env.AWS_REGION
|
|
12
|
-
});
|
|
13
|
-
const uploadParams = {
|
|
14
|
-
Bucket: bucket,
|
|
15
|
-
Key: params.fileKey,
|
|
16
|
-
UploadId: params.uploadId
|
|
17
|
-
};
|
|
18
|
-
const allParts = await getAllUploadParts(s3Client, uploadParams);
|
|
19
|
-
const command = new CompleteMultipartUploadCommand({
|
|
20
|
-
...uploadParams,
|
|
21
|
-
MultipartUpload: {
|
|
22
|
-
Parts: allParts
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
await s3Client.send(command);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async function getAllUploadParts(s3Client, params) {
|
|
29
|
-
const parts = [];
|
|
30
|
-
let marker;
|
|
31
|
-
while(true){
|
|
32
|
-
const { Parts, PartNumberMarker } = await s3Client.send(new ListPartsCommand({
|
|
33
|
-
...params,
|
|
34
|
-
PartNumberMarker: marker
|
|
35
|
-
}));
|
|
36
|
-
if (Parts) Parts.forEach((part)=>parts.push(part));
|
|
37
|
-
marker = PartNumberMarker || void 0;
|
|
38
|
-
if (EMPTY_MARKER_VALUES.includes(marker)) break;
|
|
39
|
-
}
|
|
40
|
-
return parts.map((part)=>({
|
|
41
|
-
ETag: part.ETag,
|
|
42
|
-
PartNumber: part.PartNumber
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
const CompleteMultiPartUploadUseCaseImplementation = CompleteMultiPartUploadUseCase.createImplementation({
|
|
46
|
-
implementation: CompleteMultiPartUploadUseCaseImpl,
|
|
47
|
-
dependencies: []
|
|
48
|
-
});
|
|
49
|
-
export { CompleteMultiPartUploadUseCaseImplementation };
|
|
50
|
-
|
|
51
|
-
//# sourceMappingURL=CompleteMultiPartUploadUseCase.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"features/CompleteMultiPartUpload/CompleteMultiPartUploadUseCase.js","sources":["../../../src/features/CompleteMultiPartUpload/CompleteMultiPartUploadUseCase.ts"],"sourcesContent":["import type { Part } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport type { ListPartsOutput } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { S3 } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { ListPartsCommand } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { CompleteMultipartUploadCommand } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { CompleteMultiPartUploadUseCase } from \"@webiny/api-file-manager/features/upload/CompleteMultiPartUpload/index.js\";\n\nconst EMPTY_MARKER_VALUES = [undefined, \"0\"];\n\nclass CompleteMultiPartUploadUseCaseImpl implements CompleteMultiPartUploadUseCase.Interface {\n public async execute(params: CompleteMultiPartUploadUseCase.Params): Promise<void> {\n const bucket = String(process.env.S3_BUCKET);\n const s3Client = new S3({ region: process.env.AWS_REGION });\n\n const uploadParams = {\n Bucket: bucket,\n Key: params.fileKey,\n UploadId: params.uploadId\n };\n\n const allParts = await getAllUploadParts(s3Client, uploadParams);\n\n const command = new CompleteMultipartUploadCommand({\n ...uploadParams,\n MultipartUpload: { Parts: allParts }\n });\n\n await s3Client.send(command);\n }\n}\n\ninterface GetAllUploadPartsParams {\n Bucket: string;\n Key: string;\n UploadId: string;\n}\n\nasync function getAllUploadParts(s3Client: S3, params: GetAllUploadPartsParams) {\n const parts: Part[] = [];\n\n let marker: string | undefined = undefined;\n while (true) {\n const { Parts, PartNumberMarker }: ListPartsOutput = await s3Client.send(\n new ListPartsCommand({\n ...params,\n PartNumberMarker: marker\n })\n );\n\n if (Parts) {\n Parts.forEach(part => parts.push(part));\n }\n\n marker = PartNumberMarker || undefined;\n if (EMPTY_MARKER_VALUES.includes(marker)) {\n break;\n }\n }\n\n return parts.map(part => ({\n ETag: part.ETag as string,\n PartNumber: part.PartNumber as number\n }));\n}\n\nexport const CompleteMultiPartUploadUseCaseImplementation =\n CompleteMultiPartUploadUseCase.createImplementation({\n implementation: CompleteMultiPartUploadUseCaseImpl,\n dependencies: []\n });\n"],"names":["EMPTY_MARKER_VALUES","undefined","CompleteMultiPartUploadUseCaseImpl","params","bucket","String","process","s3Client","S3","uploadParams","allParts","getAllUploadParts","command","CompleteMultipartUploadCommand","parts","marker","Parts","PartNumberMarker","ListPartsCommand","part","CompleteMultiPartUploadUseCaseImplementation","CompleteMultiPartUploadUseCase"],"mappings":";;AAOA,MAAMA,sBAAsB;IAACC;IAAW;CAAI;AAE5C,MAAMC;IACF,MAAa,QAAQC,MAA6C,EAAiB;QAC/E,MAAMC,SAASC,OAAOC,QAAQ,GAAG,CAAC,SAAS;QAC3C,MAAMC,WAAW,IAAIC,GAAG;YAAE,QAAQF,QAAQ,GAAG,CAAC,UAAU;QAAC;QAEzD,MAAMG,eAAe;YACjB,QAAQL;YACR,KAAKD,OAAO,OAAO;YACnB,UAAUA,OAAO,QAAQ;QAC7B;QAEA,MAAMO,WAAW,MAAMC,kBAAkBJ,UAAUE;QAEnD,MAAMG,UAAU,IAAIC,+BAA+B;YAC/C,GAAGJ,YAAY;YACf,iBAAiB;gBAAE,OAAOC;YAAS;QACvC;QAEA,MAAMH,SAAS,IAAI,CAACK;IACxB;AACJ;AAQA,eAAeD,kBAAkBJ,QAAY,EAAEJ,MAA+B;IAC1E,MAAMW,QAAgB,EAAE;IAExB,IAAIC;IACJ,MAAO,KAAM;QACT,MAAM,EAAEC,KAAK,EAAEC,gBAAgB,EAAE,GAAoB,MAAMV,SAAS,IAAI,CACpE,IAAIW,iBAAiB;YACjB,GAAGf,MAAM;YACT,kBAAkBY;QACtB;QAGJ,IAAIC,OACAA,MAAM,OAAO,CAACG,CAAAA,OAAQL,MAAM,IAAI,CAACK;QAGrCJ,SAASE,oBAAoBhB;QAC7B,IAAID,oBAAoB,QAAQ,CAACe,SAC7B;IAER;IAEA,OAAOD,MAAM,GAAG,CAACK,CAAAA,OAAS;YACtB,MAAMA,KAAK,IAAI;YACf,YAAYA,KAAK,UAAU;QAC/B;AACJ;AAEO,MAAMC,+CACTC,+BAA+B,oBAAoB,CAAC;IAChD,gBAAgBnB;IAChB,cAAc,EAAE;AACpB"}
|