@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 3.55 kB
Source Map (JSON)
{"version":3,"file":"clear-all.mjs","names":["mediaServices.checks.checkHasMediaStrategy"],"sources":["../../../src/services/processed-images/clear-all.ts"],"sourcesContent":["import { ProcessedImagesRepository } from \"../../libs/repositories/index.js\";\nimport { resolveMediaTenant } from \"../../utils/media/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport { mediaServices } from \"../index.js\";\nimport adjustStorageUsage from \"../media/adjust-storage-usage.js\";\n\n// TODO: push this to a queue\nconst clearAll: ServiceFn<[], undefined> = async (context) => {\n\tconst mediaStrategyRes =\n\t\tawait mediaServices.checks.checkHasMediaStrategy(context);\n\tif (mediaStrategyRes.error) return mediaStrategyRes;\n\n\tconst ProcessedImages = new ProcessedImagesRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\tconst tenant = resolveMediaTenant(\n\t\tcontext.config,\n\t\tcontext.request.tenantKey ?? null,\n\t);\n\n\tconst processedImagesRes =\n\t\tawait ProcessedImages.selectMultipleWithMediaTenant({\n\t\t\ttenantKey: context.request.tenantKey,\n\t\t});\n\tif (processedImagesRes.error) return processedImagesRes;\n\n\tif (processedImagesRes.data.length === 0) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst sizeByTenant = new Map<string | null, number>();\n\tfor (const image of processedImagesRes.data) {\n\t\tsizeByTenant.set(\n\t\t\timage.tenant_key,\n\t\t\t(sizeByTenant.get(image.tenant_key) ?? 0) + image.file_size,\n\t\t);\n\t}\n\n\tconst [_, clearProcessedRes] = await Promise.all([\n\t\tmediaStrategyRes.data.deleteMultiple(context, {\n\t\t\tkeys: processedImagesRes.data.map((i) => i.key),\n\t\t\ttenant,\n\t\t}),\n\t\tProcessedImages.deleteMultiple({\n\t\t\twhere:\n\t\t\t\tcontext.request.tenantKey !== undefined &&\n\t\t\t\tcontext.request.tenantKey !== null\n\t\t\t\t\t? [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tkey: \"key\",\n\t\t\t\t\t\t\t\toperator: \"in\",\n\t\t\t\t\t\t\t\tvalue: processedImagesRes.data.map((i) => i.key),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t]\n\t\t\t\t\t: [],\n\t\t}),\n\t]);\n\tif (clearProcessedRes.error) return clearProcessedRes;\n\n\tfor (const [tenantKey, totalSize] of sizeByTenant.entries()) {\n\t\tconst updateStorageRes = await adjustStorageUsage(context, {\n\t\t\ttenantKey,\n\t\t\tdelta: -totalSize,\n\t\t\tmin: 0,\n\t\t});\n\t\tif (updateStorageRes.error) return updateStorageRes;\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default clearAll;\n"],"mappings":"qPAOA,MAAM,EAAqC,KAAO,IAAY,CAC7D,IAAM,EACL,MAAMA,EAA2C,CAAO,EACzD,GAAI,EAAiB,MAAO,OAAO,EAEnC,IAAM,EAAkB,IAAI,EAC3B,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EACM,EAAS,EACd,EAAQ,OACR,EAAQ,QAAQ,WAAa,IAC9B,EAEM,EACL,MAAM,EAAgB,8BAA8B,CACnD,UAAW,EAAQ,QAAQ,SAC5B,CAAC,EACF,GAAI,EAAmB,MAAO,OAAO,EAErC,GAAI,EAAmB,KAAK,SAAW,EACtC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAe,IAAI,IACzB,IAAK,IAAM,KAAS,EAAmB,KACtC,EAAa,IACZ,EAAM,YACL,EAAa,IAAI,EAAM,UAAU,GAAK,GAAK,EAAM,SACnD,EAGD,GAAM,CAAC,EAAG,GAAqB,MAAM,QAAQ,IAAI,CAChD,EAAiB,KAAK,eAAe,EAAS,CAC7C,KAAM,EAAmB,KAAK,IAAK,GAAM,EAAE,GAAG,EAC9C,QACD,CAAC,EACD,EAAgB,eA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