-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.js
77 lines (65 loc) · 2.06 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
69
70
71
72
73
74
75
76
77
const { onRequest } = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
initializeApp();
exports.listAllRedirects = onRequest(async (request, response) => {
result = await getFirestore().collection("links").get();
response.type("json");
response.send(
JSON.stringify(
result.docs.map((docSnap) => docSnap.data()),
null,
2
)
);
});
exports.redirectToFullURL = onRequest(async (request, response) => {
const shortLink = request.path.split("/")[1];
if (!shortLink) {
response.status(404).send(`Invalid shortlink "${request.path}"`);
logger.info(`Invalid shortlink "${request.path}"`);
return;
}
const result = await getFirestore()
.collection("links")
.where("shortUrl", "==", request.path.split("/")[1])
.get();
if (result.docs.length === 0) {
response.status(404).send(`No entry found for shortlink "${shortLink}"`);
logger.info(`No entry found for shortlink "${shortLink}"`);
return;
} else if (result.docs.length > 1) {
response.status(500).send(
`Found too many URLs. Something is wrong`,
result.docs.map((docSnap) => docSnap.ref.id)
);
return;
}
const redirectUrl = result.docs[0].data().longUrl;
logger.info("redirect");
response.redirect(301, redirectUrl);
});
exports.shortenUrl = onDocumentCreated(
"links/{linkid}",
async (event, params) => {
const hash = (Math.random() + 1).toString(36).substring(7);
// check if the URL is valid
try {
new URL(event.data.data().longUrl);
} catch {
logger.error(
`invalid URL "${event.data.data().longUrl}"`,
event.data.data()
);
return;
}
await event.data.ref.update({ shortUrl: hash });
logger.info(`created shortlink`, {
documentId: params.linkid,
longUrl: event.data.data().longUrl,
shortLink: hash,
});
}
);