-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Add blockId prop for file upload to ensure unique URLs
- Loading branch information
1 parent
676fe94
commit d6dc242
Showing
5 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...lder/src/pages/api/typebots/[typebotId]/results/[resultId]/blocks/[blockId]/[fileName].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters