import { join } from "path";
import getPool from "@cocalc/database/pool";
import { projects } from "@cocalc/backend/data";
export default function pathToFiles(project_id: string, path: string): string {
return join(projects.replace("[project_id]", project_id), path);
}
export async function pathFromID(
id: string
): Promise<{ projectPath: string; fsPath: string }> {
const pool = getPool("infinite");
const { rows } = await pool.query(
"SELECT project_id, path FROM public_paths WHERE id=$1 AND disabled IS NOT TRUE",
[id]
);
if (rows.length == 0) {
throw Error(`no such public path: ${id}`);
}
const { project_id, path } = rows[0];
if (project_id == null || path == null) {
throw Error(`invalid public path: ${id}`);
}
return { projectPath: path, fsPath: pathToFiles(project_id, path) };
}