From 6fd2a98aa5aa2fe209366aa2ce69617c0cad00d5 Mon Sep 17 00:00:00 2001 From: Rajat Saxena Date: Thu, 9 May 2024 01:08:02 +0530 Subject: [PATCH] Public and private files on separate paths --- apps/api/src/media/service.ts | 37 +++++++++++++------ apps/api/src/media/utils/generate-key.ts | 36 ++++++++++++------ .../{get-cdn-urls.ts => get-public-urls.ts} | 4 +- 3 files changed, 53 insertions(+), 24 deletions(-) rename apps/api/src/media/utils/{get-cdn-urls.ts => get-public-urls.ts} (89%) diff --git a/apps/api/src/media/service.ts b/apps/api/src/media/service.ts index 4bdc32f3..95b531a7 100644 --- a/apps/api/src/media/service.ts +++ b/apps/api/src/media/service.ts @@ -37,7 +37,7 @@ import { } from "./queries"; import * as presignedUrlService from "../presigning/service"; import getTags from "./utils/get-tags"; -import { getMainFileUrl, getThumbnailUrl } from "./utils/get-cdn-urls"; +import { getMainFileUrl, getThumbnailUrl } from "./utils/get-public-urls"; const generateAndUploadThumbnail = async ({ workingDirectory, @@ -122,8 +122,10 @@ async function upload({ const uploadParams: UploadParams = { Key: generateKey({ mediaId: fileName.name, - extension: fileExtension, - type: "main", + access: access === "public" ? "public" : "private", + filename: `main.${fileExtension}`, + // extension: fileExtension, + // type: "main", }), Body: createReadStream(mainFilePath), ContentType: mimeType, @@ -142,7 +144,9 @@ async function upload({ originalFilePath: mainFilePath, key: generateKey({ mediaId: fileName.name, - type: "thumb", + access: "public", + filename: "thumb.webp", + // type: "thumb", }), tags, }); @@ -249,10 +253,17 @@ async function getMediaDetails({ ? await generateSignedUrl({ name: generateKey({ mediaId: media.mediaId, - extension: path + access: + media.accessControl === "private" + ? "private" + : "public", + filename: `main.${path .extname(media.fileName) - .replace(".", ""), - type: "main", + .replace(".", "")}`, + // extension: path + // .extname(media.fileName) + // .replace(".", ""), + // type: "main", }), }) : getMainFileUrl(media), @@ -278,16 +289,20 @@ async function deleteMedia({ const key = generateKey({ mediaId, - extension: media.mimeType.split("/")[1], - type: "main", + access: media.accessControl === "private" ? "private" : "public", + filename: `main.${media.fileName.split(".")[1]}`, + // extension: media.mimeType.split("/")[1], + // type: "main", }); await deleteObject({ Key: key }); if (media.thumbnailGenerated) { const thumbKey = generateKey({ mediaId, - extension: media.mimeType.split("/")[1], - type: "thumb", + access: "public", + filename: "thumb.webp", + // extension: media.mimeType.split("/")[1], + // type: "thumb", }); await deleteObject({ Key: thumbKey }); } diff --git a/apps/api/src/media/utils/generate-key.ts b/apps/api/src/media/utils/generate-key.ts index 73ce7f29..92b4de63 100644 --- a/apps/api/src/media/utils/generate-key.ts +++ b/apps/api/src/media/utils/generate-key.ts @@ -1,17 +1,31 @@ import { CLOUD_PREFIX } from "../../config/constants"; -interface GenerateKeyProps { - mediaId: string; - type: "main" | "thumb"; - extension?: string; -} +// interface GenerateKeyProps { +// mediaId: string; +// type: "main" | "thumb"; +// extension?: string; +// } + +// export default function generateKey({ +// mediaId, +// type, +// extension, +// }: GenerateKeyProps): string { +// return `${CLOUD_PREFIX ? `${CLOUD_PREFIX}/` : ""}${mediaId}/${type}.${ +// type === "thumb" ? "webp" : extension +// }`; +// } export default function generateKey({ mediaId, - type, - extension, -}: GenerateKeyProps): string { - return `${CLOUD_PREFIX ? `${CLOUD_PREFIX}/` : ""}${mediaId}/${type}.${ - type === "thumb" ? "webp" : extension - }`; + access, + filename, +}: { + mediaId: string; + access: "private" | "public"; + filename: string; +}): string { + return `${ + CLOUD_PREFIX ? `${CLOUD_PREFIX}/` : "" + }${access}/${mediaId}/${filename}`; } diff --git a/apps/api/src/media/utils/get-cdn-urls.ts b/apps/api/src/media/utils/get-public-urls.ts similarity index 89% rename from apps/api/src/media/utils/get-cdn-urls.ts rename to apps/api/src/media/utils/get-public-urls.ts index 3196747d..56f5b2af 100644 --- a/apps/api/src/media/utils/get-cdn-urls.ts +++ b/apps/api/src/media/utils/get-public-urls.ts @@ -3,12 +3,12 @@ import { cdnEndpoint, CLOUD_PREFIX } from "../../config/constants"; import { Media } from "@medialit/models"; export function getMainFileUrl(media: Media) { - return `${cdnEndpoint}/${CLOUD_PREFIX ? `${CLOUD_PREFIX}/` : ""}${ + return `${cdnEndpoint}/${CLOUD_PREFIX ? `${CLOUD_PREFIX}/` : ""}public/${ media.mediaId }/main${path.extname(media.fileName)}`; } export function getThumbnailUrl(mediaId: string) { return `${cdnEndpoint}/${ CLOUD_PREFIX ? `${CLOUD_PREFIX}/` : "" - }${mediaId}/thumb.webp`; + }public/${mediaId}/thumb.webp`; }