-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (118 loc) · 5.11 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const express = require('express');
const multer = require('multer');
const path = require('path');
const { exec } = require('child_process');
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, uuidv4() + path.extname(file.originalname)); // UUID দ্বারা ফাইল নাম তৈরী হবে
}
});
const upload = multer({ storage: storage });
app.use(express.static('public'));
// Sequential processing queue
let processingQueue = Promise.resolve();
// Process file upload
const processFileUpload = async (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
const svgFilePath = path.join(__dirname, 'uploads', req.file.filename);
const outputDir = path.join(__dirname, 'uploads');
const outputFileName = req.file.filename.replace('.svg', '.xml');
const outputFilePath = path.join(outputDir, outputFileName);
const heightDp = req.body.height ? `-heightDp ${req.body.height}` : '';
const widthDp = req.body.width ? `-widthDp ${req.body.width}` : '';
const command = `vd-tool -c -in ${svgFilePath} -out ${outputDir} ${heightDp} ${widthDp}`;
try {
await new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error during conversion: ${error.message}`);
reject('Error during conversion.');
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
resolve();
});
});
fs.readFile(outputFilePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the converted file:', err);
return res.status(500).send('Error reading the converted file.');
}
res.send(data);
});
} catch (error) {
console.error('Conversion failed:', error);
res.status(500).send('Conversion failed.');
}
};
// Endpoint to handle upload
app.post('/upload', upload.single('svg-file'), (req, res) => {
// Store the file upload time as metadata in a JSON file
const uploadTime = Date.now();
const fileMetadata = {
uuid: req.file.filename, // UUID filename
uploadTime: uploadTime, // Store the upload time
};
// Save metadata to a JSON file in the uploads directory
const metadataFilePath = path.join(__dirname, 'uploads', req.file.filename + '.json');
fs.writeFileSync(metadataFilePath, JSON.stringify(fileMetadata));
processingQueue = processingQueue.then(() => processFileUpload(req, res));
});
// Function to delete files older than 10 minutes
const deleteOldFiles = () => {
const uploadsDir = path.join(__dirname, 'uploads');
const TEN_MINUTES = 10 * 60 * 1000; // 10 minutes in milliseconds
fs.readdir(uploadsDir, (err, files) => {
if (err) return console.error('Error reading directory:', err);
files.forEach(file => {
const filePath = path.join(uploadsDir, file);
// Only process the JSON metadata files and the original files (not directories)
if (file.endsWith('.json')) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading metadata file:', err);
return;
}
const metadata = JSON.parse(data);
const currentTime = Date.now();
// Check if the file is older than 10 minutes
if (currentTime - metadata.uploadTime > TEN_MINUTES) {
// Delete the original file
const originalFilePath = path.join(uploadsDir, metadata.uuid);
fs.unlink(originalFilePath, (err) => {
if (err) console.error(`Error deleting old file: ${originalFilePath}`, err);
else console.log(`Deleted old file: ${originalFilePath}`);
});
// Delete the associated metadata file
fs.unlink(filePath, (err) => {
if (err) console.error(`Error deleting metadata file: ${filePath}`, err);
else console.log(`Deleted metadata file: ${filePath}`);
});
}
});
}
});
});
};
// Run deleteOldFiles function every 10 minutes
setInterval(deleteOldFiles, 10 * 60 * 1000);
// Add '/clear' route to manually delete old files
app.post('/clear', (req, res) => {
deleteOldFiles(); // Call the function to delete old files
res.send('Old files cleared successfully');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});