-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
93 lines (73 loc) · 2.28 KB
/
index.ts
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
import express, { Request, Response } from 'express';
import multer from 'multer';
import axios from 'axios';
import * as Minio from 'minio';
import dotenv from 'dotenv';
import fs from 'fs';
dotenv.config();
const uploadDirectory = './uploads';
if (!fs.existsSync(uploadDirectory)) {
fs.mkdirSync(uploadDirectory);
}
const minioClient = new Minio.Client({
endPoint: process.env.MINIO_ENDPOINT || '',
port: 9000,
useSSL: true,
accessKey: process.env.MINIO_ACCESS_KEY || '',
secretKey: process.env.MINIO_SECRET_KEY || '',
});
const app = express();
const port = 310;
app.use(express.json());
const upload = multer({ dest: uploadDirectory });
async function uploadObject(req: Request, res: Response): Promise<void> {
upload.single('image')(req, res, async (err) => {
if (err) {
console.error(err);
res.status(500).send('Error uploading image.');
return;
}
const imageFile = req.file;
const { path, bucket } = req.body;
if (!imageFile) {
res.status(400).send('No image uploaded.');
return;
}
const filePath: string = './uploads/' + imageFile.filename;
try {
await minioClient.fPutObject(bucket, path + imageFile.filename, filePath);
fs.unlinkSync(filePath);
const fileUrl = `https://${process.env.MINIO_ENDPOINT}:9000/${bucket}/${path + imageFile.filename}`;
const pingResponse = await axios.head(fileUrl);
if (pingResponse.status === 200) {
res.status(200).send({
url: fileUrl,
});
} else {
res.status(500).send('Error accessing uploaded object.');
}
} catch (error) {
console.error(error);
res.status(500).send('Error uploading object.');
}
});
}
async function deleteObject(req: Request, res: Response): Promise<void> {
const { bucket, path } = req.body;
if (!bucket || !path) {
res.status(400).send('Missing bucket or path.');
return;
}
try {
await minioClient.removeObject(bucket, path);
res.status(200).send('Object deleted successfully.');
} catch (error) {
console.error(error);
res.status(500).send('Error deleting object.');
}
}
app.post('/upload-object', uploadObject);
app.post('/delete-object', deleteObject);
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});