Path: blob/master/src/packages/next/lib/share/path-to-files.ts
1449 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { join } from "path";6import getPool from "@cocalc/database/pool";7import { projects } from "@cocalc/backend/data";89// Given a project_id/path, return the directory on the file system where10// that path should be located.11export default function pathToFiles(project_id: string, path: string): string {12return join(projects.replace("[project_id]", project_id), path);13}1415export async function pathFromID(16id: string17): Promise<{ projectPath: string; fsPath: string }> {18// 'infinite' since actually result can't change since id determines the path (it's a reverse sha1 hash computation)19const pool = getPool("infinite");20const { rows } = await pool.query(21"SELECT project_id, path FROM public_paths WHERE id=$1 AND disabled IS NOT TRUE",22[id]23);24if (rows.length == 0) {25throw Error(`no such public path: ${id}`);26}2728const { project_id, path } = rows[0];29if (project_id == null || path == null) {30throw Error(`invalid public path: ${id}`);31}32return { projectPath: path, fsPath: pathToFiles(project_id, path) };33}343536