nagose commited on
Commit
7317987
·
verified ·
1 Parent(s): 70d122d

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +16 -16
index.js CHANGED
@@ -1,7 +1,7 @@
1
  const express = require('express');
2
  const multer = require('multer');
3
  const JSZip = require('jszip');
4
- const { createCanvas } = require('canvas');
5
  const crypto = require('crypto');
6
  const app = express();
7
 
@@ -74,29 +74,29 @@ app.post('/upload', upload.single('file'), async (req, res) => {
74
  const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' });
75
 
76
  // 创建包含文件信息的表格图片
77
- const width = 400;
78
- const height = 300;
79
- const canvas = createCanvas(width, height);
80
- const ctx = canvas.getContext('2d');
81
- ctx.fillStyle = '#FFFFFF';
82
- ctx.fillRect(0, 0, width, height);
83
- ctx.fillStyle = '#000000';
84
- ctx.font = '20px Microsoft YaHei';
85
-
86
  const text = `
87
  File Name: ${fileName}
88
  File Type: ${fileType}
89
  File Size: ${fileBuffer.length} bytes
90
  Created At: ${new Date().toLocaleString()}
91
  `;
92
- ctx.fillText(text.trim(), 10, 100);
93
- const rawImageData = canvas.toBuffer('image/jpeg', { quality: 0.75 });
94
- const finalBuffer = Buffer.concat([rawImageData, zipBuffer]);
95
- //const outputFileName = encodeURIComponent(fileName.replace(/\.[^/.]+$/, "") + '-pic.zip.jpg');
 
 
 
 
 
 
 
 
 
96
 
97
  // 生成随机的哈希值作为文件名
98
  const hash = crypto.randomBytes(16).toString('hex');
99
- const outputFileName = encodeURIComponent(hash + '-pic.zip.jpg');
100
 
101
  res.setHeader('Content-Type', 'image/jpeg');
102
  res.setHeader('Content-Disposition', `attachment; filename="${outputFileName}"`);
@@ -110,4 +110,4 @@ app.post('/upload', upload.single('file'), async (req, res) => {
110
  const PORT = process.env.PORT || 7860;
111
  app.listen(PORT, () => {
112
  console.log(`Server running on port ${PORT}`);
113
- });
 
1
  const express = require('express');
2
  const multer = require('multer');
3
  const JSZip = require('jszip');
4
+ const sharp = require('sharp');
5
  const crypto = require('crypto');
6
  const app = express();
7
 
 
74
  const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' });
75
 
76
  // 创建包含文件信息的表格图片
 
 
 
 
 
 
 
 
 
77
  const text = `
78
  File Name: ${fileName}
79
  File Type: ${fileType}
80
  File Size: ${fileBuffer.length} bytes
81
  Created At: ${new Date().toLocaleString()}
82
  `;
83
+ const imageBuffer = await sharp({
84
+ create: {
85
+ width: 400,
86
+ height: 300,
87
+ channels: 4,
88
+ background: { r: 255, g: 255, b: 255, alpha: 1 }
89
+ }
90
+ })
91
+ .composite([{ input: Buffer.from(text), top: 10, left: 10 }])
92
+ .jpeg()
93
+ .toBuffer();
94
+
95
+ const finalBuffer = Buffer.concat([imageBuffer, zipBuffer]);
96
 
97
  // 生成随机的哈希值作为文件名
98
  const hash = crypto.randomBytes(16).toString('hex');
99
+ const outputFileName = encodeURI(hash + '-pic.zip.jpg');
100
 
101
  res.setHeader('Content-Type', 'image/jpeg');
102
  res.setHeader('Content-Disposition', `attachment; filename="${outputFileName}"`);
 
110
  const PORT = process.env.PORT || 7860;
111
  app.listen(PORT, () => {
112
  console.log(`Server running on port ${PORT}`);
113
+ });