Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions backend/src/modules/content/controllers/content.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ContentController {
const file = files.thumbnail[0];
const fileName = file.newFilename;
const fileType = file.mimetype;
const filePath = file.filepath; // Use 'filepath' for formidable v2+
const filePath = file.filepath;

try {
if (!filePath) {
Expand All @@ -54,10 +54,10 @@ export class ContentController {

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

static async editContent(req: Request, res: Response) {
console.log("Fetching Content...");
const { contentId, userId } = req.params;
const { data } = req.body;
const { contentId } = req.params;
const data = req.body;
const uid = req.user?.uid;

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

if (userId == owner_id) {
if (uid == owner_id) {
//check whether they are allowed to edit the content
const response = await ContentService.editContent(contentId, data);
res.status(200).json(response);
Expand All @@ -104,7 +105,7 @@ export class ContentController {
} catch (error: any) {
console.log(error);
res
.status(401)
.status(500)
.json({ error: error.message || "Failed to edit content" });
}
}
Expand Down Expand Up @@ -156,7 +157,7 @@ export class ContentController {
file,
"thumbnails",
fileName,
fileType,
fileType
);

const updateData = JSON.parse(fields.data);
Expand Down
7 changes: 6 additions & 1 deletion backend/src/modules/content/routes/content.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from "express";
import { ContentController } from "../controllers/content.controller";
import commentRouter from "./comment.routes";
import { authenticateToken } from "../../../shared/middleware/auth";

const contentRoutes = Router();

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

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

contentRoutes.put("/:contentId/:userId", ContentController.editContent); // Edit content by ID
contentRoutes.put(
"/:contentId",
authenticateToken,
ContentController.editContent
); // Edit content by ID
contentRoutes.put(
"/editThumbnail/:contentId/:userId",
ContentController.editContentAndThumbnail
Expand Down
8 changes: 6 additions & 2 deletions backend/src/modules/storage/services/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ export class StorageService {
throw new Error("No valid file data found (no filepath or buffer).");
}

// Saving File to direct path
fileName = `${fileName}${fileType ? `.${fileType.split("/")[1]}` : ""}`;

const fullPath = path.join(dir, fileName);
await fs.writeFile(fullPath, fileBuffer);
logger.info(`File saved locally at ${fullPath}`);

// Returning URL for the file
const urlPath = `${filePath}/${fileName}`;
const url = `http://localhost:3000/local_uploads/${urlPath}`;

// Return the absolute file system path
return { url: fullPath };
return { url };
Comment on lines +116 to +121
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update the misleading comment.

The comment on line 120 states "Return the absolute file system path" but the method now returns a URL string. This inconsistency could confuse future developers.

-      // Return the absolute file system path
+      // Return the URL for accessing the uploaded file
       return { url };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Returning URL for the file
const urlPath = `${filePath}/${fileName}`;
const url = `http://localhost:3000/local_uploads/${urlPath}`;
// Return the absolute file system path
return { url: fullPath };
return { url };
// Returning URL for the file
const urlPath = `${filePath}/${fileName}`;
const url = `http://localhost:3000/local_uploads/${urlPath}`;
// Return the URL for accessing the uploaded file
return { url };
🤖 Prompt for AI Agents
In backend/src/modules/storage/services/storage.service.ts around lines 116 to
121, the comment on line 120 incorrectly states "Return the absolute file system
path" while the code actually returns a URL string. Update this comment to
accurately reflect that the method returns a URL string pointing to the file
location, removing any mention of an absolute file system path to avoid
confusion.

} catch (error: any) {
logger.error("Local upload error:", error);
throw new Error(`Local upload failed: ${error.message}`);
Expand Down
27 changes: 14 additions & 13 deletions frontend/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint";

export default tseslint.config(
{ ignores: ['dist'] },
{ ignores: ["dist"] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
files: ["**/*.{ts,tsx}"],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
"react-hooks": reactHooks,
"react-refresh": reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
"react-hooks/exhaustive-deps": "off",
},
},
)
}
);
9 changes: 8 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ export default function App() {
<Route path='/legal/accessibility' element={<Accessibility />} />

{/* CONTENT */}
<Route path='/content/create' element={<ContentEditor />} />
<Route
path='/content/create'
element={<ContentEditor isEditMode={false} />}
/>
<Route
path='/content/edit/:id'
element={<ContentEditor isEditMode={true} />}
/>
<Route path='/content/:id' element={<ContentView />} />

{/* PRO */}
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/components/content/CommentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ export default function CommentList({
setLoading(false);
return;
}
setComments(commentsResult);
setNumComments(commentsResult.length);

if (commentsResult) {
setComments(commentsResult);
setNumComments(commentsResult.length);
}
setLoading(false);
};

Expand Down
Loading