@webiny/api-file-manager-s3 6.6.0-alpha.11 → 6.6.0-alpha.13
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.js +2 -0
- package/FileManagerS3Feature.js.map +1 -1
- package/assetDelivery/s3/S3ContentsReader.d.ts +1 -0
- package/assetDelivery/s3/S3ContentsReader.js +5 -0
- package/assetDelivery/s3/S3ContentsReader.js.map +1 -1
- package/assetDelivery/s3/transformation/CallableContentsReader.d.ts +1 -0
- package/assetDelivery/s3/transformation/CallableContentsReader.js +5 -0
- package/assetDelivery/s3/transformation/CallableContentsReader.js.map +1 -1
- package/assetDelivery/threatDetection/createThreatDetectionEventHandler.d.ts +8 -8
- package/assetDelivery/threatDetection/createThreatDetectionEventHandler.js +24 -13
- package/assetDelivery/threatDetection/createThreatDetectionEventHandler.js.map +1 -1
- package/assetDelivery/threatDetection/processThreatScanResult.d.ts +1 -1
- package/assetDelivery/threatDetection/processThreatScanResult.js +5 -3
- package/assetDelivery/threatDetection/processThreatScanResult.js.map +1 -1
- package/features/DeleteFileFromBucket/DeleteFileFromBucketHandler.js +4 -1
- package/features/DeleteFileFromBucket/DeleteFileFromBucketHandler.js.map +1 -1
- package/features/DeleteFileFromBucket/DeleteS3FolderTask.d.ts +3 -0
- package/features/DeleteFileFromBucket/DeleteS3FolderTask.js +8 -3
- package/features/DeleteFileFromBucket/DeleteS3FolderTask.js.map +1 -1
- package/features/FileContentsStorage/S3FileContentsStorage.d.ts +17 -0
- package/features/FileContentsStorage/S3FileContentsStorage.js +60 -0
- package/features/FileContentsStorage/S3FileContentsStorage.js.map +1 -0
- package/features/FileContentsStorage/feature.d.ts +5 -0
- package/features/FileContentsStorage/feature.js +11 -0
- package/features/FileContentsStorage/feature.js.map +1 -0
- package/features/GetFileContentsById/GetFileContentsByIdUseCase.d.ts +6 -1
- package/features/GetFileContentsById/GetFileContentsByIdUseCase.js +13 -8
- package/features/GetFileContentsById/GetFileContentsByIdUseCase.js.map +1 -1
- package/features/GetFileContentsByKey/GetFileContentsByKeyUseCase.d.ts +4 -1
- package/features/GetFileContentsByKey/GetFileContentsByKeyUseCase.js +11 -8
- package/features/GetFileContentsByKey/GetFileContentsByKeyUseCase.js.map +1 -1
- package/features/WriteFileMetadata/MetadataWriter.js +3 -0
- package/features/WriteFileMetadata/MetadataWriter.js.map +1 -1
- package/graphql/S3GraphQLSchema.js +17 -8
- package/graphql/S3GraphQLSchema.js.map +1 -1
- package/index.d.ts +2 -1
- package/index.js +1 -1
- package/multiPartUpload/CompleteMultiPartUploadUseCase.d.ts +1 -0
- package/multiPartUpload/CompleteMultiPartUploadUseCase.js +2 -1
- package/multiPartUpload/CompleteMultiPartUploadUseCase.js.map +1 -1
- package/multiPartUpload/CreateMultiPartUploadUseCase.d.ts +6 -1
- package/multiPartUpload/CreateMultiPartUploadUseCase.js +11 -2
- package/multiPartUpload/CreateMultiPartUploadUseCase.js.map +1 -1
- package/package.json +18 -22
- package/utils/FileNormalizer.test.js +86 -0
- package/utils/FileNormalizer.test.js.map +1 -0
- package/utils/createFileNormalizerFromContext.d.ts +1 -1
- package/utils/createFileNormalizerFromContext.js +2 -2
- package/utils/createFileNormalizerFromContext.js.map +1 -1
- package/utils/ensureNewFileId.d.ts +13 -0
- package/utils/ensureNewFileId.js +26 -0
- package/utils/ensureNewFileId.js.map +1 -0
- package/utils/getFileStorageKey.d.ts +5 -0
- package/utils/getFileStorageKey.js +4 -0
- package/utils/getFileStorageKey.js.map +1 -0
- package/utils/getPresignedPostPayload.d.ts +6 -1
- package/utils/getPresignedPostPayload.js +12 -4
- package/utils/getPresignedPostPayload.js.map +1 -1
- package/utils/FileExtension.d.ts +0 -6
- package/utils/FileExtension.js +0 -16
- package/utils/FileExtension.js.map +0 -1
- package/utils/FileKey.d.ts +0 -11
- package/utils/FileKey.js +0 -33
- package/utils/FileKey.js.map +0 -1
- package/utils/FileKey.test.js +0 -59
- package/utils/FileKey.test.js.map +0 -1
- package/utils/FileNormalizer.d.ts +0 -18
- package/utils/FileNormalizer.js +0 -48
- package/utils/FileNormalizer.js.map +0 -1
- package/utils/FileUploadModifier.d.ts +0 -36
- package/utils/FileUploadModifier.js +0 -41
- package/utils/FileUploadModifier.js.map +0 -1
- package/utils/mimeTypes.d.ts +0 -5
- package/utils/mimeTypes.js +0 -9
- package/utils/mimeTypes.js.map +0 -1
- /package/utils/{FileKey.test.d.ts → FileNormalizer.test.d.ts} +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BaseError } from "@webiny/feature/api";
|
|
2
|
+
import { getFileStorageKey } from "./getFileStorageKey.js";
|
|
3
|
+
class FileAlreadyExistsError extends BaseError {
|
|
4
|
+
constructor(id){
|
|
5
|
+
super({
|
|
6
|
+
message: `A file with id "${id}" already exists.`,
|
|
7
|
+
data: {
|
|
8
|
+
id
|
|
9
|
+
}
|
|
10
|
+
}), this.code = "FILE_ALREADY_EXISTS";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const ensureNewFileId = async (params)=>{
|
|
14
|
+
const { s3, bucket, file, tenantId, clientId } = params;
|
|
15
|
+
if (!clientId) return;
|
|
16
|
+
const folder = file.key.slice(0, file.key.lastIndexOf("/") + 1);
|
|
17
|
+
const result = await s3.listObjectsV2({
|
|
18
|
+
Bucket: bucket,
|
|
19
|
+
Prefix: getFileStorageKey(tenantId, folder),
|
|
20
|
+
MaxKeys: 1
|
|
21
|
+
});
|
|
22
|
+
if (result.KeyCount) throw new FileAlreadyExistsError(clientId);
|
|
23
|
+
};
|
|
24
|
+
export { ensureNewFileId };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=ensureNewFileId.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/ensureNewFileId.js","sources":["../../src/utils/ensureNewFileId.ts"],"sourcesContent":["import type { S3 } from \"@webiny/aws-sdk/client-s3/index.js\";\nimport { BaseError } from \"@webiny/feature/api\";\nimport type { FileData } from \"@webiny/api-file-manager/features/upload/types.js\";\nimport { getFileStorageKey } from \"./getFileStorageKey.js\";\n\nclass FileAlreadyExistsError extends BaseError<{ id: string }> {\n override readonly code = \"FILE_ALREADY_EXISTS\" as const;\n\n constructor(id: string) {\n super({ message: `A file with id \"${id}\" already exists.`, data: { id } });\n }\n}\n\n/**\n * The client may choose the file id. Uploading with an id that is already used would overwrite\n * the existing file, so refuse when anything is stored under that id's folder.\n */\nexport const ensureNewFileId = async (params: {\n s3: S3;\n bucket: string;\n file: FileData;\n tenantId: string;\n clientId: string | undefined;\n}) => {\n const { s3, bucket, file, tenantId, clientId } = params;\n if (!clientId) {\n // Server-generated ids are unique.\n return;\n }\n\n const folder = file.key.slice(0, file.key.lastIndexOf(\"/\") + 1);\n const result = await s3.listObjectsV2({\n Bucket: bucket,\n Prefix: getFileStorageKey(tenantId, folder),\n MaxKeys: 1\n });\n\n if (result.KeyCount) {\n throw new FileAlreadyExistsError(clientId);\n }\n};\n"],"names":["FileAlreadyExistsError","BaseError","id","ensureNewFileId","params","s3","bucket","file","tenantId","clientId","folder","result","getFileStorageKey"],"mappings":";;AAKA,MAAMA,+BAA+BC;IAGjC,YAAYC,EAAU,CAAE;QACpB,KAAK,CAAC;YAAE,SAAS,CAAC,gBAAgB,EAAEA,GAAG,iBAAiB,CAAC;YAAE,MAAM;gBAAEA;YAAG;QAAE,SAH1D,IAAI,GAAG;IAIzB;AACJ;AAMO,MAAMC,kBAAkB,OAAOC;IAOlC,MAAM,EAAEC,EAAE,EAAEC,MAAM,EAAEC,IAAI,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,GAAGL;IACjD,IAAI,CAACK,UAED;IAGJ,MAAMC,SAASH,KAAK,GAAG,CAAC,KAAK,CAAC,GAAGA,KAAK,GAAG,CAAC,WAAW,CAAC,OAAO;IAC7D,MAAMI,SAAS,MAAMN,GAAG,aAAa,CAAC;QAClC,QAAQC;QACR,QAAQM,kBAAkBJ,UAAUE;QACpC,SAAS;IACb;IAEA,IAAIC,OAAO,QAAQ,EACf,MAAM,IAAIX,uBAAuBS;AAEzC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/getFileStorageKey.js","sources":["../../src/utils/getFileStorageKey.ts"],"sourcesContent":["/**\n * The S3 key of a file: every file lives under its tenant's folder. The file `key` (which may\n * include a client-provided `keyPrefix`) is always relative to that folder.\n */\nexport const getFileStorageKey = (tenantId: string, fileKey: string) => {\n return `tenants/${tenantId}/files/${fileKey.replace(/^\\/+/, \"\")}`;\n};\n"],"names":["getFileStorageKey","tenantId","fileKey"],"mappings":"AAIO,MAAMA,oBAAoB,CAACC,UAAkBC,UACzC,CAAC,QAAQ,EAAED,SAAS,OAAO,EAAEC,QAAQ,OAAO,CAAC,QAAQ,KAAK"}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { FileData, PresignedPostPayloadDataResponse } from "../types.js";
|
|
2
2
|
import type { FileManagerSettings } from "@webiny/api-file-manager/domain/settings/types.js";
|
|
3
3
|
import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
|
|
4
|
-
export declare const getPresignedPostPayload: (file: FileData, settings: FileManagerSettings, tenantContext: TenantContext.Interface
|
|
4
|
+
export declare const getPresignedPostPayload: (file: FileData, settings: FileManagerSettings, tenantContext: TenantContext.Interface, options?: {
|
|
5
|
+
/**
|
|
6
|
+
* The file id, when it was provided by the client (not generated on the server).
|
|
7
|
+
*/
|
|
8
|
+
clientId?: string;
|
|
9
|
+
}) => Promise<PresignedPostPayloadDataResponse>;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { S3Client, createPresignedPost } from "@webiny/aws-sdk/client-s3/index.js";
|
|
1
|
+
import { S3Client, createPresignedPost, createS3 } from "@webiny/aws-sdk/client-s3/index.js";
|
|
2
2
|
import { validation } from "@webiny/validation";
|
|
3
|
+
import { getFileStorageKey } from "./getFileStorageKey.js";
|
|
4
|
+
import { ensureNewFileId } from "./ensureNewFileId.js";
|
|
3
5
|
const S3_BUCKET = process.env.S3_BUCKET;
|
|
4
6
|
const UPLOAD_MAX_FILE_SIZE_DEFAULT = 1099511627776;
|
|
5
7
|
const sanitizeFileSizeValue = (value, defaultValue)=>{
|
|
@@ -10,11 +12,18 @@ const sanitizeFileSizeValue = (value, defaultValue)=>{
|
|
|
10
12
|
return defaultValue;
|
|
11
13
|
}
|
|
12
14
|
};
|
|
13
|
-
const getPresignedPostPayload = async (file, settings, tenantContext)=>{
|
|
15
|
+
const getPresignedPostPayload = async (file, settings, tenantContext, options = {})=>{
|
|
14
16
|
const uploadMinFileSize = sanitizeFileSizeValue(settings.uploadMinFileSize, 0);
|
|
15
17
|
const uploadMaxFileSize = sanitizeFileSizeValue(settings.uploadMaxFileSize, UPLOAD_MAX_FILE_SIZE_DEFAULT);
|
|
16
18
|
const tenant = tenantContext.getTenant();
|
|
17
|
-
|
|
19
|
+
await ensureNewFileId({
|
|
20
|
+
s3: createS3(),
|
|
21
|
+
bucket: S3_BUCKET,
|
|
22
|
+
file,
|
|
23
|
+
tenantId: tenant.id,
|
|
24
|
+
clientId: options.clientId
|
|
25
|
+
});
|
|
26
|
+
const storageKey = getFileStorageKey(tenant.id, file.key);
|
|
18
27
|
const params = {
|
|
19
28
|
Key: storageKey,
|
|
20
29
|
Expires: 60,
|
|
@@ -30,7 +39,6 @@ const getPresignedPostPayload = async (file, settings, tenantContext)=>{
|
|
|
30
39
|
"Content-Type": file.type
|
|
31
40
|
}
|
|
32
41
|
};
|
|
33
|
-
if (params.Key.startsWith("/")) params.Key = params.Key.slice(1);
|
|
34
42
|
const s3 = new S3Client();
|
|
35
43
|
const payload = await createPresignedPost(s3, params);
|
|
36
44
|
return {
|
|
@@ -1 +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 =
|
|
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, createS3 } 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\";\nimport { getFileStorageKey } from \"./getFileStorageKey.js\";\nimport { ensureNewFileId } from \"./ensureNewFileId.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 options: {\n /**\n * The file id, when it was provided by the client (not generated on the server).\n */\n clientId?: string;\n } = {}\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 await ensureNewFileId({\n s3: createS3(),\n bucket: S3_BUCKET as string,\n file,\n tenantId: tenant.id,\n clientId: options.clientId\n });\n\n const storageKey = getFileStorageKey(tenant.id, 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 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","options","uploadMinFileSize","uploadMaxFileSize","tenant","ensureNewFileId","createS3","storageKey","getFileStorageKey","params","s3","S3Client","payload","createPresignedPost"],"mappings":";;;;AASA,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,eACAC,UAKI,CAAC,CAAC;IAEN,MAAMC,oBAAoBT,sBAAsBM,SAAS,iBAAiB,EAAE;IAC5E,MAAMI,oBAAoBV,sBACtBM,SAAS,iBAAiB,EAC1BP;IAGJ,MAAMY,SAASJ,cAAc,SAAS;IACtC,MAAMK,gBAAgB;QAClB,IAAIC;QACJ,QAAQhB;QACRQ;QACA,UAAUM,OAAO,EAAE;QACnB,UAAUH,QAAQ,QAAQ;IAC9B;IAEA,MAAMM,aAAaC,kBAAkBJ,OAAO,EAAE,EAAEN,KAAK,GAAG;IAExD,MAAMW,SAAS;QACX,KAAKF;QACL,SAAS;QACT,QAAQjB;QACR,YAAY;YACR;gBAAC;gBAAwBY;gBAAmBC;aAAkB;SACjE;QACD,QAAQ;YACJ,gBAAgBL,KAAK,IAAI;QAC7B;IACJ;IAEA,MAAMY,KAAK,IAAIC;IACf,MAAMC,UAAU,MAAMC,oBAAoBH,IAAID;IAE9C,OAAO;QACH,MAAMG;QACNd;IACJ;AACJ"}
|
package/utils/FileExtension.d.ts
DELETED
package/utils/FileExtension.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/utils/FileKey.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
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
|
package/utils/FileKey.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/utils/FileKey.test.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
}
|
package/utils/FileNormalizer.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { mdbid } from "@webiny/utils";
|
|
2
|
-
import { FileKey } from "./FileKey.js";
|
|
3
|
-
import { validateFileKey } from "@webiny/api-file-manager/domain/file/validateFileKey.js";
|
|
4
|
-
import { mimeTypes as external_mimeTypes_js_mimeTypes } from "./mimeTypes.js";
|
|
5
|
-
const extensionToMime = new Map();
|
|
6
|
-
for (const [mime, extensions] of Object.entries(external_mimeTypes_js_mimeTypes))for (const ext of extensions)if (!extensionToMime.has(ext)) extensionToMime.set(ext, mime);
|
|
7
|
-
function resolveTypeFromName(name) {
|
|
8
|
-
const ext = name.slice(name.lastIndexOf(".") + 1).toLowerCase();
|
|
9
|
-
return extensionToMime.get(ext) || "application/octet-stream";
|
|
10
|
-
}
|
|
11
|
-
class FileNormalizer {
|
|
12
|
-
constructor(modifier){
|
|
13
|
-
this.modifier = modifier;
|
|
14
|
-
}
|
|
15
|
-
async normalizeFile(payload) {
|
|
16
|
-
const data = {
|
|
17
|
-
...payload,
|
|
18
|
-
id: payload.id || mdbid(),
|
|
19
|
-
type: payload.type || resolveTypeFromName(payload.name)
|
|
20
|
-
};
|
|
21
|
-
const key = new FileKey(data);
|
|
22
|
-
const defaultKey = key.toString();
|
|
23
|
-
const file = {
|
|
24
|
-
name: data.name,
|
|
25
|
-
type: data.type,
|
|
26
|
-
key: defaultKey,
|
|
27
|
-
size: data.size
|
|
28
|
-
};
|
|
29
|
-
if (this.modifier) Object.assign(file, await this.modifier(file));
|
|
30
|
-
if (defaultKey !== file.key) key.setKey(file.key);
|
|
31
|
-
const fileKey = key.toString();
|
|
32
|
-
const keyResult = validateFileKey({
|
|
33
|
-
key: fileKey,
|
|
34
|
-
id: data.id
|
|
35
|
-
});
|
|
36
|
-
if (keyResult.isFail()) throw keyResult.error;
|
|
37
|
-
return {
|
|
38
|
-
id: data.id,
|
|
39
|
-
key: fileKey,
|
|
40
|
-
name: file.name,
|
|
41
|
-
size: file.size,
|
|
42
|
-
type: file.type
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
export { FileNormalizer };
|
|
47
|
-
|
|
48
|
-
//# sourceMappingURL=FileNormalizer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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 { validateFileKey } from \"@webiny/api-file-manager/domain/file/validateFileKey.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 // The ID and key prefix come from the client and end up in a storage path.\n const fileKey = key.toString();\n const keyResult = validateFileKey({ key: fileKey, id: data.id });\n if (keyResult.isFail()) {\n throw keyResult.error;\n }\n\n return {\n id: data.id,\n key: fileKey,\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","fileKey","keyResult","validateFileKey"],"mappings":";;;;AAcA,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;QAIvB,MAAMC,UAAUJ,IAAI,QAAQ;QAC5B,MAAMK,YAAYC,gBAAgB;YAAE,KAAKF;YAAS,IAAIN,KAAK,EAAE;QAAC;QAC9D,IAAIO,UAAU,MAAM,IAChB,MAAMA,UAAU,KAAK;QAGzB,OAAO;YACH,IAAIP,KAAK,EAAE;YACX,KAAKM;YACL,MAAMD,KAAK,IAAI;YACf,MAAMA,KAAK,IAAI;YACf,MAAMA,KAAK,IAAI;QACnB;IACJ;AACJ"}
|
|
@@ -1,36 +0,0 @@
|
|
|
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 {};
|
|
@@ -1,41 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/utils/mimeTypes.d.ts
DELETED
package/utils/mimeTypes.js
DELETED
package/utils/mimeTypes.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
File without changes
|