-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (57 loc) · 1.83 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
// index.js
const express = require("express");
const path = require("path");
const cors = require("cors");
const app = express();
const PORT = 4000;
const multer = require("multer");
const { Storage } = require("@google-cloud/storage");
const { format } = require("util");
const fs = require("fs");
const { v4: uuidv4 } = require("uuid");
const storage = new Storage({ credentials: require("./config/kyber-image-sharing-fc9d5aa59ef7.json") });
const bucket = storage.bucket("image_sharing");
const multerMid = multer({
storage: multer.memoryStorage(),
limits: { fileSize: 5 * 1024 * 1024 },
});
app.use(cors());
app.listen(PORT, () => {
console.log(`API listening on PORT ${PORT} `);
});
app.get("/", (req, res) => {
const metaImage = req.query.imageurl;
const redirectUrl = req.query.redirecturl;
const filePath = path.resolve("./index.html");
fs.readFile(filePath, "utf8", function (err, data) {
if (err) {
return console.log(err);
}
if (metaImage) {
data = data.replace("@META_IMAGE", metaImage);
}
data = data.replace('"@REDIRECT_URL"', `"${redirectUrl}"` || undefined);
res.send(data);
});
});
app.disable("x-powered-by");
app.post("/upload", multerMid.single("file"), async (req, res, next) => {
if (!req.body.file) {
res.status(400).send("No file uploaded.");
return;
}
try {
const base64EncodedString = req.body.file.replace(/^data:\w+\/\w+;base64,/, "");
const buffer = Buffer.from(base64EncodedString, "base64");
const fileName = uuidv4() + ".png";
const blob = bucket.file(fileName);
await blob.save(buffer);
const publicUrl = format(`https://storage.googleapis.com/${bucket.name}/${fileName}`);
res.status(200).send(publicUrl);
} catch (err) {
next(err);
}
});
// Export the Express API
module.exports = app;
//AIzaSyAmjh7mt_9D6Qqo4XSQOndRSBTyIuEFFEM