"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathFromID = void 0;
const path_1 = require("path");
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const data_1 = require("@cocalc/backend/data");
// Given a project_id/path, return the directory on the file system where
// that path should be located.
function pathToFiles(project_id, path) {
    return (0, path_1.join)(data_1.projects.replace("[project_id]", project_id), path);
}
exports.default = pathToFiles;
async function pathFromID(id) {
    // 'infinite' since actually result can't change since id determines the path (it's a reverse sha1 hash computation)
    const pool = (0, pool_1.default)("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) };
}
exports.pathFromID = pathFromID;
//# sourceMappingURL=path-to-files.js.map