Spaces:
Running
Running
Download server.js from ekonkati/pdf-upload-wizard: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/ekonkati/pdf-upload-wizard/resolve/main/server.js
- Command line
-
hf download hf://spaces/ekonkati/pdf-upload-wizard/server.js
-
curl -L -o server.js https://huggingface.co/spaces/ekonkati/pdf-upload-wizard/resolve/main/server.js
2.78 kB
| const express = require('express'); | |
| const multer = require('multer'); | |
| const path = require('path'); | |
| const fs = require('fs'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 3000; | |
| // Middleware | |
| app.use(cors()); | |
| app.use(express.json()); | |
| app.use(express.static('public')); | |
| // Configure multer for file uploads | |
| const storage = multer.diskStorage({ | |
| destination: (req, file, cb) => { | |
| const uploadDir = 'uploads/'; | |
| if (!fs.existsSync(uploadDir)) { | |
| fs.mkdirSync(uploadDir); | |
| } | |
| cb(null, uploadDir); | |
| }, | |
| filename: (req, file, cb) => { | |
| cb(null, Date.now() + path.extname(file.originalname)); | |
| } | |
| }); | |
| const upload = multer({ | |
| storage: storage, | |
| limits: { fileSize: 10 * 1024 * 1024 }, // 10MB limit | |
| fileFilter: (req, file, cb) => { | |
| if (file.mimetype === 'application/pdf') { | |
| cb(null, true); | |
| } else { | |
| cb(new Error('Only PDF files are allowed'), false); | |
| } | |
| } | |
| }); | |
| // In-memory "database" for demo purposes | |
| let documents = [ | |
| { | |
| id: '1', | |
| name: 'Product_Requirements.pdf', | |
| originalName: 'Product_Requirements.pdf', | |
| size: 2400000, | |
| uploadDate: new Date(), | |
| status: 'processed', | |
| path: 'uploads/sample.pdf' | |
| } | |
| ]; | |
| // API Routes | |
| app.get('/api/documents', (req, res) => { | |
| res.json(documents.map(doc => ({ | |
| id: doc.id, | |
| name: doc.name, | |
| size: doc.size, | |
| uploadDate: doc.uploadDate, | |
| status: doc.status | |
| }))); | |
| }); | |
| app.post('/api/upload', upload.single('pdf'), (req, res) => { | |
| if (!req.file) { | |
| return res.status(400).json({ error: 'No file uploaded' }); | |
| } | |
| const newDoc = { | |
| id: Date.now().toString(), | |
| name: req.body.name || req.file.originalname, | |
| originalName: req.file.originalname, | |
| size: req.file.size, | |
| uploadDate: new Date(), | |
| status: 'uploaded', | |
| path: req.file.path | |
| }; | |
| documents.unshift(newDoc); | |
| res.status(201).json({ | |
| id: newDoc.id, | |
| name: newDoc.name, | |
| size: newDoc.size, | |
| uploadDate: newDoc.uploadDate, | |
| status: newDoc.status | |
| }); | |
| }); | |
| app.delete('/api/documents/:id', (req, res) => { | |
| const docIndex = documents.findIndex(d => d.id === req.params.id); | |
| if (docIndex === -1) { | |
| return res.status(404).json({ error: 'Document not found' }); | |
| } | |
| const doc = documents[docIndex]; | |
| try { | |
| fs.unlinkSync(doc.path); | |
| } catch (err) { | |
| console.error('Error deleting file:', err); | |
| } | |
| documents.splice(docIndex, 1); | |
| res.status(204).end(); | |
| }); | |
| // Start server | |
| app.listen(PORT, () => { | |
| console.log(`Server running on http://localhost:${PORT}`); | |
| }); |