-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
196 lines (157 loc) · 4.97 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// init Web File System
const wfs = require("wfs-local");
const root = process.argv[3];
const drive = new wfs.LocalFiles(root, null, {
verbose: true
});
// init REST API
const fs = require("fs")
const cors = require("cors");
const path = require("path");
const Busboy = require("busboy");
const express = require("express");
const bodyParser = require("body-parser");
const { Readable } = require('stream')
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/info", async (req, res, next)=>{
const id = req.query.id;
const { free, used } = await drive.stats(id);
res.send({
stats:{ free, used, total: free+used },
features:{ preview:{}, meta:{} }
});
});
app.get("/files", async (req, res, next)=>{
const id = req.query.id;
const search = req.query.search;
const config = {
exclude: a => a.indexOf(".") === 0
};
if (search){
config.subFolders = true;
config.include = a => a.indexOf(search) !== -1;
}
res.send( await drive.list(id, config));
});
app.get("/folders", async (req, res, next)=>{
res.send( await drive.list("/", {
skipFiles: true,
subFolders: true,
nested: true,
exclude: a => a.indexOf(".") === 0
}));
});
app.get("/icons/:size/:type/:name", async (req, res, next)=>{
const url = await getIconURL(req.params.size, req.params.type, req.params.name, "");
res.sendFile(path.join(__dirname, url))
});
app.get("/icons/:skin/:size/:type/:name", async (req, res, next)=>{
const url = await getIconURL(req.params.size, req.params.type, req.params.name, req.params.skin);
res.sendFile(path.join(__dirname, url))
});
app.post("/copy", async (req, res, next)=>{
const source = req.body.id;
const target = req.body.to;
res.send(await drive.info(await drive.copy(source, target, "", { preventNameCollision: true })));
});
app.post("/move", async (req, res, next)=>{
const source = req.body.id;
const target = req.body.to;
res.send(await drive.info(await drive.move(source, target, "", { preventNameCollision: true })));
});
app.post("/rename", async (req, res, next)=>{
const source = req.body.id;
const target = path.dirname(source);
const name = req.body.name;
res.send(await drive.info(await drive.move(source, target, name, { preventNameCollision: true })));
});
app.post("/upload", async (req, res, next)=>{
const busboy = new Busboy({ headers: req.headers });
busboy.on("file", async (field, file, name) => {
console.log(req.body, name)
busboy.on('field', async function(field, val) {
// support folder upload
let base = req.query.id;
const parts = val.split("/");
if (parts.length > 1){
for (let i = 0; i < parts.length - 1; ++i){
const p = parts[i];
const exists = await drive.exists(base + "/" + p);
if (!exists) {
base = await drive.make(base, p, true);
} else {
base = base + "/" + p;
}
}
}
const target = await drive.make(base, name, false, { preventNameCollision: true });
res.send(await drive.info(await drive.write(target, file)));
});
});
req.pipe(busboy);
});
app.post("/makedir", async (req, res, next)=>{
const id = await drive.make(req.body.id, req.body.name, true, { preventNameCollision: true })
res.send(await drive.info(id));
});
app.post("/makefile", async (req, res, next)=>{
const id = await drive.make(req.body.id, req.body.name, false, { preventNameCollision: true })
res.send(await drive.info(id));
});
app.post("/delete", async (req, res, next)=>{
drive.remove(req.body.id)
res.send({})
});
app.post("/text", async (req, res, next)=>{
const name = req.body.id;
const content = req.body.content;
const id = await drive.write(name, Readable.from([content]));
res.send(await drive.info(id));
});
app.get("/text", async (req, res, next)=>{
const data = await drive.read(req.query.id);
data.pipe(res);
});
app.get("/direct", async (req, res, next) => {
const id = req.query.id;
const data = await drive.read(req.query.id);
const info = await drive.info(req.query.id);
const name = encodeURIComponent(info.value);
let disposition = "inline";
if (req.query.download){
disposition = "attachment";
}
res.writeHead(200, {
"Content-Disposition": `${disposition}; filename=${name}`
});
data.pipe(res);
});
async function getIconURL(size, type, name, skin){
size = size.replace(/[^A-Za-z0-9.]/g, "");
name = name.replace(/[^A-Za-z0-9.]/g, "");
type = type.replace(/[^A-Za-z0-9.]/g, "");
skin = skin.replace(/[^A-Za-z0-9.]/g, "");
const names = [
// get by name
`icons/default/${size}/${name}`,
// get by type with skin or not
( skin ? `icons/${skin}/${size}/types/${type}.svg` : null),
`icons/default/${size}/types/${type}.svg`
].filter(a => a);
for (let i = 0; i < names.length-1; i++){
try {
await fs.promises.stat(names[i]);
return names[i];
} catch (e) { }
}
return names[names.length-1];
}
// load other assets
app.use(express.static("./"));
const port = "3200";
const host = "localhost";
app.listen(port, host, function () {
console.log("Server is running on port " + port + "...");
});