Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/projects/copy-path.ts
Views: 687
/*1API endpoint to copy a path from one project to another (or from public files to2a project) or within a project.34This requires the user to be signed in with appropriate access.56See "@cocalc/server/projects/control/base" for params.7*/8import getAccountId from "lib/account/get-account";9import { getProject } from "@cocalc/server/projects/control";10import { isValidUUID } from "@cocalc/util/misc";11import getPool from "@cocalc/database/pool";12import isCollaborator from "@cocalc/server/projects/is-collaborator";13import getParams from "lib/api/get-params";1415export default async function handle(req, res) {16const params = getParams(req);1718const error = checkParams(params);19if (error) {20res.json({ error });21return;22}2324const {25public_id,26path,27src_project_id,28target_project_id,29target_path,30overwrite_newer,31delete_missing,32backup,33timeout,34bwlimit,35} = params;3637try {38const account_id = await getAccountId(req);39if (!account_id) {40throw Error("must be signed in");41}42if (43!(await isCollaborator({ account_id, project_id: target_project_id }))44) {45throw Error("must be a collaborator on target project");46}47if (public_id) {48// Verify that path is contained in the public path with id public_id:49if (50!(await isContainedInPublicPath({51id: public_id,52project_id: src_project_id,53path,54}))55) {56}57} else {58if (!(await isCollaborator({ account_id, project_id: src_project_id }))) {59throw Error("must be a collaborator on source project");60}61}62const project = getProject(src_project_id);63await project.copyPath({64path,65target_project_id,66target_path,67overwrite_newer,68delete_missing,69backup,70timeout,71bwlimit,72public: !!public_id,73wait_until_done: true,74});75// success means no exception and no error field in response.76res.json({});77} catch (err) {78res.json({ error: `${err.message}` });79}80}8182function checkParams(obj: any): string | undefined {83if (obj.path == null) return "path must be specified";84if (!isValidUUID(obj.src_project_id))85return "src_project_id must be a valid uuid";86if (!isValidUUID(obj.target_project_id))87return "target_project_id must be a valid uuid";88}8990async function isContainedInPublicPath({ id, project_id, path }) {91const pool = getPool("long");92const { rows } = await pool.query(93"SELECT project_id, path FROM public_paths WHERE disabled IS NOT TRUE AND vhost IS NULL AND id=$1",94[id],95);96return (97rows.length > 0 &&98rows[0].project_id == project_id &&99path.startsWith(rows[0].path)100);101}102103104