-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.js
170 lines (143 loc) · 4.76 KB
/
router.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
const path = require("path"),
fs = require("fs-extra"),
archiver = require("archiver");
const sockets = require("./core/sockets"),
dev = require("./core/dev-log"),
cache = require("./core/cache"),
api = require("./core/api"),
file = require("./core/file"),
exporter = require("./core/exporter"),
auth = require("./core/auth"),
importer = require("./core/importer"),
remote_api = require("./core/remote_api");
module.exports = function (app) {
/**
* routing event
*/
app.get("/", load_index);
app.get("/:slug/*", load_index);
app.get("/_archives/:type/:slugFolderName", downloadArchive);
app.post("/_file-upload/:type/:slugFolderName", postFile);
remote_api.init(app);
function collaborativeEditing(ws, req) {
console.log("WebSocket sharedb event");
ws.on("message", (msg) => {
console.log("WebSocket was closed");
ws.send(msg);
});
ws.on("close", () => {
console.log("WebSocket was closed");
});
}
function generatePageData(req) {
return new Promise(function (resolve, reject) {
let fullUrl = req.protocol + "://" + req.get("host") + req.originalUrl;
dev.log(`••• the following page has been requested: ${fullUrl} •••`);
let pageData = {};
pageData.pageTitle = global.appInfos.productName;
// full path on the storage space, as displayed in the footer
pageData.folderPath = api.getFolderPath();
pageData.slugFolderName = "";
pageData.url = req.path;
pageData.protocol = req.protocol;
pageData.structure = global.settings.structure;
pageData.ap = auth.hashCode(global.settings.adminPass);
pageData.isDebug = dev.isDebug();
pageData.store = Object.keys(global.settings.structure).reduce(
(acc, k) => {
acc[k] = {};
return acc;
},
{}
);
pageData.mode = "live";
resolve(pageData);
});
}
// GET
function load_index(req, res) {
generatePageData(req).then(
(pageData) => {
res.render("index", pageData);
},
(err) => {
dev.error(`Err while getting index data: ${err}`);
}
);
}
function downloadArchive(req, res) {
let type = req.param("type");
let slugFolderName = req.param("slugFolderName");
// check if folder is protected
file
.getFolder({ type: type, slugFolderName })
.then((foldersData) => {
const folder_meta = Object.values(foldersData)[0];
if (!folder_meta.hasOwnProperty("password") || !folder_meta.password) {
return;
}
// if it is, check that we have a socketid with the request and if so, if that id is allowed to access that folder
if (!req.query.hasOwnProperty("pwd")) {
throw "Missing password for protected folder";
}
const pwd = req.query.pwd;
if (String(auth.hashCode(folder_meta.password)) !== String(pwd)) {
throw "Wrong password for folder";
}
return;
})
.then(() => {
dev.log(
`Will create and stream archive for folder ${type}/${slugFolderName}`
);
// checks passed
var archive = archiver("zip", {
zlib: { level: 0 }, //
});
archive.on("error", function (err) {
res.status(500).send({ error: err.message });
});
//on stream closed we can end the request
archive.on("end", function () {
dev.log("Archive wrote %d bytes", archive.pointer());
});
//set the archive name
res.attachment(slugFolderName + ".zip");
//this is the streaming magic
archive.pipe(res);
const baseFolderPath = global.settings.structure[type].path;
const mainFolderPath = api.getFolderPath(baseFolderPath);
const thisFolderPath = path.join(mainFolderPath, slugFolderName);
archive.directory(thisFolderPath, false);
archive.finalize();
})
.catch((err) => {
dev.error(`Error! ${err}`);
res.status(500).send({ error: err });
});
}
async function postFile(req, res) {
let type = req.params.type;
let slugFolderName = req.params.slugFolderName;
dev.log(`••• postFile for ${type}/${slugFolderName} •••`);
importer
.handleForm({ req, type, slugFolderName })
.then(({ msg }) => {
sockets.notify({
socketid: req.query.socketid,
localized_string: `imported_files_successfully`,
type: "success",
});
res.end(JSON.stringify(msg));
})
.catch((err) => {
sockets.notify({
socketid: req.query.socketid,
localized_string: `action_not_allowed`,
not_localized_string: err.message,
type: "error",
});
res.end();
});
}
};