"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
API endpoint to copy a path from one project to another (or from public files to
a project) or within a project.

This requires the user to be signed in with appropriate access.

See "@cocalc/server/projects/control/base" for params.
*/
const get_account_1 = __importDefault(require("lib/account/get-account"));
const control_1 = require("@cocalc/server/projects/control");
const misc_1 = require("@cocalc/util/misc");
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const is_collaborator_1 = __importDefault(require("@cocalc/server/projects/is-collaborator"));
const get_params_1 = __importDefault(require("lib/api/get-params"));
async function handle(req, res) {
    const params = (0, get_params_1.default)(req);
    const error = checkParams(params);
    if (error) {
        res.json({ error });
        return;
    }
    const { public_id, path, src_project_id, target_project_id, target_path, overwrite_newer, delete_missing, backup, timeout, bwlimit, } = params;
    try {
        const account_id = await (0, get_account_1.default)(req);
        if (!account_id) {
            throw Error("must be signed in");
        }
        if (!(0, is_collaborator_1.default)({ account_id, project_id: target_project_id })) {
            throw Error("must be a collaborator on target project");
        }
        if (public_id) {
            // Verify that path is contained in the public path with id public_id:
            if (!(await isContainedInPublicPath({
                id: public_id,
                project_id: src_project_id,
                path,
            }))) {
            }
        }
        else {
            if (!(0, is_collaborator_1.default)({ account_id, project_id: src_project_id })) {
                throw Error("must be a collaborator on source project");
            }
        }
        const project = (0, control_1.getProject)(src_project_id);
        await project.copyPath({
            path,
            target_project_id,
            target_path,
            overwrite_newer,
            delete_missing,
            backup,
            timeout,
            bwlimit,
            public: !!public_id,
            wait_until_done: true,
        });
        // success means no exception and no error field in response.
        res.json({});
    }
    catch (err) {
        res.json({ error: `${err.message}` });
    }
}
exports.default = handle;
function checkParams(obj) {
    if (obj.path == null)
        return "path must be specified";
    if (!(0, misc_1.isValidUUID)(obj.src_project_id))
        return "src_project_id must be a valid uuid";
    if (!(0, misc_1.isValidUUID)(obj.target_project_id))
        return "target_project_id must be a valid uuid";
}
async function isContainedInPublicPath({ id, project_id, path }) {
    const pool = (0, pool_1.default)("long");
    const { rows } = await pool.query("SELECT project_id, path FROM public_paths WHERE disabled IS NOT TRUE AND vhost IS NULL AND id=$1", [id]);
    return (rows.length > 0 &&
        rows[0].project_id == project_id &&
        path.startsWith(rows[0].path));
}
//# sourceMappingURL=copy-path.js.map