Skip to content

Commit

Permalink
improve media endpoints and file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa-lopes-pt committed Jul 31, 2024
1 parent d977980 commit e71da1f
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dist-ssr
NOTES.md

#server disk data
backend/resources/profile-images/*
backend/src/resources/profile-images/*


# Editor directories and files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@ export default async function updateProfileImageMiddleware(
) {
//save image to disk
try {
const resourceDir = "resources/profile-images/";
const path = `${resourceDir}${res.locals.email}.webp`;
const resourceDir = "./src/resources/profile-images",
file = `${res.locals.email}.webp`,
filePath = `${resourceDir}/${file}`;
//the file will always have the same name...so this flag prevents unnecessary calls to db
let alreadyDefined;

if (!fs.existsSync(resourceDir)) {
fs.mkdirSync("./" + resourceDir, { recursive: true });
fs.mkdirSync(resourceDir, { recursive: true });
} else {
alreadyDefined = fs.existsSync(path);
alreadyDefined = fs.existsSync(filePath);
}

fs.writeFileSync("./" + path, res.locals.fileBuffer);
fs.writeFileSync(filePath, res.locals.fileBuffer);

if (alreadyDefined) {
console.log("already created");

return res.status(HTTPCodes.Success.CREATED).end();
}

req.body = {
image: `${req.protocol}://${req.headers.host}/${path}`,
image: `${req.protocol}://${req.headers.host}/resources/profile-images/${res.locals.email}.webp`,
};
return next();
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/routes/controllers/misc/media.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export default function mediaController(
) {
try {
const path = req.path;
return res.status(200).sendFile(path, { root: "./" });
console.log(path);

return res.status(200).sendFile(path, { root: "./src" });
} catch (error) {
next(
new HttpError(HTTPCodes.ClientError.NOT_FOUND, "Not Found", {
Expand Down
1 change: 1 addition & 0 deletions backend/src/routes/middleware/imageOwnerAuth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default async function imageOwnerAuthMiddleware(
res: Response,
next: NextFunction
) {
console.log(req.path);
if (req.path !== `/resources/profile-images/${res.locals.email}.webp`) {
return res
.status(HTTPCodes.ClientError.FORBIDDEN)
Expand Down
12 changes: 7 additions & 5 deletions backend/src/routes/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ router.use(authRouter);
router.use("/shop", shopRouter);
router.use("/profile", accountRouter);
router.use("/orders", ordersRouter);
router.use(mailRouter);
// router.use(mailRouter);
//resource access
router.get(
"/resources/profile-images/:resource",
authMiddleware,
imageOwnerAuthMiddleware,
mediaController
);

/*
router.get(
"/resources/product-images/:resource",
mediaController
);


*/
//STAGE 0 ONLY
router.get("/dev/markAllOrdersAsShipped", markOrdersAsDeliveredController);

//
router.all("*", (_, res) =>
res.status(404).json({ data: "Endpoint Not Implemented" })
);
Expand Down

0 comments on commit e71da1f

Please sign in to comment.