mirror of
https://github.com/detleph/server.git
synced 2026-09-07 16:06:18 +02:00
FIX: **quickly** fix all errors in the media system
This commit is contained in:
@@ -11,6 +11,8 @@ COPY package.json package-lock.json /app/
|
|||||||
# Change directory into the container
|
# Change directory into the container
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN mkdir /app/media
|
||||||
|
|
||||||
# Install dependencies
|
# Install dependencies
|
||||||
RUN npm i -g typescript
|
RUN npm i -g typescript
|
||||||
RUN npm i
|
RUN npm i
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import { Prisma } from "@prisma/client";
|
|||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import NotFoundError from "../Middleware/error/NotFoundError";
|
import NotFoundError from "../Middleware/error/NotFoundError";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { generateInvalidBodyError, DataType, } from "./common";
|
import { generateInvalidBodyError, DataType } from "./common";
|
||||||
import { type } from "os";
|
import { type } from "os";
|
||||||
import {unlink} from "fs/promises"
|
import { unlink } from "fs/promises";
|
||||||
|
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||||
|
|
||||||
|
require("express-async-errors");
|
||||||
|
|
||||||
function createMediaLinks(fileName: string) {
|
function createMediaLinks(fileName: string) {
|
||||||
return [{ rel: "self", type: "GET", href: `/api/media/${fileName}` }];
|
return [{ rel: "self", type: "GET", href: `/api/media/${fileName}` }];
|
||||||
@@ -25,6 +27,11 @@ export const uploadImage = async (req: Request, res: Response) => {
|
|||||||
return res.status(403).json(createInsufficientPermissionsError());
|
return res.status(403).json(createInsufficientPermissionsError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// REVIEW: This is only a **quick** fix (As the body is multipart form data, it cannot be used like this)
|
||||||
|
// @ts-ignore
|
||||||
|
req.body.description = "Sample image";
|
||||||
|
console.log(req.body);
|
||||||
|
|
||||||
if (!req.files || !("file" in req.files)) {
|
if (!req.files || !("file" in req.files)) {
|
||||||
return res.json({
|
return res.json({
|
||||||
type: "error",
|
type: "error",
|
||||||
@@ -39,10 +46,10 @@ export const uploadImage = async (req: Request, res: Response) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( typeof req.body.description !== "string") {
|
if (typeof req.body.description !== "string") {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError({
|
||||||
description: DataType.STRING
|
description: DataType.STRING,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -83,24 +90,30 @@ export const uploadImage = async (req: Request, res: Response) => {
|
|||||||
return res.status(409).json(createError("The uploaded file already exists", {}, createMediaLinks(fileName)));
|
return res.status(409).json(createError("The uploaded file already exists", {}, createMediaLinks(fileName)));
|
||||||
}
|
}
|
||||||
|
|
||||||
file.mv("media/" + fileName, console.error);
|
file.mv("/app/media/" + fileName, console.error);
|
||||||
|
|
||||||
//generate record
|
try {
|
||||||
const media = await prisma.media.create({
|
//generate record
|
||||||
data: {
|
const media = await prisma.media.create({
|
||||||
pid: fileName,
|
data: {
|
||||||
description: req.body.description,
|
pid: fileName,
|
||||||
},
|
description: req.body.description,
|
||||||
select: {
|
},
|
||||||
pid: true,
|
select: {
|
||||||
description: true,
|
pid: true,
|
||||||
|
description: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(201).json({
|
||||||
|
type: "success",
|
||||||
|
payload: { media },
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2002") {
|
||||||
|
throw new ForwardableError(409, `The image with the the hash and extenstion ${fileName} already exists!`);
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
return res.status(201).json({
|
|
||||||
type: "success",
|
|
||||||
payload: { media }
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAllMedia = async (req: Request, res: Response) => {
|
export const getAllMedia = async (req: Request, res: Response) => {
|
||||||
@@ -139,7 +152,7 @@ interface MediaParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const deleteMedia = async (req: Request, res: Response) => {
|
export const deleteMedia = async (req: Request, res: Response) => {
|
||||||
if (req.auth?.permission_level != "ELEVATED"){
|
if (req.auth?.permission_level != "ELEVATED") {
|
||||||
res.status(403).json(createInsufficientPermissionsError());
|
res.status(403).json(createInsufficientPermissionsError());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,14 +162,15 @@ export const deleteMedia = async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
if (fs.existsSync(location)) {
|
if (fs.existsSync(location)) {
|
||||||
await unlink(location);
|
await unlink(location);
|
||||||
|
1;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.discipline.delete({ where: { pid }});
|
await prisma.media.delete({ where: { pid } });
|
||||||
return res.status(204).end();
|
return res.status(204).end();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||||
throw new NotFoundError("discipline", pid);
|
throw new NotFoundError("media", pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ import { deleteMedia, getAllMedia, uploadImage } from "../Controllers/media.cont
|
|||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
// Media storage with express static (Should only handle GET and HEAD request methods)
|
||||||
|
router.use("/", express.static("/app/media", { redirect: false }));
|
||||||
|
|
||||||
router.post("/", requireAuthentication, fileUpload(), uploadImage);
|
router.post("/", requireAuthentication, fileUpload(), uploadImage);
|
||||||
|
|
||||||
// Media storage with express static (Should only handle GET and HEAD request methods)
|
router.get("/", requireAuthentication, getAllMedia); // Reading all files should be a senstive operation
|
||||||
router.use("/", express.static("media", { redirect: false }));
|
|
||||||
|
|
||||||
router.get("/:pid", getAllMedia);
|
|
||||||
|
|
||||||
router.delete("/:pid", requireAuthentication, deleteMedia);
|
router.delete("/:pid", requireAuthentication, deleteMedia);
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -60,11 +60,11 @@ async function main() {
|
|||||||
|
|
||||||
app.use("/api/role-schemas", roleSchemaRouter);
|
app.use("/api/role-schemas", roleSchemaRouter);
|
||||||
|
|
||||||
// Error handling
|
|
||||||
app.use(defaultErrorHandler); // Not working
|
|
||||||
|
|
||||||
app.use("/api/media", mediaRouter);
|
app.use("/api/media", mediaRouter);
|
||||||
|
|
||||||
|
// Error handling
|
||||||
|
app.use(defaultErrorHandler); // This has to be the LAST ROUTE
|
||||||
|
|
||||||
// Disable the media router for now
|
// Disable the media router for now
|
||||||
// app.use("/api/media", mediaRouter);
|
// app.use("/api/media", mediaRouter);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user