CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/directory-listing.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Server directory listing through the HTTP server and Websocket API.
8
9
{files:[..., {size:?,name:?,mtime:?,isdir:?}]}
10
11
where mtime is integer SECONDS since epoch, size is in bytes, and isdir
12
is only there if true.
13
14
Obviously we should probably use POST instead of GET, due to the
15
result being a function of time... but POST is so complicated.
16
Use ?random= or ?time= if you're worried about cacheing.
17
Browser client code only uses this through the websocket anyways.
18
*/
19
20
import { Router } from "express";
21
import getListing from "@cocalc/backend/get-listing";
22
23
export default function init(): Router {
24
const base = "/.smc/directory_listing/";
25
const router = Router();
26
27
router.get(base + "*", async (req, res) => {
28
// decodeURIComponent because decodeURI(misc.encode_path('asdf/te #1/')) != 'asdf/te #1/'
29
// https://github.com/sagemathinc/cocalc/issues/2400
30
const path = decodeURIComponent(req.path.slice(base.length).trim());
31
const { hidden } = req.query;
32
// Fast -- do directly in this process.
33
try {
34
const files = await getListing(path, !!hidden);
35
res.json({ files });
36
} catch (err) {
37
res.json({ error: `${err}` });
38
}
39
});
40
41
return router;
42
}
43
44