Path: blob/master/src/packages/project/directory-listing.ts
1447 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Server directory listing through the HTTP server and Websocket API.78{files:[..., {size:?,name:?,mtime:?,isdir:?}]}910where mtime is integer SECONDS since epoch, size is in bytes, and isdir11is only there if true.1213Obviously we should probably use POST instead of GET, due to the14result being a function of time... but POST is so complicated.15Use ?random= or ?time= if you're worried about cacheing.16Browser client code only uses this through the websocket anyways.17*/1819import { Router } from "express";20import getListing from "@cocalc/backend/get-listing";2122export default function init(): Router {23const base = "/.smc/directory_listing/";24const router = Router();2526router.get(base + "*", async (req, res) => {27// decodeURIComponent because decodeURI(misc.encode_path('asdf/te #1/')) != 'asdf/te #1/'28// https://github.com/sagemathinc/cocalc/issues/240029const path = decodeURIComponent(req.path.slice(base.length).trim());30const { hidden } = req.query;31// Fast -- do directly in this process.32try {33const files = await getListing(path, !!hidden);34res.json({ files });35} catch (err) {36res.json({ error: `${err}` });37}38});3940return router;41}424344