This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
filesRoute.js
64 lines (48 loc) · 1.58 KB
/
filesRoute.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
const path = require('path');
const fs = require('fs');
const { randomUUID } = require('node:crypto');
const router = require('express').Router();
const { ensureAuthenticated, getUserByEmail } = require('../utils/firebase');
// // GET /api/users/:email
// router.get('/:email', ensureAuthenticated, async (req, res) => {
// const userData = await getUserByEmail(req.params.email);
// res.json(userData);
// });
// // PUT /api/users
// router.put('/', ensureAuthenticated, async (req, res) => {
// const { email, data } = req.body;
// const userData = await getUserByEmail(email);
// await updateUser(userData.id, data);
// res.status(200).json({ message: 'cool' });
// });
// GET /api/files/:id
router.get('/:id', async (req, res) => {
try {
const fileId = req.params.id;
// Get form from Google
const filePath = path.join(__dirname, '..', 'temp', `${fileId}.html`);
res.sendFile(filePath);
res.on('finish', () => {
fs.rmSync(filePath);
});
} catch (err) {
res.status(500).json({ err });
}
});
// POST /api/files
router.post('/', async (req, res) => {
try {
const url = req.body.url;
// Get form from Google
let htmlData = await (await fetch(url)).text();
// const beautify = require('js-beautify').html;
// htmlData = beautify(htmlData, { indent_size: 2 });
const fileId = randomUUID();
const filePath = path.join(__dirname, '..', 'temp', `${fileId}.html`);
fs.writeFileSync(filePath, htmlData);
res.status(200).json({ fileId });
} catch (err) {
res.status(500).json({ err });
}
});
module.exports = router;