-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (70 loc) · 2.58 KB
/
index.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
const express = require('express');
const sharp = require('sharp');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const AdmZip = require('adm-zip');
const app = express();
const port = 12901;
let ApiRateCount = 0;
// 保存先のディレクトリを作成
const outputDirectory = path.join(__dirname, 'output');
const zipDirectory = path.join(__dirname, 'zip');
fs.mkdirSync(outputDirectory, { recursive: true });
fs.mkdirSync(zipDirectory, { recursive: true });
// Multerの設定
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
// ルートエンドポイント
app.post('/api/convert', upload.array('images'), async (req, res) => {
ApiRateCount++; // APIへのアクセス数をカウント
res.setHeader('Access-Control-Allow-Origin', '*');
try {
if (!req.files || req.files.length === 0) {
return res.status(400).send('No images uploaded.');
}
const toFormat = req.query.toFormat || 'webp';
const convertedImages = [];
// ファイルが1つの場合
if (req.files.length === 1) {
const inputBuffer = req.files[0].buffer;
const outputBuffer = await sharp(inputBuffer).toFormat(toFormat).toBuffer();
res.type(toFormat);
res.send(outputBuffer);
return;
} else {
// ファイルが複数の場合
await Promise.all(
req.files.map(async (file, index) => {
const inputBuffer = file.buffer;
const outputBuffer = await sharp(inputBuffer).toFormat(toFormat).toBuffer();
convertedImages.push(outputBuffer);
})
);
}
if (req.files.length > 1) {
const zip = new AdmZip();
convertedImages.forEach((image, index) => {
zip.addFile(`${index + 1}.${toFormat}`, image);
});
const zipFilePath = path.join(zipDirectory, 'converted_images.zip');
res.send(await zip.toBufferPromise());
}
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.get('/health', (req, res) => {
res.set('Content-Type', 'text/plain');
res.send('OK');
});
// メトリクスエンドポイント
app.get('/metrics', async (req, res) => {
let metrics = `ApiRateCount ${ApiRateCount}`;
res.set('Content-Type', 'text/plain');
res.send(metrics);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});