job description file upload
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
'use server';
|
||||
import { db, companies, jobs } from '@workspace/db';
|
||||
import { writeFile, mkdir } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
export async function createJob(formData: FormData) {
|
||||
const companyIdRaw = formData.get('companyId');
|
||||
const companyId = companyIdRaw ? Number(companyIdRaw) : undefined;
|
||||
const title = String(formData.get('title') ?? '').trim();
|
||||
const link = String(formData.get('link') ?? '').trim();
|
||||
const description = String(formData.get('description') ?? '').trim() || 'N/A';
|
||||
const description = String(formData.get('description') ?? '').trim() || '';
|
||||
const location = String(formData.get('location') ?? '').trim() || 'N/A';
|
||||
const imageURL =
|
||||
String(formData.get('imageURL') ?? '').trim() || 'https://via.placeholder.com/100x100?text=Job';
|
||||
@@ -19,13 +21,49 @@ export async function createJob(formData: FormData) {
|
||||
const allowDeadKT = formData.get('allowDeadKT') === 'on' || formData.get('allowDeadKT') === 'true';
|
||||
const allowLiveKT = formData.get('allowLiveKT') === 'on' || formData.get('allowLiveKT') === 'true';
|
||||
|
||||
// Handle file upload
|
||||
const descriptionFile = formData.get('descriptionFile') as File | null;
|
||||
const fileType = formData.get('fileType') as string | null;
|
||||
|
||||
let fileUrl: string | null = null;
|
||||
let fileName: string | null = null;
|
||||
|
||||
if (descriptionFile && descriptionFile.size > 0) {
|
||||
try {
|
||||
// Create uploads directory if it doesn't exist
|
||||
const uploadsDir = join(process.cwd(), 'public', 'uploads', 'job-descriptions');
|
||||
await mkdir(uploadsDir, { recursive: true });
|
||||
|
||||
// Generate unique filename
|
||||
const timestamp = Date.now();
|
||||
const originalName = descriptionFile.name.replace(/[^a-zA-Z0-9.-]/g, '_');
|
||||
fileName = `${timestamp}_${originalName}`;
|
||||
const filePath = join(uploadsDir, fileName);
|
||||
|
||||
// Write file to disk
|
||||
const bytes = await descriptionFile.arrayBuffer();
|
||||
await writeFile(filePath, Buffer.from(bytes));
|
||||
|
||||
// Set file URL for database
|
||||
fileUrl = `/uploads/job-descriptions/${fileName}`;
|
||||
} catch (error) {
|
||||
console.error('Error uploading file:', error);
|
||||
return { error: 'Failed to upload description file.' };
|
||||
}
|
||||
}
|
||||
|
||||
if (!companyId || !title) return { error: 'Company and title are required.' };
|
||||
|
||||
// Either description text OR file is required
|
||||
if (!description && !descriptionFile) {
|
||||
return { error: 'Either description text or description file is required.' };
|
||||
}
|
||||
|
||||
await db.insert(jobs).values({
|
||||
companyId,
|
||||
title,
|
||||
link,
|
||||
description,
|
||||
description: description || null,
|
||||
location,
|
||||
imageURL,
|
||||
salary,
|
||||
@@ -36,18 +74,20 @@ export async function createJob(formData: FormData) {
|
||||
minHSC,
|
||||
allowDeadKT,
|
||||
allowLiveKT,
|
||||
fileType: fileType || null,
|
||||
fileUrl: fileUrl || null,
|
||||
fileName: fileName || null,
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function createCompany(formData: FormData) {
|
||||
const name = String(formData.get('name') ?? '').trim();
|
||||
const email = String(formData.get('email') ?? '').trim();
|
||||
const link = String(formData.get('link') ?? '').trim();
|
||||
const description = String(formData.get('description') ?? '').trim();
|
||||
const imageURL = String(formData.get('imageURL') ?? '').trim() || 'https://via.placeholder.com/100x100?text=Company';
|
||||
if (!name || !email || !link || !description) return { error: 'All fields are required.' };
|
||||
const [inserted] = await db.insert(companies).values({ name, email, link, description, imageURL }).returning();
|
||||
if (!name || !link || !description) return { error: 'Name, link, and description are required.' };
|
||||
const [inserted] = await db.insert(companies).values({ name, link, description, imageURL }).returning();
|
||||
if (!inserted) return { error: 'Failed to add company.' };
|
||||
return { success: true, company: { id: inserted.id, name: inserted.name } };
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ import {
|
||||
CheckCircle,
|
||||
AlertCircle,
|
||||
LinkIcon,
|
||||
Upload,
|
||||
FileText,
|
||||
X,
|
||||
} from "lucide-react"
|
||||
import { cn } from "@workspace/ui/lib/utils"
|
||||
import { Alert, AlertDescription } from "@workspace/ui/components/alert"
|
||||
@@ -62,6 +65,8 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
minHSC: 0,
|
||||
allowDeadKT: true,
|
||||
allowLiveKT: true,
|
||||
fileType: undefined,
|
||||
descriptionFile: undefined,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -74,7 +79,7 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
formData.append('companyId', String(data.companyId))
|
||||
formData.append('title', data.title)
|
||||
formData.append('link', data.link)
|
||||
formData.append('description', data.description)
|
||||
formData.append('description', data.description || '')
|
||||
formData.append('location', data.location)
|
||||
formData.append('imageURL', data.imageURL || '')
|
||||
formData.append('salary', data.salary)
|
||||
@@ -89,6 +94,12 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
formData.append('allowDeadKT', String(data.allowDeadKT))
|
||||
formData.append('allowLiveKT', String(data.allowLiveKT))
|
||||
|
||||
// Handle file upload
|
||||
if (data.descriptionFile) {
|
||||
formData.append('descriptionFile', data.descriptionFile)
|
||||
formData.append('fileType', data.fileType || (data.descriptionFile.type === 'application/pdf' ? 'pdf' : 'text'))
|
||||
}
|
||||
|
||||
const result = await createJob(formData)
|
||||
if (result?.success) {
|
||||
setSuccess(true)
|
||||
@@ -412,23 +423,102 @@ function NewJobForm({ companies }: { companies: { id: number; name: string }[] }
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Job Description *</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Describe the role, responsibilities, and requirements..."
|
||||
{...field}
|
||||
className="min-h-[120px] resize-none"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{/* Job Description Section - Text or File */}
|
||||
<div className="space-y-4">
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Job Description *</FormLabel>
|
||||
<p className="text-sm text-gray-600">Provide either a text description or upload a PDF/text file</p>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Text Description</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Describe the role, responsibilities, and requirements..."
|
||||
{...field}
|
||||
className="min-h-[120px] resize-none"
|
||||
disabled={!!form.watch('descriptionFile')}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="flex items-center justify-center">
|
||||
<div className="flex-1 border-t border-gray-300"></div>
|
||||
<div className="px-3 text-sm text-gray-500 bg-white">OR</div>
|
||||
<div className="flex-1 border-t border-gray-300"></div>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="descriptionFile"
|
||||
render={({ field: { onChange, value, ...field } }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-gray-700">Upload Description File</FormLabel>
|
||||
<FormControl>
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-center w-full">
|
||||
<label className="flex flex-col items-center justify-center w-full h-32 border-2 border-gray-300 border-dashed rounded-lg cursor-pointer bg-gray-50 hover:bg-gray-100">
|
||||
<div className="flex flex-col items-center justify-center pt-5 pb-6">
|
||||
<Upload className="w-8 h-8 mb-4 text-gray-500" />
|
||||
<p className="mb-2 text-sm text-gray-500">
|
||||
<span className="font-semibold">Click to upload</span> or drag and drop
|
||||
</p>
|
||||
<p className="text-xs text-gray-500">PDF or TXT (MAX. 5MB)</p>
|
||||
</div>
|
||||
<input
|
||||
type="file"
|
||||
accept=".pdf,.txt,application/pdf,text/plain"
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) {
|
||||
onChange(file)
|
||||
// Auto-detect file type
|
||||
const fileType = file.type === 'application/pdf' ? 'pdf' : 'text'
|
||||
form.setValue('fileType', fileType)
|
||||
// Clear text description
|
||||
form.setValue('description', '')
|
||||
}
|
||||
}}
|
||||
disabled={!!form.watch('description')?.trim()}
|
||||
{...field}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{value && (
|
||||
<div className="flex items-center gap-2 p-3 bg-blue-50 rounded-lg border border-blue-200">
|
||||
<FileText className="w-4 h-4 text-blue-600" />
|
||||
<span className="text-sm text-blue-800 flex-1">{value.name}</span>
|
||||
<span className="text-xs text-blue-600">
|
||||
{(value.size / 1024 / 1024).toFixed(2)} MB
|
||||
</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
onChange(undefined)
|
||||
form.setValue('fileType', undefined)
|
||||
}}
|
||||
className="text-blue-600 hover:text-blue-800"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Academic Requirements Section */}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
const ACCEPTED_FILE_TYPES = ['application/pdf', 'text/plain'];
|
||||
|
||||
export const jobSchema = z.object({
|
||||
companyId: z.number().min(1, 'Company is required'),
|
||||
title: z.string().min(1, 'Title is required'),
|
||||
link: z.string().url('Invalid URL'),
|
||||
description: z.string().min(1, 'Description is required'),
|
||||
description: z.string().optional(), // Made optional since file can replace it
|
||||
location: z.string().min(1, 'Location is required'),
|
||||
imageURL: z.string().url('Invalid URL').or(z.literal('')).optional(),
|
||||
salary: z.string().min(1, 'Salary is required'),
|
||||
@@ -14,6 +17,21 @@ export const jobSchema = z.object({
|
||||
minHSC: z.coerce.number().min(0, 'Minimum HSC must be 0 or greater'),
|
||||
allowDeadKT: z.boolean(),
|
||||
allowLiveKT: z.boolean(),
|
||||
});
|
||||
// File upload fields
|
||||
fileType: z.enum(['pdf', 'text']).optional(),
|
||||
descriptionFile: z.instanceof(File)
|
||||
.refine(file => file?.size <= MAX_FILE_SIZE, 'File size must be less than 5MB')
|
||||
.refine(file => ACCEPTED_FILE_TYPES.includes(file?.type), 'Only PDF and text files are allowed')
|
||||
.optional(),
|
||||
}).refine(
|
||||
(data) => {
|
||||
// Either description text OR file is required
|
||||
return (data.description && data.description.trim().length > 0) || data.descriptionFile;
|
||||
},
|
||||
{
|
||||
message: 'Either provide a text description or upload a description file',
|
||||
path: ['description'],
|
||||
}
|
||||
);
|
||||
|
||||
export type JobFormData = z.infer<typeof jobSchema>;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { filename: string } }
|
||||
) {
|
||||
try {
|
||||
const filename = params.filename;
|
||||
|
||||
// Security check - prevent directory traversal
|
||||
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
|
||||
return NextResponse.json({ error: 'Invalid filename' }, { status: 400 });
|
||||
}
|
||||
|
||||
const filePath = join(process.cwd(), 'public', 'uploads', 'job-descriptions', filename);
|
||||
|
||||
try {
|
||||
const fileBuffer = await readFile(filePath);
|
||||
|
||||
// Determine content type based on file extension
|
||||
const ext = filename.split('.').pop()?.toLowerCase();
|
||||
let contentType = 'application/octet-stream';
|
||||
|
||||
if (ext === 'pdf') {
|
||||
contentType = 'application/pdf';
|
||||
} else if (ext === 'txt') {
|
||||
contentType = 'text/plain';
|
||||
}
|
||||
|
||||
// Set appropriate headers
|
||||
const response = new NextResponse(new Uint8Array(fileBuffer));
|
||||
response.headers.set('Content-Type', contentType);
|
||||
response.headers.set('Content-Disposition', `inline; filename="${filename}"`);
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'File not found' }, { status: 404 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error serving file:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ import {
|
||||
ExternalLink,
|
||||
Loader2,
|
||||
AlertCircle,
|
||||
FileText,
|
||||
Download,
|
||||
} from 'lucide-react';
|
||||
import JobApplicationModal from '@/components/job-application-modal';
|
||||
import { type InferSelectModel } from '@workspace/db/drizzle';
|
||||
@@ -42,6 +44,68 @@ export type Job = InferSelectModel<typeof jobs> & {
|
||||
|
||||
export type Resume = typeof resumes.$inferSelect;
|
||||
|
||||
const JobDescription = ({ job }: { job: Job }) => {
|
||||
if (job.fileUrl && job.fileName) {
|
||||
const apiFileUrl = `/api/files/job-descriptions/${job.fileName}`;
|
||||
// Display file information
|
||||
return (
|
||||
<div className="mb-4">
|
||||
<div className="flex items-center gap-3 p-3 bg-blue-50 rounded-lg border border-blue-200">
|
||||
<FileText className="w-5 h-5 text-blue-600" />
|
||||
<div className="flex-1">
|
||||
<p className="text-sm font-medium text-blue-800">Job Description File</p>
|
||||
<p className="text-xs text-blue-600">{job.fileName}</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{job.fileType === 'pdf' ? (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="text-blue-600 border-blue-300 hover:bg-blue-100"
|
||||
onClick={() => window.open(apiFileUrl, '_blank')}
|
||||
>
|
||||
<Eye className="w-4 h-4 mr-1" />
|
||||
View PDF
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="text-blue-600 border-blue-300 hover:bg-blue-100"
|
||||
onClick={() => window.open(apiFileUrl, '_blank')}
|
||||
>
|
||||
<FileText className="w-4 h-4 mr-1" />
|
||||
View File
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="text-blue-600 hover:bg-blue-100"
|
||||
onClick={() => {
|
||||
const link = document.createElement('a');
|
||||
link.href = apiFileUrl;
|
||||
link.download = job.fileName!;
|
||||
link.click();
|
||||
}}
|
||||
>
|
||||
<Download className="w-4 h-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Display text description
|
||||
if (job.description) {
|
||||
return <p className="text-gray-600 text-sm mb-4 line-clamp-2">{job.description}</p>;
|
||||
}
|
||||
|
||||
// Fallback
|
||||
return <p className="text-gray-500 text-sm mb-4 italic">No description available</p>;
|
||||
};
|
||||
|
||||
export default function JobsPage({
|
||||
eligibleJobs,
|
||||
ineligibleJobs,
|
||||
@@ -77,7 +141,8 @@ export default function JobsPage({
|
||||
(job) =>
|
||||
job.title.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
job.company.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
job.description.toLowerCase().includes(searchTerm.toLowerCase()),
|
||||
(job.description && job.description.toLowerCase().includes(searchTerm.toLowerCase())) ||
|
||||
(job.fileName && job.fileName.toLowerCase().includes(searchTerm.toLowerCase())),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -324,7 +389,7 @@ export default function JobsPage({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="text-gray-600 text-sm mb-4 line-clamp-2">{job.description}</p>
|
||||
<JobDescription job={job} />
|
||||
|
||||
<div className="grid grid-cols-2 gap-4 mb-4">
|
||||
<div className="flex items-center gap-2 text-sm text-gray-600">
|
||||
@@ -341,7 +406,7 @@ export default function JobsPage({
|
||||
<div className="flex gap-2">
|
||||
{activeTab === 'eligible' ? (
|
||||
<JobApplicationModal
|
||||
job={{ ...job, minCGPA: Number(job.minCGPA) }}
|
||||
job={job}
|
||||
studentId={studentId}
|
||||
resumes={resumes}
|
||||
isApplied={appliedJobIds.includes(job.id)}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { filename: string } }
|
||||
) {
|
||||
try {
|
||||
const filename = params.filename;
|
||||
|
||||
// Security check - prevent directory traversal
|
||||
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
|
||||
return NextResponse.json({ error: 'Invalid filename' }, { status: 400 });
|
||||
}
|
||||
|
||||
const filePath = join(process.cwd(), 'public', 'uploads', 'job-descriptions', filename);
|
||||
|
||||
try {
|
||||
const fileBuffer = await readFile(filePath);
|
||||
|
||||
// Determine content type based on file extension
|
||||
const ext = filename.split('.').pop()?.toLowerCase();
|
||||
let contentType = 'application/octet-stream';
|
||||
|
||||
if (ext === 'pdf') {
|
||||
contentType = 'application/pdf';
|
||||
} else if (ext === 'txt') {
|
||||
contentType = 'text/plain';
|
||||
}
|
||||
|
||||
// Set appropriate headers
|
||||
const response = new NextResponse(new Uint8Array(fileBuffer));
|
||||
response.headers.set('Content-Type', contentType);
|
||||
response.headers.set('Content-Disposition', `inline; filename="${filename}"`);
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'File not found' }, { status: 404 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error serving file:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ export type Job = InferSelectModel<typeof jobs> & {
|
||||
export type Resume = typeof resumes.$inferSelect;
|
||||
|
||||
interface JobApplicationModalProps {
|
||||
job: Job & { minCGPA: number };
|
||||
job: Job;
|
||||
studentId: number;
|
||||
resumes: Resume[];
|
||||
isApplied?: boolean;
|
||||
|
||||
Reference in New Issue
Block a user