feat: Implement file upload structure and API for file retrieval

This commit is contained in:
2025-09-16 11:27:25 +02:00
parent e5955a31fd
commit 06599c844a
9 changed files with 76 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ services:
- "3001:3000"
volumes:
- ./data:/app/data
- ./uploads:/app/public/uploads
environment:
- NODE_ENV=production
- NEXTAUTH_SECRET=${NEXTAUTH_SECRET:-your-secret-key-generate-a-strong-random-string-at-least-32-characters}

View File

@@ -8,6 +8,14 @@ echo "🚀 Starting development environment..."
# Ensure data directory exists
mkdir -p /app/data
# Ensure uploads directory structure exists
mkdir -p /app/public/uploads/contracts
mkdir -p /app/public/uploads/projects
mkdir -p /app/public/uploads/tasks
# Set proper permissions for uploads directory
chmod -R 755 /app/public/uploads
# Create admin account if it doesn't exist
echo "🔧 Setting up admin account..."
node scripts/create-admin.js

View File

@@ -8,6 +8,14 @@ echo "🚀 Starting application..."
# Ensure data directory exists
mkdir -p /app/data
# Ensure uploads directory structure exists
mkdir -p /app/public/uploads/contracts
mkdir -p /app/public/uploads/projects
mkdir -p /app/public/uploads/tasks
# Set proper permissions for uploads directory
chmod -R 755 /app/public/uploads
# Create admin account if it doesn't exist
echo "🔧 Setting up admin account..."
node scripts/create-admin.js

View File

@@ -1,8 +1,58 @@
import { NextResponse } from "next/server";
import { readFile } from "fs/promises";
import { existsSync } from "fs";
import { unlink } from "fs/promises";
import path from "path";
import db from "@/lib/db";
export async function GET(request, { params }) {
try {
const fileId = params.fileId;
// Get file info from database
const file = db.prepare(`
SELECT * FROM file_attachments WHERE file_id = ?
`).get(parseInt(fileId));
if (!file) {
return NextResponse.json(
{ error: "File not found" },
{ status: 404 }
);
}
// Construct the full file path
const fullPath = path.join(process.cwd(), "public", file.file_path);
// Check if file exists
if (!existsSync(fullPath)) {
return NextResponse.json(
{ error: "File not found on disk" },
{ status: 404 }
);
}
// Read the file
const fileBuffer = await readFile(fullPath);
// Return the file with appropriate headers
return new NextResponse(fileBuffer, {
headers: {
"Content-Type": file.mime_type || "application/octet-stream",
"Content-Disposition": `attachment; filename="${encodeURIComponent(file.original_filename)}"`,
"Content-Length": fileBuffer.length.toString(),
},
});
} catch (error) {
console.error("Error downloading file:", error);
return NextResponse.json(
{ error: "Failed to download file" },
{ status: 500 }
);
}
}
export async function DELETE(request, { params }) {
try {
const fileId = params.fileId;

View File

@@ -149,7 +149,7 @@ export default function FileAttachmentsList({ entityType, entityId, onFilesChang
</div>
<div className="flex items-center gap-2 ml-3">
<a
href={file.file_path}
href={`/api/files/${file.file_id}`}
target="_blank"
rel="noopener noreferrer"
className="text-blue-600 hover:text-blue-800"

8
uploads/README.md Normal file
View File

@@ -0,0 +1,8 @@
# File Uploads Directory
This directory contains uploaded files organized by entity type.
## Structure:
- contracts/ - Contract attachments
- projects/ - Project attachments
- tasks/ - Task attachments

View File

View File

0
uploads/tasks/.gitkeep Normal file
View File