-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (83 loc) · 3.19 KB
/
app.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
const express = require("express");
const multer = require("multer");
const sharp = require("sharp");
const fs = require("fs");
const archiver = require("archiver");
const path = require("path");
const app = express();
const port = 3000;
// Configure multer for image uploads
const upload = multer({ dest: "uploads/" });
// Serve static files from the `public` directory
app.use(express.static("public"));
// Serve HTML form at root
app.get("/", (req, res) => {
const indexPath = path.join(__dirname, "index.html"); // Adjust the path according to your file structure
fs.readFile(indexPath, "utf8", (err, html) => {
if (err) {
res.status(500).send("Sorry, something went wrong");
console.error(err);
return;
}
res.send(html);
});
});
// handle image upload
app.post("/upload", upload.array("images"), async (req, res) => {
// Extract color from form data, default to '#808080' if not provided
const customColor = req.body.color || "#808080";
const output = fs.createWriteStream("placeholders.zip");
const archive = archiver("zip", { zlib: { level: 9 } });
output.on("close", () => {
res.download("placeholders.zip", "placeholders.zip", (err) => {
if (err) {
console.error("Error sending file", err);
}
fs.unlinkSync("placeholders.zip"); // Clean up zip file after sending
});
});
archive.on("error", (err) => {
throw err;
});
archive.pipe(output);
for (const file of req.files) {
const metadata = await sharp(file.path).metadata();
const dimensionsText = `${metadata.width}p x ${metadata.height}p`;
// Calculate font size as 30% of image width
const fontSize = metadata.width * 0.1;
const svg = `
<svg width="${metadata.width}" height="${metadata.height}" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="${customColor}"/>
<text x="50%" y="50%" alignment-baseline="middle" text-anchor="middle" font-family="sans-serif" font-size="${fontSize}px" fill="white">${dimensionsText}</text>
</svg>
`;
try {
const buffer = await sharp(Buffer.from(svg))
.toFormat(metadata.format)
.toBuffer();
const sanitizedOriginalName = sanitizeFileName(
path.basename(file.originalname),
);
archive.append(buffer, { name: `${sanitizedOriginalName}` });
} catch (error) {
console.error("Error processing image:", error);
res.status(500).send("An error occurred while generating placeholders.");
return;
} finally {
fs.unlinkSync(file.path); // Cleanup the uploaded file
}
}
archive.finalize();
});
function sanitizeFileName(fileName) {
// Replace spaces with underscores
// let sanitized = fileName.replace(/\s+/g, "_");
// it is not good to change space if there is because we will use it in our html template to replace all images
// therefore same name needed. keeping above function for future option in the UI.
// Remove or replace other non-standard or special characters
// This regex removes anything that's not a letter, number, underscore, or dot.
return fileName.replace(/[^a-zA-Z0-9._\s-]/g, "");
}
app.listen(port, () =>
console.log(`Server running on http://localhost:${port}`),
);