Refactor code
This commit is contained in:
@ -2,25 +2,30 @@
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { s3 } from "@/lib/s3";
|
||||
import {
|
||||
confirmArtworkTimelapseUploadSchema,
|
||||
createArtworkTimelapseUploadSchema,
|
||||
deleteArtworkTimelapseSchema,
|
||||
setArtworkTimelapseEnabledSchema,
|
||||
} from "@/schemas/artworks/timelapse";
|
||||
import type {
|
||||
ConfirmArtworkTimelapseUploadInput,
|
||||
CreateArtworkTimelapseUploadInput,
|
||||
DeleteArtworkTimelapseInput,
|
||||
SetArtworkTimelapseEnabledInput,
|
||||
} from "@/schemas/artworks/timelapse";
|
||||
import { PutObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod/v4";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
const createUploadSchema = z.object({
|
||||
artworkId: z.string().min(1),
|
||||
fileName: z.string().min(1),
|
||||
mimeType: z.string().min(1),
|
||||
sizeBytes: z.number().int().positive(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates a presigned PUT url so the client can upload large timelapse videos directly to S3
|
||||
* (avoids Next.js body-size/proxy limits).
|
||||
*/
|
||||
export async function createArtworkTimelapseUpload(input: z.infer<typeof createUploadSchema>) {
|
||||
const { artworkId, fileName, mimeType, sizeBytes } = createUploadSchema.parse(input);
|
||||
export async function createArtworkTimelapseUpload(input: CreateArtworkTimelapseUploadInput) {
|
||||
const { artworkId, fileName, mimeType, sizeBytes } =
|
||||
createArtworkTimelapseUploadSchema.parse(input);
|
||||
|
||||
const ext = fileName.includes(".") ? fileName.split(".").pop() : undefined;
|
||||
const suffix = ext ? `.${ext}` : "";
|
||||
@ -41,17 +46,10 @@ export async function createArtworkTimelapseUpload(input: z.infer<typeof createU
|
||||
return { uploadUrl, s3Key, fileName, mimeType, sizeBytes };
|
||||
}
|
||||
|
||||
const confirmSchema = z.object({
|
||||
artworkId: z.string().min(1),
|
||||
s3Key: z.string().min(1),
|
||||
fileName: z.string().min(1),
|
||||
mimeType: z.string().min(1),
|
||||
sizeBytes: z.number().int().positive(),
|
||||
});
|
||||
|
||||
/** Persist uploaded timelapse metadata in DB (upsert by artworkId). */
|
||||
export async function confirmArtworkTimelapseUpload(input: z.infer<typeof confirmSchema>) {
|
||||
const { artworkId, s3Key, fileName, mimeType, sizeBytes } = confirmSchema.parse(input);
|
||||
export async function confirmArtworkTimelapseUpload(input: ConfirmArtworkTimelapseUploadInput) {
|
||||
const { artworkId, s3Key, fileName, mimeType, sizeBytes } =
|
||||
confirmArtworkTimelapseUploadSchema.parse(input);
|
||||
|
||||
// If an old timelapse exists, delete the old object so you don't leak storage.
|
||||
const existing = await prisma.artworkTimelapse.findUnique({ where: { artworkId } });
|
||||
@ -91,13 +89,8 @@ export async function confirmArtworkTimelapseUpload(input: z.infer<typeof confir
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
const enabledSchema = z.object({
|
||||
artworkId: z.string().min(1),
|
||||
enabled: z.boolean(),
|
||||
});
|
||||
|
||||
export async function setArtworkTimelapseEnabled(input: z.infer<typeof enabledSchema>) {
|
||||
const { artworkId, enabled } = enabledSchema.parse(input);
|
||||
export async function setArtworkTimelapseEnabled(input: SetArtworkTimelapseEnabledInput) {
|
||||
const { artworkId, enabled } = setArtworkTimelapseEnabledSchema.parse(input);
|
||||
|
||||
await prisma.artworkTimelapse.update({
|
||||
where: { artworkId },
|
||||
@ -108,12 +101,8 @@ export async function setArtworkTimelapseEnabled(input: z.infer<typeof enabledSc
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
const deleteSchema = z.object({
|
||||
artworkId: z.string().min(1),
|
||||
});
|
||||
|
||||
export async function deleteArtworkTimelapse(input: z.infer<typeof deleteSchema>) {
|
||||
const { artworkId } = deleteSchema.parse(input);
|
||||
export async function deleteArtworkTimelapse(input: DeleteArtworkTimelapseInput) {
|
||||
const { artworkId } = deleteArtworkTimelapseSchema.parse(input);
|
||||
|
||||
const existing = await prisma.artworkTimelapse.findUnique({ where: { artworkId } });
|
||||
if (!existing) return { ok: true };
|
||||
|
||||
Reference in New Issue
Block a user