This project provides a basic scaffold for a file management application with a Next.js frontend and an Express backend.
-
Backend:
cd backend npm install node server.jsThe backend server will run on
http://localhost:3001. -
Frontend:
cd frontend npm install npm run devThe frontend development server will run on
http://localhost:3000.
Goal: Enhance the file listing application to allow users to view file details, add comments, and render the content of specific file types directly in the browser.
Instructions:
-
Understand the Existing Code:
- Familiarize yourself with the provided Next.js frontend (
frontend/src) and Express backend (backend). - Review the data structure in
backend/data/data.json(note the newfilenameproperty) and the API endpoints inbackend/server.js. - Examine
frontend/src/app/page.tsxandfrontend/src/components/CommentSection.tsx. Understand state management, data fetching, and component interaction.
- Familiarize yourself with the provided Next.js frontend (
-
Verify Core Functionality:
- Run both servers and confirm the file list loads, selection works, and basic commenting is functional.
-
Implement File Previews (DOCX and PDF):
- Backend Task (DOCX Conversion):
- Add a Node.js library capable of converting
.docxfiles to HTML to the backend project (e.g.,mammoth). Install it usingnpm install mammothin thebackenddirectory. - Create a new API endpoint in
backend/server.js, for example:GET /api/files/:fileId/content. - This endpoint should:
- Find the file metadata in
data.jsonusing the:fileId. - Check if the
filenameends with.docx. - If it's a
.docxfile, read the file from thebackend/datadirectory using itsfilename. - Use the chosen library (e.g.,
mammoth.convertToHtml()) to convert the.docxfile buffer/content into an HTML string. - Handle potential errors during conversion.
- Send the resulting HTML string back as the response (e.g.,
res.send(result.value)). - If the file is not a
.docxfile, proceed to the next check (or respond appropriately if no other preview type is supported).
- Find the file metadata in
- Add a Node.js library capable of converting
- Backend Task (PDF Serving):
- Create another API endpoint (or modify the previous one) specifically for serving files suitable for browser viewing, e.g.,
GET /api/files/:fileId/view. - This endpoint should:
- Find the file metadata in
data.jsonusing:fileId. - Get the
filename(e.g., "contract.pdf"). - Construct the full path to the file in
backend/data. - Check if the file exists.
- If the file is a
.pdf, set the response headerContent-Typetoapplication/pdf. - Read the PDF file and send its contents in the response. Do not set the
Content-Disposition: attachmentheader. (Usingres.sendFile()in Express is recommended). - If the file is not a PDF (and wasn't handled by DOCX conversion), respond with an appropriate error (e.g., 400 or
{ message: 'Preview not supported' }).
- Find the file metadata in
- Create another API endpoint (or modify the previous one) specifically for serving files suitable for browser viewing, e.g.,
- Frontend Task (Conditional Rendering):
- Modify the frontend (
page.tsxor a new detail component) to determine the selected file's type based on itsfilename(you may need to ensure thefilenameis available in the frontend state when a file is selected). - When a
.docxfile is selected:- Call the
/api/files/:fileId/contentendpoint. - Display a loading state.
- Render the fetched HTML using
dangerouslySetInnerHTMLin a designated area. (Acknowledge security implications for the challenge). - Handle errors.
- Call the
- When a
.pdffile is selected:- Render an
<iframe>element. - Set the
srcattribute to the PDF viewing endpoint (e.g.,/api/files/:fileId/view). - Set appropriate
width,height, andtitleattributes for the iframe.
- Render an
- For any other file types, display a message like "Preview not available for this file type."
- Ensure the Comment section is still accessible, perhaps alongside or below the preview area.
- Modify the frontend (
- Backend Task (DOCX Conversion):
-
(Optional) Enhance File Details Display:
- Display the selected file's details (name, upload date) more prominently when selected, perhaps above the preview/comment area.
-
(Optional) Refine Styling:
- Improve the visual presentation, especially the layout of the file list, selected file details, preview area (iframe/HTML content), and comments.
-
(Optional) Enhance Error Handling & Loading States:
- Add more specific user feedback for loading states (e.g., when converting/fetching DOCX, loading PDF iframe) and error scenarios.