"use strict";
(() => {
var exports = {};
exports.id = 1733;
exports.ids = [1733];
exports.modules = {

/***/ 439491:
/***/ ((module) => {

module.exports = require("assert");

/***/ }),

/***/ 714300:
/***/ ((module) => {

module.exports = require("buffer");

/***/ }),

/***/ 532081:
/***/ ((module) => {

module.exports = require("child_process");

/***/ }),

/***/ 435001:
/***/ ((module) => {

module.exports = require("cluster");

/***/ }),

/***/ 722057:
/***/ ((module) => {

module.exports = require("constants");

/***/ }),

/***/ 706113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 809523:
/***/ ((module) => {

module.exports = require("dns");

/***/ }),

/***/ 582361:
/***/ ((module) => {

module.exports = require("events");

/***/ }),

/***/ 657147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 773292:
/***/ ((module) => {

module.exports = require("fs/promises");

/***/ }),

/***/ 113685:
/***/ ((module) => {

module.exports = require("http");

/***/ }),

/***/ 795687:
/***/ ((module) => {

module.exports = require("https");

/***/ }),

/***/ 241808:
/***/ ((module) => {

module.exports = require("net");

/***/ }),

/***/ 822037:
/***/ ((module) => {

module.exports = require("os");

/***/ }),

/***/ 371017:
/***/ ((module) => {

module.exports = require("path");

/***/ }),

/***/ 504074:
/***/ ((module) => {

module.exports = require("perf_hooks");

/***/ }),

/***/ 677282:
/***/ ((module) => {

module.exports = require("process");

/***/ }),

/***/ 514521:
/***/ ((module) => {

module.exports = require("readline");

/***/ }),

/***/ 12781:
/***/ ((module) => {

module.exports = require("stream");

/***/ }),

/***/ 371576:
/***/ ((module) => {

module.exports = require("string_decoder");

/***/ }),

/***/ 924404:
/***/ ((module) => {

module.exports = require("tls");

/***/ }),

/***/ 76224:
/***/ ((module) => {

module.exports = require("tty");

/***/ }),

/***/ 257310:
/***/ ((module) => {

module.exports = require("url");

/***/ }),

/***/ 473837:
/***/ ((module) => {

module.exports = require("util");

/***/ }),

/***/ 984655:
/***/ ((module) => {

module.exports = require("v8");

/***/ }),

/***/ 959796:
/***/ ((module) => {

module.exports = require("zlib");

/***/ }),

/***/ 271949:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handle)
/* harmony export */ });
/* harmony import */ var lib_account_get_account__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(559459);
/* harmony import */ var _cocalc_server_projects_control__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(145970);
/* harmony import */ var _cocalc_server_projects_control__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_projects_control__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _cocalc_util_misc__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(854006);
/* harmony import */ var _cocalc_util_misc__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_cocalc_util_misc__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _cocalc_database_pool__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(541808);
/* harmony import */ var _cocalc_database_pool__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_cocalc_database_pool__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var _cocalc_server_projects_is_collaborator__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(126804);
/* harmony import */ var _cocalc_server_projects_is_collaborator__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_projects_is_collaborator__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var lib_api_get_params__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(26438);
/*
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.
*/ 





async function handle(req, res) {
    const params = (0,lib_api_get_params__WEBPACK_IMPORTED_MODULE_5__/* ["default"] */ .Z)(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,lib_account_get_account__WEBPACK_IMPORTED_MODULE_0__/* ["default"] */ .Z)(req);
        if (!account_id) {
            throw Error("must be signed in");
        }
        if (!_cocalc_server_projects_is_collaborator__WEBPACK_IMPORTED_MODULE_4___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 (!_cocalc_server_projects_is_collaborator__WEBPACK_IMPORTED_MODULE_4___default()({
                account_id,
                project_id: src_project_id
            })) {
                throw Error("must be a collaborator on source project");
            }
        }
        const project = (0,_cocalc_server_projects_control__WEBPACK_IMPORTED_MODULE_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}`
        });
    }
}
function checkParams(obj) {
    if (obj.path == null) return "path must be specified";
    if (!(0,_cocalc_util_misc__WEBPACK_IMPORTED_MODULE_2__.isValidUUID)(obj.src_project_id)) return "src_project_id must be a valid uuid";
    if (!(0,_cocalc_util_misc__WEBPACK_IMPORTED_MODULE_2__.isValidUUID)(obj.target_project_id)) return "target_project_id must be a valid uuid";
}
async function isContainedInPublicPath({ id , project_id , path  }) {
    const pool = _cocalc_database_pool__WEBPACK_IMPORTED_MODULE_3___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);
}


/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [6918,1199,8180,2921,9557,5245,6652,6942,5517,6099,5751,2739,6257,8759,2003,1808,4006,8312,5710,9459,4181,4557,7896,6177,7710,4012,5970,6394], () => (__webpack_exec__(271949)));
module.exports = __webpack_exports__;

})();