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/next/lib/share/path-to-files.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
import { join } from "path";
7
import getPool from "@cocalc/database/pool";
8
import { projects } from "@cocalc/backend/data";
9
10
// Given a project_id/path, return the directory on the file system where
11
// that path should be located.
12
export default function pathToFiles(project_id: string, path: string): string {
13
return join(projects.replace("[project_id]", project_id), path);
14
}
15
16
export async function pathFromID(
17
id: string
18
): Promise<{ projectPath: string; fsPath: string }> {
19
// 'infinite' since actually result can't change since id determines the path (it's a reverse sha1 hash computation)
20
const pool = getPool("infinite");
21
const { rows } = await pool.query(
22
"SELECT project_id, path FROM public_paths WHERE id=$1 AND disabled IS NOT TRUE",
23
[id]
24
);
25
if (rows.length == 0) {
26
throw Error(`no such public path: ${id}`);
27
}
28
29
const { project_id, path } = rows[0];
30
if (project_id == null || path == null) {
31
throw Error(`invalid public path: ${id}`);
32
}
33
return { projectPath: path, fsPath: pathToFiles(project_id, path) };
34
}
35
36