44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { z } from "zod"
|
|
|
|
export const allowedImageContentTypes = [
|
|
"image/jpeg",
|
|
"image/png",
|
|
"image/webp",
|
|
"image/heic",
|
|
"image/heif",
|
|
] as const
|
|
export const allowedImageExtensions = [".jpg", ".jpeg", ".png", ".webp", ".heic", ".heif"] as const
|
|
export const maxUploadSizeBytes = 10 * 1024 * 1024
|
|
|
|
export type AllowedImageContentType = (typeof allowedImageContentTypes)[number]
|
|
|
|
export function getUploadContentType(file: File): AllowedImageContentType | null {
|
|
const type = file.type.toLowerCase()
|
|
if (type === "image/jpg") return "image/jpeg"
|
|
if (allowedImageContentTypes.includes(type as AllowedImageContentType)) {
|
|
return type as AllowedImageContentType
|
|
}
|
|
|
|
const extension = `.${file.name.split(".").pop()?.toLowerCase() ?? ""}`
|
|
if ([".jpg", ".jpeg"].includes(extension)) return "image/jpeg"
|
|
if (extension === ".png") return "image/png"
|
|
if (extension === ".webp") return "image/webp"
|
|
if (extension === ".heic") return "image/heic"
|
|
if (extension === ".heif") return "image/heif"
|
|
return null
|
|
}
|
|
|
|
export const presignUploadSchema = z.object({
|
|
token: z.string().min(1),
|
|
filename: z.string().trim().min(1).max(255),
|
|
contentType: z.enum(allowedImageContentTypes),
|
|
sizeBytes: z.number().int().positive().max(maxUploadSizeBytes),
|
|
})
|
|
|
|
export const completeUploadSchema = presignUploadSchema.extend({
|
|
objectKey: z.string().min(1),
|
|
})
|
|
|
|
export type PresignUploadInput = z.infer<typeof presignUploadSchema>
|
|
export type CompleteUploadInput = z.infer<typeof completeUploadSchema>
|