|
1 |
| -const multer = require('multer'); |
2 |
| -const path = require('path'); |
3 |
| -const sharp = require('sharp'); |
| 1 | +const multer = require("multer"); |
| 2 | +const path = require("path"); |
| 3 | +const sharp = require("sharp"); |
4 | 4 |
|
5 | 5 | const storage = multer.diskStorage({
|
6 |
| - destination: (req, file, cb) => { |
7 |
| - const uploadDir = path.join(__dirname, '../images/uploads'); |
8 |
| - cb(null, uploadDir); |
9 |
| - }, |
10 |
| - filename: (req, file, cb) => { |
11 |
| - const uniqueName = `${Date.now()}-${file.originalname.replace(/\s+/g, '_')}`; |
12 |
| - cb(null, uniqueName); |
13 |
| - } |
| 6 | + destination: (req, file, cb) => { |
| 7 | + const uploadDir = path.join(__dirname, "../images/uploads"); |
| 8 | + cb(null, uploadDir); |
| 9 | + }, |
| 10 | + filename: (req, file, cb) => { |
| 11 | + const uniqueName = `${Date.now()}-${file.originalname.replace( |
| 12 | + /\s+/g, |
| 13 | + "_" |
| 14 | + )}`; |
| 15 | + cb(null, uniqueName); |
| 16 | + }, |
14 | 17 | });
|
15 | 18 |
|
16 | 19 | const upload = multer({ storage });
|
17 | 20 |
|
18 | 21 | async function optimizeImage(filePath, qualityValue, resizeWidth = 800) {
|
19 |
| - const uploadDir = path.join(__dirname, '..', 'images', 'uploads'); |
20 |
| - const sanitizedFilename = `optimized-${Date.now()}-${path.basename(filePath).replace(/\s+/g, '_').replace(/[^a-zA-Z0-9.-_]/g, '')}`; |
21 |
| - const outputFilePath = path.join(uploadDir, sanitizedFilename); |
| 22 | + const uploadDir = path.join(__dirname, "..", "images", "uploads"); |
| 23 | + const sanitizedFilename = `optimized-${Date.now()}-${path |
| 24 | + .basename(filePath) |
| 25 | + .replace(/\s+/g, "_") |
| 26 | + .replace(/[^a-zA-Z0-9.-_]/g, "")}`; |
| 27 | + const outputFilePath = path.join(uploadDir, sanitizedFilename); |
22 | 28 |
|
23 |
| - await sharp(filePath) |
24 |
| - .resize(resizeWidth) |
25 |
| - .toFormat('jpeg') |
26 |
| - .jpeg({ quality: qualityValue }) |
27 |
| - .toFile(outputFilePath); |
| 29 | + await sharp(filePath) |
| 30 | + .resize(resizeWidth) |
| 31 | + .toFormat("jpeg") |
| 32 | + .jpeg({ quality: qualityValue }) |
| 33 | + .toFile(outputFilePath); |
28 | 34 |
|
29 |
| - return sanitizedFilename; |
| 35 | + return sanitizedFilename; |
30 | 36 | }
|
31 | 37 |
|
32 | 38 | function sanitizeFileName(filePath) {
|
33 |
| - const ext = path.extname(filePath).toLowerCase(); |
34 |
| - const sanitizedFilename = `optimized-${Date.now()}-${Math.random().toString(36).substr(2, 9)}-${path.basename(filePath).replace(/\s+/g, '_').replace(/[^a-zA-Z0-9.-_]/g, '')}`; |
35 |
| - return sanitizedFilename; |
| 39 | + const ext = path.extname(filePath).toLowerCase(); |
| 40 | + const sanitizedFilename = `optimized-${Date.now()}-${Math.random() |
| 41 | + .toString(36) |
| 42 | + .substr(2, 9)}-${path |
| 43 | + .basename(filePath) |
| 44 | + .replace(/\s+/g, "_") |
| 45 | + .replace(/[^a-zA-Z0-9.-_]/g, "")}`; |
| 46 | + return sanitizedFilename; |
36 | 47 | }
|
37 | 48 |
|
38 | 49 | module.exports = { upload, optimizeImage, sanitizeFileName };
|
0 commit comments