return presigned url when getting a single video
This commit is contained in:
parent
d578dd18e1
commit
e87da5cc4c
1 changed files with 11 additions and 1 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||||
import { PutObjectCommand } from "@aws-sdk/client-s3";
|
import { GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
|
||||||
import { env } from "~/env.mjs";
|
import { env } from "~/env.mjs";
|
||||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
|
|
@ -19,6 +19,7 @@ export const videoRouter = createTRPCRouter({
|
||||||
get: protectedProcedure
|
get: protectedProcedure
|
||||||
.input(z.object({ videoId: z.string() }))
|
.input(z.object({ videoId: z.string() }))
|
||||||
.query(async ({ ctx, input }) => {
|
.query(async ({ ctx, input }) => {
|
||||||
|
const { s3 } = ctx;
|
||||||
const video = await ctx.prisma.video.findUnique({
|
const video = await ctx.prisma.video.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: input.videoId,
|
id: input.videoId,
|
||||||
|
|
@ -29,6 +30,15 @@ export const videoRouter = createTRPCRouter({
|
||||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getObjectCommand = new GetObjectCommand({
|
||||||
|
Bucket: env.AWS_BUCKET_NAME,
|
||||||
|
Key: ctx.session.user.id + "/" + video.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const signedUrl = await getSignedUrl(s3, getObjectCommand);
|
||||||
|
|
||||||
|
video.video_url = signedUrl;
|
||||||
|
|
||||||
return video;
|
return video;
|
||||||
}),
|
}),
|
||||||
getUploadUrl: protectedProcedure
|
getUploadUrl: protectedProcedure
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue