Skip to content

Commit 1d2b396

Browse files
committed
Implement the Image service for compression and upload
1 parent e16e8fc commit 1d2b396

File tree

7 files changed

+352
-101
lines changed

7 files changed

+352
-101
lines changed

backend/src/modules/content/controllers/content.controller.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class ContentController {
4545
const file = files.thumbnail[0];
4646
const fileName = file.newFilename;
4747
const fileType = file.mimetype;
48-
const filePath = file.filepath; // Use 'filepath' for formidable v2+
48+
const filePath = file.filepath;
4949

5050
try {
5151
if (!filePath) {
@@ -54,10 +54,10 @@ export class ContentController {
5454

5555
// Pass the file object with the correct path to StorageService
5656
const response = await StorageService.uploadFile(
57-
{ ...file, path: filePath }, // Ensure 'path' is set if your StorageService expects it
57+
{ ...file, path: filePath },
5858
"thumbnails",
5959
fileName,
60-
fileType,
60+
fileType
6161
);
6262
res.status(201).json(response);
6363
} catch (error: any) {
@@ -86,15 +86,16 @@ export class ContentController {
8686

8787
static async editContent(req: Request, res: Response) {
8888
console.log("Fetching Content...");
89-
const { contentId, userId } = req.params;
90-
const { data } = req.body;
89+
const { contentId } = req.params;
90+
const data = req.body;
91+
const uid = req.user?.uid;
9192

9293
try {
9394
// const confirmation = await axios.get(`${apiURL}/content/${contentId}`)
9495
const confirmation = await ContentService.getContent(contentId);
9596
const owner_id = confirmation?.creatorUID;
9697

97-
if (userId == owner_id) {
98+
if (uid == owner_id) {
9899
//check whether they are allowed to edit the content
99100
const response = await ContentService.editContent(contentId, data);
100101
res.status(200).json(response);
@@ -104,7 +105,7 @@ export class ContentController {
104105
} catch (error: any) {
105106
console.log(error);
106107
res
107-
.status(401)
108+
.status(500)
108109
.json({ error: error.message || "Failed to edit content" });
109110
}
110111
}
@@ -156,7 +157,7 @@ export class ContentController {
156157
file,
157158
"thumbnails",
158159
fileName,
159-
fileType,
160+
fileType
160161
);
161162

162163
const updateData = JSON.parse(fields.data);

backend/src/modules/content/routes/content.routes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Router } from "express";
22
import { ContentController } from "../controllers/content.controller";
33
import commentRouter from "./comment.routes";
4+
import { authenticateToken } from "../../../shared/middleware/auth";
45

56
const contentRoutes = Router();
67

@@ -23,7 +24,11 @@ contentRoutes.put("/shares/:contentId", ContentController.incrementShareCount);
2324

2425
contentRoutes.delete("/:contentId", ContentController.deleteContent); // Delete content by ID
2526

26-
contentRoutes.put("/:contentId/:userId", ContentController.editContent); // Edit content by ID
27+
contentRoutes.put(
28+
"/:contentId",
29+
authenticateToken,
30+
ContentController.editContent
31+
); // Edit content by ID
2732
contentRoutes.put(
2833
"/editThumbnail/:contentId/:userId",
2934
ContentController.editContentAndThumbnail

backend/src/modules/storage/services/storage.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,18 @@ export class StorageService {
107107
throw new Error("No valid file data found (no filepath or buffer).");
108108
}
109109

110+
// Saving File to direct path
110111
fileName = `${fileName}${fileType ? `.${fileType.split("/")[1]}` : ""}`;
111-
112112
const fullPath = path.join(dir, fileName);
113113
await fs.writeFile(fullPath, fileBuffer);
114114
logger.info(`File saved locally at ${fullPath}`);
115115

116+
// Returning URL for the file
117+
const urlPath = `${filePath}/${fileName}`;
118+
const url = `http://localhost:3000/local_uploads/${urlPath}`;
119+
116120
// Return the absolute file system path
117-
return { url: fullPath };
121+
return { url };
118122
} catch (error: any) {
119123
logger.error("Local upload error:", error);
120124
throw new Error(`Local upload failed: ${error.message}`);

frontend/src/components/content/CommentList.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ export default function CommentList({
5454
setLoading(false);
5555
return;
5656
}
57-
setComments(commentsResult);
58-
setNumComments(commentsResult.length);
57+
58+
if (commentsResult) {
59+
setComments(commentsResult);
60+
setNumComments(commentsResult.length);
61+
}
5962
setLoading(false);
6063
};
6164

0 commit comments

Comments
 (0)