feat: Implement file upload structure and API for file retrieval
This commit is contained in:
@@ -13,6 +13,7 @@ services:
|
|||||||
- "3001:3000"
|
- "3001:3000"
|
||||||
volumes:
|
volumes:
|
||||||
- ./data:/app/data
|
- ./data:/app/data
|
||||||
|
- ./uploads:/app/public/uploads
|
||||||
environment:
|
environment:
|
||||||
- NODE_ENV=production
|
- NODE_ENV=production
|
||||||
- NEXTAUTH_SECRET=${NEXTAUTH_SECRET:-your-secret-key-generate-a-strong-random-string-at-least-32-characters}
|
- NEXTAUTH_SECRET=${NEXTAUTH_SECRET:-your-secret-key-generate-a-strong-random-string-at-least-32-characters}
|
||||||
|
|||||||
@@ -8,6 +8,14 @@ echo "🚀 Starting development environment..."
|
|||||||
# Ensure data directory exists
|
# Ensure data directory exists
|
||||||
mkdir -p /app/data
|
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
|
# Create admin account if it doesn't exist
|
||||||
echo "🔧 Setting up admin account..."
|
echo "🔧 Setting up admin account..."
|
||||||
node scripts/create-admin.js
|
node scripts/create-admin.js
|
||||||
|
|||||||
@@ -8,6 +8,14 @@ echo "🚀 Starting application..."
|
|||||||
# Ensure data directory exists
|
# Ensure data directory exists
|
||||||
mkdir -p /app/data
|
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
|
# Create admin account if it doesn't exist
|
||||||
echo "🔧 Setting up admin account..."
|
echo "🔧 Setting up admin account..."
|
||||||
node scripts/create-admin.js
|
node scripts/create-admin.js
|
||||||
|
|||||||
@@ -1,8 +1,58 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { readFile } from "fs/promises";
|
||||||
|
import { existsSync } from "fs";
|
||||||
import { unlink } from "fs/promises";
|
import { unlink } from "fs/promises";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import db from "@/lib/db";
|
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 }) {
|
export async function DELETE(request, { params }) {
|
||||||
try {
|
try {
|
||||||
const fileId = params.fileId;
|
const fileId = params.fileId;
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ export default function FileAttachmentsList({ entityType, entityId, onFilesChang
|
|||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2 ml-3">
|
<div className="flex items-center gap-2 ml-3">
|
||||||
<a
|
<a
|
||||||
href={file.file_path}
|
href={`/api/files/${file.file_id}`}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
className="text-blue-600 hover:text-blue-800"
|
className="text-blue-600 hover:text-blue-800"
|
||||||
|
|||||||
8
uploads/README.md
Normal file
8
uploads/README.md
Normal 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
|
||||||
0
uploads/contracts/.gitkeep
Normal file
0
uploads/contracts/.gitkeep
Normal file
0
uploads/projects/.gitkeep
Normal file
0
uploads/projects/.gitkeep
Normal file
0
uploads/tasks/.gitkeep
Normal file
0
uploads/tasks/.gitkeep
Normal file
Reference in New Issue
Block a user