Skip to content

Commit

Permalink
Public and private files on separate paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat Saxena committed May 8, 2024
1 parent 0923b66 commit 6fd2a98
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
37 changes: 26 additions & 11 deletions apps/api/src/media/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -142,7 +144,9 @@ async function upload({
originalFilePath: mainFilePath,
key: generateKey({
mediaId: fileName.name,
type: "thumb",
access: "public",
filename: "thumb.webp",
// type: "thumb",
}),
tags,
});
Expand Down Expand Up @@ -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),
Expand All @@ -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 });
}
Expand Down
36 changes: 25 additions & 11 deletions apps/api/src/media/utils/generate-key.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
}

0 comments on commit 6fd2a98

Please sign in to comment.