fix: update file path handling for template file operations
This commit is contained in:
@@ -58,7 +58,7 @@ export async function PUT(request, { params }) {
|
|||||||
|
|
||||||
// Delete old file
|
// Delete old file
|
||||||
try {
|
try {
|
||||||
const oldFilePath = path.join(process.cwd(), "public", existingTemplate.file_path);
|
const oldFilePath = path.join(process.cwd(), existingTemplate.file_path);
|
||||||
await unlink(oldFilePath);
|
await unlink(oldFilePath);
|
||||||
} catch (fileError) {
|
} catch (fileError) {
|
||||||
console.warn("Could not delete old template file:", fileError);
|
console.warn("Could not delete old template file:", fileError);
|
||||||
@@ -67,10 +67,10 @@ export async function PUT(request, { params }) {
|
|||||||
// Save new file
|
// Save new file
|
||||||
const fileExtension = path.extname(file.name);
|
const fileExtension = path.extname(file.name);
|
||||||
const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}${fileExtension}`;
|
const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}${fileExtension}`;
|
||||||
const filePath = path.join(process.cwd(), "public", "templates", fileName);
|
const filePath = path.join(process.cwd(), "templates", fileName);
|
||||||
|
|
||||||
// Ensure templates directory exists
|
// Ensure templates directory exists
|
||||||
const templatesDir = path.join(process.cwd(), "public", "templates");
|
const templatesDir = path.join(process.cwd(), "templates");
|
||||||
try {
|
try {
|
||||||
await fs.promises.access(templatesDir);
|
await fs.promises.access(templatesDir);
|
||||||
} catch {
|
} catch {
|
||||||
@@ -80,7 +80,7 @@ export async function PUT(request, { params }) {
|
|||||||
const buffer = Buffer.from(await file.arrayBuffer());
|
const buffer = Buffer.from(await file.arrayBuffer());
|
||||||
await fs.promises.writeFile(filePath, buffer);
|
await fs.promises.writeFile(filePath, buffer);
|
||||||
|
|
||||||
updateData.file_path = `/templates/${fileName}`;
|
updateData.file_path = `templates/${fileName}`;
|
||||||
updateData.original_filename = file.name;
|
updateData.original_filename = file.name;
|
||||||
updateData.file_size = file.size;
|
updateData.file_size = file.size;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ export async function POST(request) {
|
|||||||
`).all(projectId);
|
`).all(projectId);
|
||||||
|
|
||||||
// Load template file
|
// Load template file
|
||||||
const templatePath = path.join(process.cwd(), "public", template.file_path);
|
const templatePath = path.join(process.cwd(), template.file_path);
|
||||||
const templateContent = await readFile(templatePath);
|
const templateContent = await readFile(templatePath);
|
||||||
|
|
||||||
// Load the docx file as a binary
|
// Load the docx file as a binary
|
||||||
|
|||||||
Reference in New Issue
Block a user