fix media storage get
This commit is contained in:
parent
abb9dcfd36
commit
63a1bf2a8a
2 changed files with 47 additions and 12 deletions
|
|
@ -1,10 +1,14 @@
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { storageId } from "~/schemas/public/Storage";
|
import {
|
||||||
|
type MediaStorage,
|
||||||
|
mediaStorageId,
|
||||||
|
} from "~/schemas/public/MediaStorage";
|
||||||
|
import { type Storage, storageId } from "~/schemas/public/Storage";
|
||||||
import { TOKEN_CONFIG } from "~/server/auth/configs";
|
import { TOKEN_CONFIG } from "~/server/auth/configs";
|
||||||
import { verifyToken } from "~/server/auth/jwt";
|
import { verifyToken } from "~/server/auth/jwt";
|
||||||
import { getFile } from "~/server/services/storage.service";
|
import { getFile, getMediaFile } from "~/server/services/storage.service";
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
|
|
@ -55,19 +59,33 @@ export default async function handler(
|
||||||
return res.status(401);
|
return res.status(401);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const parsedId = storageId.safeParse(id);
|
let file: Storage | MediaStorage | undefined;
|
||||||
if (!parsedId.success) {
|
if (isImageRequest || isNextImageRequest || isVideoRequest) {
|
||||||
if (isImageRequest || isNextImageRequest) {
|
const parsedId = mediaStorageId.safeParse(id);
|
||||||
return getFallback("fallback-image.png", req, res);
|
if (!parsedId.success) {
|
||||||
|
if (isImageRequest || isNextImageRequest) {
|
||||||
|
return getFallback("fallback-image.png", req, res);
|
||||||
|
}
|
||||||
|
if (isVideoRequest) {
|
||||||
|
return getFallback("fallback-video.png", req, res);
|
||||||
|
}
|
||||||
|
return res.status(404).json({ error: "Invalid input id" });
|
||||||
}
|
}
|
||||||
if (isVideoRequest) {
|
file = await getMediaFile(parsedId.data);
|
||||||
return getFallback("fallback-video.png", req, res);
|
} else {
|
||||||
|
const parsedId = storageId.safeParse(id);
|
||||||
|
if (!parsedId.success) {
|
||||||
|
if (isImageRequest || isNextImageRequest) {
|
||||||
|
return getFallback("fallback-image.png", req, res);
|
||||||
|
}
|
||||||
|
if (isVideoRequest) {
|
||||||
|
return getFallback("fallback-video.png", req, res);
|
||||||
|
}
|
||||||
|
return res.status(404).json({ error: "Invalid input id" });
|
||||||
}
|
}
|
||||||
return res.status(404).json({ error: "Invalid input id" });
|
|
||||||
|
file = await getFile(parsedId.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = await getFile(parsedId.data);
|
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
if (isImageRequest || isNextImageRequest) {
|
if (isImageRequest || isNextImageRequest) {
|
||||||
return getFallback("fallback-image.png", req, res);
|
return getFallback("fallback-image.png", req, res);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import type { MediaStorageId } from "~/schemas/public/MediaStorage";
|
||||||
import type {
|
import type {
|
||||||
Storage,
|
Storage,
|
||||||
StorageId,
|
StorageId,
|
||||||
|
|
@ -108,6 +109,22 @@ export const getFile = async (fileId: StorageId) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getMediaFile = async (fileId: MediaStorageId) => {
|
||||||
|
try {
|
||||||
|
return await db
|
||||||
|
.$pickTables<"media_storage">()
|
||||||
|
.selectFrom("media_storage")
|
||||||
|
.selectAll()
|
||||||
|
.where("id", "=", fileId)
|
||||||
|
.executeTakeFirst();
|
||||||
|
} catch (e) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
|
message: `Failed to get media_file: ${(e as Error).message}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const editFile = async (fileId: StorageId, data: StorageUpdate) => {
|
export const editFile = async (fileId: StorageId, data: StorageUpdate) => {
|
||||||
try {
|
try {
|
||||||
await db
|
await db
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue