-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (95 loc) · 2.03 KB
/
server.js
File metadata and controls
107 lines (95 loc) · 2.03 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const config = require("./config/config"),
createCdnUploader = require("./cdn-upload/index.js"),
bodyParser = require("body-parser"),
express = require("express"),
cors = require("cors");
/**
* Express App
*/
let app = express();
/**
* Middleware for disapling x - powered - by message
*/
app.disable("x-powered-by");
/**
* Cross Origin Sources middleware
*/
app.use(cors());
/**
* Bodyparser middleware image buffer size to 50mb limit
*/
app.use(
bodyParser.json({
limit: "50mb",
})
);
/**
* Bodyparser middleware urlencoded not extended
*/
app.use(
bodyParser.urlencoded({
extended: false,
})
);
/**
* @GET
* static Api endpoint
*/
app.get("/", (_, res) => {
//TODO: Add beter interface to show case screenify
res.send("Screenify");
});
/**
* @POST
* /upload Api endpoint
*/
app.use("/api/upload", async (req, res, next) => {
const { buffer } = req.body;
try {
const imageUrl = await upload(buffer, "google");
res.status(200).json({
success: true,
url: imageUrl,
});
} catch (error) {
next(error);
}
});
/**
* Uploading function returns the url of the uploaded picture.
* @param {blob} serializeBlob
* @param {string} cdnType
*/
function upload(serializeBlob, cdnType) {
return new Promise((resolve, reject) => {
const bytes = new Uint8Array(serializeBlob.split(","));
const uploader = createCdnUploader(config, cdnType);
uploader
.upload(Buffer.from(bytes))
.then((url) => {
resolve(url);
})
.catch((e) => {
try {
upload(buffer, "google");
} catch (err) {
reject(e);
}
});
});
}
/**
* Error Handler middleware
*/
app.use(function (err, req, res) {
if (!err.statusCode) err.statusCode = 500;
res.status(err.statusCode).send(err.message);
});
/**
* Server Starter 🚀
*/
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () => {
console.log("🚀 Connected to port", server.address().port);
console.log("Press Ctrl + C to stop the server ");
});