feat: Implement file upload and management features in ProjectViewPage
This commit is contained in:
@@ -6,9 +6,9 @@ import path from "path";
|
||||
import db from "@/lib/db";
|
||||
|
||||
export async function GET(request, { params }) {
|
||||
try {
|
||||
const fileId = params.fileId;
|
||||
const { fileId } = await params;
|
||||
|
||||
try {
|
||||
// Get file info from database
|
||||
const file = db.prepare(`
|
||||
SELECT * FROM file_attachments WHERE file_id = ?
|
||||
@@ -53,10 +53,94 @@ export async function GET(request, { params }) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request, { params }) {
|
||||
export async function PUT(request, { params }) {
|
||||
const { fileId } = await params;
|
||||
try {
|
||||
const fileId = params.fileId;
|
||||
const body = await request.json();
|
||||
const { description, original_filename } = body;
|
||||
|
||||
// Validate input
|
||||
if (description !== undefined && typeof description !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: "Description must be a string" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (original_filename !== undefined && typeof original_filename !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: "Original filename must be a string" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
const existingFile = db.prepare(`
|
||||
SELECT * FROM file_attachments WHERE file_id = ?
|
||||
`).get(parseInt(fileId));
|
||||
|
||||
if (!existingFile) {
|
||||
return NextResponse.json(
|
||||
{ error: "File not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Build update query
|
||||
const updates = [];
|
||||
const values = [];
|
||||
|
||||
if (description !== undefined) {
|
||||
updates.push('description = ?');
|
||||
values.push(description);
|
||||
}
|
||||
|
||||
if (original_filename !== undefined) {
|
||||
updates.push('original_filename = ?');
|
||||
values.push(original_filename);
|
||||
}
|
||||
|
||||
if (updates.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "No valid fields to update" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
values.push(parseInt(fileId));
|
||||
|
||||
const result = db.prepare(`
|
||||
UPDATE file_attachments
|
||||
SET ${updates.join(', ')}
|
||||
WHERE file_id = ?
|
||||
`).run(...values);
|
||||
|
||||
if (result.changes === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "File not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get updated file
|
||||
const updatedFile = db.prepare(`
|
||||
SELECT * FROM file_attachments WHERE file_id = ?
|
||||
`).get(parseInt(fileId));
|
||||
|
||||
return NextResponse.json(updatedFile);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error updating file:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update file" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request, { params }) {
|
||||
const { fileId } = await params;
|
||||
try {
|
||||
// Get file info from database
|
||||
const file = db.prepare(`
|
||||
SELECT * FROM file_attachments WHERE file_id = ?
|
||||
|
||||
Reference in New Issue
Block a user