-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.ts
113 lines (101 loc) · 2.98 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Storage } from "@google-cloud/storage";
import cors from "cors";
import express from "express";
import { nanoid } from "nanoid";
import favicon from "serve-favicon";
import * as path from "path";
const PROJECT_NAME = process.env.GOOGLE_CLOUD_PROJECT || "excalidraw-json-dev";
const PROD = PROJECT_NAME === "excalidraw-json";
const LOCAL = process.env.NODE_ENV !== "production";
const BUCKET_NAME = PROD
? "excalidraw-json.appspot.com"
: "excalidraw-json-dev.appspot.com";
const FILE_SIZE_LIMIT = 2 * 1024 * 1024;
const storage = new Storage(
LOCAL
? {
projectId: PROJECT_NAME,
keyFilename: `${__dirname}/keys/${PROJECT_NAME}.json`,
}
: undefined
);
const bucket = storage.bucket(BUCKET_NAME);
const app = express();
let allowOrigins = [
"excalidraw.vercel.app",
"https://dai-shi.github.io",
"https://excalidraw.com",
"https://www.excalidraw.com",
"https://math.preview.excalidraw.com",
];
if (!PROD) {
allowOrigins.push("http://localhost:");
}
const corsGet = cors();
const corsPost = cors((req, callback) => {
const origin = req.headers.origin;
let isGood = false;
if (origin) {
for (const allowOrigin of allowOrigins) {
if (origin.indexOf(allowOrigin) >= 0) {
isGood = true;
break;
}
}
}
callback(null, { origin: isGood });
});
app.use(favicon(path.join(__dirname, "favicon.ico")));
app.get("/", (req, res) => res.sendFile(`${process.cwd()}/index.html`));
app.get("/api/v2/:key", corsGet, async (req, res) => {
try {
const key = req.params.key;
const file = bucket.file(key);
await file.getMetadata();
res.status(200);
res.setHeader("content-type", "application/octet-stream");
file.createReadStream().pipe(res);
} catch (error) {
console.error(error);
res.status(404).json({ message: "Could not find the file." });
}
});
app.post("/api/v2/post/", corsPost, (req, res) => {
try {
let fileSize = 0;
const id = nanoid();
const blob = bucket.file(id);
const blobStream = blob.createWriteStream({ resumable: false });
blobStream.on("error", (error) => {
console.error(error);
res.status(500).json({ message: error.message });
});
blobStream.on("finish", async () => {
res.status(200).json({
id,
data: `${LOCAL ? "http" : "https"}://${req.get("host")}/api/v2/${id}`,
});
});
req.on("data", (chunk) => {
blobStream.write(chunk);
fileSize += chunk.length;
if (fileSize > FILE_SIZE_LIMIT) {
const error = {
message: "Data is too large.",
max_limit: FILE_SIZE_LIMIT,
};
blobStream.destroy();
console.error(error);
return res.status(413).json(error);
}
});
req.on("end", () => {
blobStream.end();
});
} catch (error) {
console.error(error);
res.status(500).json({ message: "Could not upload the data." });
}
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`http://localhost:${port}`));