Finalize form display

This commit is contained in:
2024-09-26 18:14:04 -04:00
parent 6584a48f27
commit be56ead43f
7 changed files with 73 additions and 24 deletions

View File

@@ -12,12 +12,32 @@ export async function GET(
return new NextResponse('Unauthorized', { status: 401 });
}
// Construct the file path relative to the project root
const filePath = path.join(process.cwd(), 'content', ...params.path);
console.log('Attempting to read file:', filePath); // Add this log
try {
const file = await fs.readFile(filePath);
const response = new NextResponse(file);
response.headers.set('Content-Type', 'application/pdf');
// Determine content type based on file extension
const ext = path.extname(filePath).toLowerCase();
switch (ext) {
case '.pdf':
response.headers.set('Content-Type', 'application/pdf');
break;
case '.png':
response.headers.set('Content-Type', 'image/png');
break;
case '.jpg':
case '.jpeg':
response.headers.set('Content-Type', 'image/jpeg');
break;
default:
response.headers.set('Content-Type', 'application/octet-stream');
}
return response;
} catch (error) {
console.error('Error reading file:', error);

View File

@@ -26,6 +26,7 @@ export async function DELETE(
.select({
contentId: informedConsentForms.contentId,
location: contents.location,
previewLocation: contents.previewLocation,
})
.from(informedConsentForms)
.innerJoin(contents, eq(informedConsentForms.contentId, contents.id))
@@ -35,14 +36,21 @@ export async function DELETE(
return NextResponse.json({ error: 'Form not found' }, { status: 404 });
}
// Delete the file from the file system
const fullPath = path.join(process.cwd(), form.location);
// Delete the file and preview from the file system
const fullPath = path.join(process.cwd(), form.location ?? '');
const previewPath = path.join(process.cwd(), form.previewLocation ?? '');
try {
await fs.access(fullPath);
await fs.unlink(fullPath);
} catch (error) {
console.warn(`File not found or couldn't be deleted: ${fullPath}`);
}
try {
await fs.access(previewPath);
await fs.unlink(previewPath);
} catch (error) {
console.warn(`Preview file not found or couldn't be deleted: ${previewPath}`);
}
// Delete the form and content from the database
await db.transaction(async (tx) => {
@@ -50,7 +58,7 @@ export async function DELETE(
await tx.delete(contents).where(eq(contents.id, form.contentId));
});
return NextResponse.json({ message: "Form deleted successfully" });
return NextResponse.json({ message: "Form and preview deleted successfully" });
} catch (error) {
console.error('Error deleting form:', error);
return NextResponse.json({ error: 'Failed to delete form' }, { status: 500 });

View File

@@ -5,7 +5,7 @@ import { auth } from "@clerk/nextjs/server";
import { eq } from "drizzle-orm";
import { saveFile } from "~/lib/fileStorage";
import fs from 'fs/promises';
import { studies, participants } from "~/server/db/schema";
// Function to generate a random string
const generateRandomString = (length: number) => {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
@@ -28,10 +28,14 @@ export async function GET(request: Request) {
location: contents.location,
previewLocation: contents.previewLocation,
studyId: informedConsentForms.studyId,
studyTitle: studies.title,
participantId: informedConsentForms.participantId,
participantName: participants.name,
contentId: informedConsentForms.contentId,
}).from(informedConsentForms)
.innerJoin(contents, eq(informedConsentForms.contentId, contents.id));
.innerJoin(contents, eq(informedConsentForms.contentId, contents.id))
.innerJoin(studies, eq(informedConsentForms.studyId, studies.id))
.innerJoin(participants, eq(informedConsentForms.participantId, participants.id));
return NextResponse.json(forms);
}