-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (83 loc) · 2.45 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require("express");
const axios = require("axios");
const fs = require('fs');
const app = express();
const port = process.env.PORT || 8757;
const path = require('path');
var bodyParser = require('body-parser')
app.use("/dist", express.static(__dirname + '/dist'));
app.use("/public", express.static(__dirname + '/public'));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
const imageKitBaseUrl = "https://imagekit.io/api/v1";
const headers = (cookie) => ({
headers: {
Cookie: cookie,
},
});
const handleErrors = (res, error) => {
res.status(500).send("Server error");
};
app.use(express.json());
app.get("/login", async (req, res) => {
res.sendfile((__dirname + '/login.html'));
});
app.get("/file", async (req, res) => {
res.sendfile((__dirname + '/imgs.html'));
});
app.get("/", async (req, res) => {
res.sendfile((__dirname + '/index.html'));
});
app.get("/api/v1/folders", async (req, res) => {
try {
const { cookie } = req.headers;
const { data } = await axios.get(
`${imageKitBaseUrl}/files?searchQuery=type%3D%22folder%22`,
headers(cookie)
);
const filteredResponse = data.map(({ name }) => ({ name }));
res.json(filteredResponse);
} catch (error) {
console.log(error)
handleErrors(res, error);
}
});
app.get("/api/v1/allfile", async (req, res) => {
try {
const { cookie } = req.headers;
var {data} = await axios.get(
`${imageKitBaseUrl}/files?searchQuery=type%3D%22file%22`,
headers(cookie)
);
const filteredResponse = data.map(({ url }) => ({ url }));
res.json(filteredResponse);
} catch (error) {
console.log(error)
handleErrors(res, error);
}
});
app.get("/api/v1/files", async (req, res) => {
try {
const { cookie } = req.headers;
const { path } = req.query;
const { data } = await axios.get(
`${imageKitBaseUrl}/files?searchQuery=type%3D%22file%22&path=${path}`,
headers(cookie)
);
const filteredResponse = data.map(({ filePath, url }) => ({
filePath,
url,
}));
res.json(filteredResponse);
} catch (error) {
handleErrors(res, error);
}
});
app.post("/api/v1/cookie", (req, res) => {
const { value } = req.body;
res.cookie("connect.sid", value);
res.json("OK");
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});