Skip to content

Commit

Permalink
🐛 Add blockId prop for file upload to ensure unique URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 23, 2024
1 parent 676fe94 commit d6dc242
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { getAuthenticatedUser } from "@/features/auth/helpers/getAuthenticatedUser";
import {
badRequest,
methodNotAllowed,
notAuthenticated,
notFound,
} from "@typebot.io/lib/api/utils";
import { getFileTempUrl } from "@typebot.io/lib/s3/getFileTempUrl";
import prisma from "@typebot.io/prisma";
import { isReadTypebotForbidden } from "@typebot.io/typebot/helpers/isReadTypebotForbidden";
import type { NextApiRequest, NextApiResponse } from "next";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "GET") {
const user = await getAuthenticatedUser(req, res);
if (!user) return notAuthenticated(res);

const typebotId = req.query.typebotId as string;
const resultId = req.query.resultId as string;
const blockId = req.query.blockId as string;
const fileName = req.query.fileName as string;

if (!fileName) return badRequest(res, "fileName missing not found");

const typebot = await prisma.typebot.findFirst({
where: {
id: typebotId,
},
select: {
whatsAppCredentialsId: true,
collaborators: {
select: {
userId: true,
},
},
workspace: {
select: {
id: true,
isSuspended: true,
isPastDue: true,
members: {
select: {
userId: true,
},
},
},
},
},
});

if (!typebot?.workspace || (await isReadTypebotForbidden(typebot, user)))
return notFound(res, "Workspace not found");

if (!typebot) return notFound(res, "Typebot not found");

const tmpUrl = await getFileTempUrl({
key: `private/workspaces/${typebot.workspace.id}/typebots/${typebotId}/results/${resultId}/blocks/${blockId}/${fileName}`,
});

if (!tmpUrl) return notFound(res, "File not found");

return res.redirect(tmpUrl);
}
return methodNotAllowed(res);
};

export default handler;
9 changes: 5 additions & 4 deletions apps/viewer/src/features/fileUpload/api/generateUploadUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const generateUploadUrl = publicProcedure
.input(
z.object({
sessionId: z.string(),
blockId: z.string(),
fileName: z.string(),
fileType: z.string().optional(),
}),
Expand All @@ -35,7 +36,7 @@ export const generateUploadUrl = publicProcedure
fileUrl: z.string(),
}),
)
.mutation(async ({ input: { fileName, sessionId, fileType } }) => {
.mutation(async ({ input: { fileName, sessionId, fileType, blockId } }) => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
Expand Down Expand Up @@ -100,8 +101,8 @@ export const generateUploadUrl = publicProcedure
"workspaceId" in typebot && typebot.workspaceId && resultId
? `${visibility === "Private" ? "private" : "public"}/workspaces/${
typebot.workspaceId
}/typebots/${typebotId}/results/${resultId}/${fileName}`
: `public/tmp/${typebotId}/${fileName}`;
}/typebots/${typebotId}/results/${resultId}/blocks/${blockId}/${fileName}`
: `public/tmp/typebots/${typebotId}/blocks/${blockId}/${fileName}`;

const presignedPostPolicy = await generatePresignedPostPolicy({
fileType,
Expand All @@ -114,7 +115,7 @@ export const generateUploadUrl = publicProcedure
formData: presignedPostPolicy.formData,
fileUrl:
visibility === "Private" && !isPreview
? `${env.NEXTAUTH_URL}/api/typebots/${typebotId}/results/${resultId}/${fileName}`
? `${env.NEXTAUTH_URL}/api/typebots/${typebotId}/results/${resultId}/blocks/${blockId}/${fileName}`
: env.S3_PUBLIC_CUSTOM_DOMAIN
? `${env.S3_PUBLIC_CUSTOM_DOMAIN}/${filePath}`
: `${presignedPostPolicy.postURL}/${presignedPostPolicy.formData.key}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const FileUploadForm = (props: Props) => {
file,
input: {
sessionId: props.context.sessionId,
blockId: props.block.id,
fileName: file.name,
},
},
Expand Down Expand Up @@ -104,6 +105,7 @@ export const FileUploadForm = (props: Props) => {
file: file,
input: {
sessionId: props.context.sessionId,
blockId: props.block.id,
fileName: files.some((f) => f.name === file.name)
? file.name + `-${index}`
: file.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type UploadFileProps = {
file: File;
input: {
sessionId: string;
blockId: string;
fileName: string;
};
}[];
Expand Down Expand Up @@ -39,6 +40,7 @@ export const uploadFiles = async ({
fileName: input.fileName,
sessionId: input.sessionId,
fileType: file.type,
blockId: input.blockId,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const TextInput = (props: Props) => {
files: selectedFiles().map((file) => ({
file: file,
input: {
blockId: props.block.id,
sessionId: props.context.sessionId,
fileName: file.name,
},
Expand Down Expand Up @@ -217,6 +218,7 @@ export const TextInput = (props: Props) => {
{
file: audioFile,
input: {
blockId: props.block.id,
sessionId: props.context.sessionId,
fileName: audioFile.name,
},
Expand Down

0 comments on commit d6dc242

Please sign in to comment.