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/lib/names/public-path.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/* Get basic information about a user or organization6from the database. This should be enough to render7a nice "homepage" for that user or organization.8*/9import getPool from "@cocalc/database/pool";10import getProjectId from "./project";11import { getOwnerName } from "lib/names/owner";12import getProxyPublicPath, {13shouldUseProxy,14} from "lib/share/proxy/get-public-path";15import { join } from "path";1617export default async function getPublicPathId(18owner: string,19project: string,20public_path: string[] // this is the entire actual path21): Promise<string> {22if (shouldUseProxy(owner)) {23// special case -- proxy urls...24const { id } = await getProxyPublicPath({25url: join(owner, project, ...public_path),26});27return id;28}2930const project_id = await getProjectId(owner, project);31const pool = getPool("long");32const result = await pool.query(33"SELECT id FROM public_paths WHERE LOWER(name)=$1 AND project_id=$2",34[public_path[0]?.toLowerCase() ?? "", project_id]35);36if (result.rows.length > 0) {37return result.rows[0].id;38}39throw Error(`no public_path ${owner}/${project}/${public_path[0]}`);40}4142// Given the id of a public path, returns owner name, project name, and public_path name43// if they are all defined and nonempty. Otherwise returns undefined.44export async function getPublicPathNames(45id: string46): Promise<47{ owner: string; project: string; public_path: string } | undefined48> {49const pool = getPool("medium");50let result = await pool.query(51"SELECT project_id, name FROM public_paths WHERE id=$1",52[id]53);54if (result.rows.length == 0) return;55const { project_id, name: public_path } = result.rows[0];56if (!public_path) return;5758// Having to get users is pretty stupid -- see comment in lib/project/get-owner.ts59result = await pool.query(60"SELECT name, users FROM projects WHERE project_id=$1 AND name IS NOT NULL AND name != ''",61[project_id]62);63if (result.rows.length == 0) return;6465const { name: project, users } = result.rows[0];66let owner_id: string = "";67for (const account_id in users) {68if (users[account_id].group == "owner") {69owner_id = account_id;70break;71}72}73if (!owner_id) return; // shouldn't be possible74const owner = await getOwnerName(owner_id);75if (!owner) return;76return { owner, project, public_path };77}787980