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-url.ts
Views: 687
/*1API endpoint to copy from a URL on the internet to a project.23This requires the user to be signed in with appropriate access to the project.45If project doesn't have network access, we stop the project, start it with6network access, get the content, then restart the project without network access.7*/89import getAccountId from "lib/account/get-account";10import { isValidUUID } from "@cocalc/util/misc";11import isCollaborator from "@cocalc/server/projects/is-collaborator";12import getParams from "lib/api/get-params";13import call from "@cocalc/server/projects/connection/call";14import getProxiedPublicPathInfo from "lib/share/proxy/get-proxied-public-path-info";1516export default async function handle(req, res) {17const params = getParams(req);18const error = checkParams(params);19if (error) {20res.json({ error });21return;22}23const {24project_id,25url, // the supported schema is as in next/lib/share/proxy/get-public-path.ts).26path, // where to write the contents of the url27} = params;2829try {30const account_id = await getAccountId(req);31if (!account_id) {32throw Error("must be signed in");33}34if (!(await isCollaborator({ account_id, project_id }))) {35throw Error("must be a collaborator on target project");36}37const info = await getProxiedPublicPathInfo(url);38if (info.contents?.content == null) {39throw Error(40"copying of directories (e.g., full GitHub repos) is not implemented; copy an individual file instead",41);42}43const i = url.lastIndexOf("/");44const filename = url.slice(i + 1);45const mesg = {46event: "write_text_file_to_project",47path: path ? path : filename,48content: info.contents.content,49};50const response = await call({ project_id, mesg });51res.json({ response });52} catch (err) {53res.json({ error: `${err.message}` });54}55}5657function checkParams(obj: any): string | undefined {58if (!obj.url) return "url must be specified";59if (!isValidUUID(obj.project_id)) return "project_id must be a valid uuid";60}616263