49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
import fs from "fs";
|
|
import * as minio from "minio";
|
|
import 'dotenv/config';
|
|
import express from "express";
|
|
import cors from "cors";
|
|
import mime from "mime-types";
|
|
|
|
|
|
let minioClient = new minio.Client({
|
|
endPoint: process.env.MINIO_ENDPOINT,
|
|
accessKey: process.env.MINIO_ACCESS_KEY,
|
|
secretKey: process.env.MINIO_SECRET_KEY,
|
|
useSSL: true
|
|
});
|
|
|
|
const app = express();
|
|
const port = 3000;
|
|
const host = "0.0.0.0";
|
|
|
|
app.get("/img/:bucket/:image_path(*)", async (req, res) => {
|
|
|
|
let data;
|
|
minioClient.getObject(req.params.bucket, req.params.image_path, function(err, objStream) {
|
|
if (err) {
|
|
return console.log(err)
|
|
}
|
|
objStream.on('data', function(chunk) {
|
|
data = !data ? new Buffer(chunk) : Buffer.concat([data, chunk]);
|
|
})
|
|
objStream.on('end', function() {
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
res.writeHead(200, {'Content-Type': mime.lookup(req.params.image_path) });
|
|
res.write(data);
|
|
res.end();
|
|
})
|
|
objStream.on('error', function(err) {
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
res.status(500);
|
|
res.send(err);
|
|
})
|
|
});
|
|
});
|
|
|
|
|
|
app.listen(port, host, () => {
|
|
console.log(`minage started on ${host}:${port}`);
|
|
});
|
|
|