|
const express = require('express'); |
|
const multer = require('multer'); |
|
const JSZip = require('jszip'); |
|
const jpeg = require('jpeg-js'); |
|
const fs = require('fs'); |
|
const app = express(); |
|
|
|
|
|
const storage = multer.memoryStorage(); |
|
const upload = multer({ storage: storage }); |
|
|
|
app.use(express.json()); |
|
app.use(express.urlencoded({ extended: true })); |
|
|
|
app.post('/upload', upload.single('file'), async (req, res) => { |
|
try { |
|
const file = req.file; |
|
const fileName = file.originalname; |
|
const fileType = file.mimetype; |
|
const fileBuffer = file.buffer; |
|
|
|
|
|
console.log(`File Name: ${fileName}`); |
|
console.log(`File Type: ${fileType}`); |
|
console.log(`File Size: ${fileBuffer.length} bytes`); |
|
|
|
|
|
const zip = new JSZip(); |
|
zip.file(fileName, fileBuffer); |
|
const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' }); |
|
|
|
console.log(`Zip OK File Name: ${fileName}`); |
|
|
|
|
|
const width = 800; |
|
const height = 600; |
|
const frameData = Buffer.alloc(width * height * 4); |
|
const text = ` |
|
File Name: ${fileName} |
|
File Type: ${fileType} |
|
File Size: ${fileBuffer.length} bytes |
|
Created At: ${new Date().toLocaleString()} |
|
Modified At: ${new Date().toLocaleString()} |
|
`; |
|
|
|
frameData.fill(0); |
|
|
|
|
|
const textBuffer = Buffer.from(text); |
|
frameData.set(textBuffer, 0); |
|
|
|
const imageData = { |
|
data: frameData, |
|
width: width, |
|
height: height, |
|
}; |
|
const rawImageData = jpeg.encode(imageData, 50).data; |
|
|
|
console.log(`JPG OK File Name: ${fileName}`); |
|
|
|
|
|
const finalBuffer = Buffer.concat([rawImageData, zipBuffer]); |
|
|
|
|
|
const outputFileName = fileName.replace(/\.[^/.]+$/, "") + '-piczip.jpg'; |
|
|
|
|
|
res.setHeader('Content-Type', 'image/jpeg'); |
|
res.setHeader('Content-Disposition', `attachment; filename="${outputFileName}"`); |
|
res.send(finalBuffer); |
|
} catch (error) { |
|
console.error(error); |
|
res.status(500).send('An error occurred'); |
|
} |
|
}); |
|
|
|
const PORT = process.env.PORT || 7860; |
|
app.listen(PORT, () => { |
|
console.log(`Server running on port ${PORT}`); |
|
}); |
|
|