feat: implement template storage and download functionality with Docker support
This commit is contained in:
61
src/app/api/templates/download/[filename]/route.js
Normal file
61
src/app/api/templates/download/[filename]/route.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { readFile } from "fs/promises";
|
||||
import { existsSync } from "fs";
|
||||
import path from "path";
|
||||
import db from "@/lib/db";
|
||||
|
||||
export async function GET(request, { params }) {
|
||||
try {
|
||||
const { filename } = params;
|
||||
|
||||
if (!filename) {
|
||||
return NextResponse.json(
|
||||
{ error: "Filename is required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get template info from database
|
||||
const template = db.prepare(`
|
||||
SELECT * FROM docx_templates WHERE stored_filename = ? AND is_active = 1
|
||||
`).get(filename);
|
||||
|
||||
if (!template) {
|
||||
return NextResponse.json(
|
||||
{ error: "Template not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
const filePath = path.join(process.cwd(), "templates", filename);
|
||||
if (!existsSync(filePath)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Template file not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Read file
|
||||
const fileBuffer = await readFile(filePath);
|
||||
|
||||
// Return file with proper headers
|
||||
const response = new NextResponse(fileBuffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": template.mime_type || "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"Content-Disposition": `attachment; filename="${template.original_filename}"`,
|
||||
"Content-Length": fileBuffer.length.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
return response;
|
||||
|
||||
} catch (error) {
|
||||
console.error("Template download error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to download template" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { existsSync } from "fs";
|
||||
import path from "path";
|
||||
import db from "@/lib/db";
|
||||
|
||||
const TEMPLATES_DIR = path.join(process.cwd(), "public", "templates");
|
||||
const TEMPLATES_DIR = path.join(process.cwd(), "templates");
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
||||
const ALLOWED_TYPES = [
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
@@ -49,7 +49,7 @@ export async function POST(request) {
|
||||
const sanitizedOriginalName = file.name.replace(/[^a-zA-Z0-9.-]/g, "_");
|
||||
const storedFilename = `${timestamp}_${sanitizedOriginalName}`;
|
||||
const filePath = path.join(TEMPLATES_DIR, storedFilename);
|
||||
const relativePath = `/templates/${storedFilename}`;
|
||||
const relativePath = `templates/${storedFilename}`;
|
||||
|
||||
// Save file
|
||||
const bytes = await file.arrayBuffer();
|
||||
|
||||
@@ -68,7 +68,7 @@ export default function ProjectListPage() {
|
||||
// Apply status filter
|
||||
if (filters.status !== 'all') {
|
||||
if (filters.status === 'not_finished') {
|
||||
filtered = filtered.filter(project => project.project_status !== 'fulfilled');
|
||||
filtered = filtered.filter(project => project.project_status !== 'fulfilled' && project.project_status !== 'cancelled');
|
||||
} else {
|
||||
filtered = filtered.filter(project => project.project_status === filters.status);
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ export default function TemplateList({ templates, onTemplateDeleted, onTemplateU
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(template.file_path, "_blank")}
|
||||
onClick={() => window.open(`/api/templates/download/${template.stored_filename}`, "_blank")}
|
||||
>
|
||||
Pobierz
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user