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/share/proxy/get-contents.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { FileInfo, PathContents } from "lib/share/get-contents";6import { rawText, contents, defaultBranch, repos } from "./api";7import { join } from "path";8import { field_cmp } from "@cocalc/util/cmp";910export default async function getContents(11githubOrg: string,12githubRepo: string,13segments: string[],14): Promise<PathContents> {15if (!githubRepo) {16// get all repos attached to an org17const listing = await repos(githubOrg);18listing.sort(field_cmp("mtime"));19listing.reverse();20return { listing };21}2223switch (segments[0]) {24case undefined:25case "tree":26// directory listing27const response = await contents(githubOrg, githubRepo, segments);28if (response["name"] != null) {29// it's a file rather than a directory30throw Error("not implemented");31}32const branch =33segments[0] == null34? await defaultBranch(githubOrg, githubRepo)35: segments[1];36const listing: FileInfo[] = [];37for (const file of response) {38const isdir = file.type == "dir";39let url;40if (isdir) {41if (segments[0] == null) {42url = `/github/${githubOrg}/${githubRepo}/${join(43"tree",44branch,45...segments,46file.name,47)}`;48} else {49url = `/github/${githubOrg}/${githubRepo}/${join(50...segments,51file.name,52)}`;53}54} else {55if (segments[0] == null) {56url = `/github/${githubOrg}/${githubRepo}/${join(57"blob",58branch,59...segments,60file.name,61)}`;62} else {63url = `/github/${githubOrg}/${githubRepo}/${join(64"blob",65segments[1],66...segments.slice(2),67file.name,68)}`;69}70}71listing.push({72url,73name: file.name,74isdir,75...(file.type == "file" ? { size: file.size } : undefined),76});77}78return { isdir: true, listing };79break;80case "blob":81const content = await rawText(githubOrg, githubRepo, segments);82return { content, size: content.length };83default:84throw Error("not implemented");85}86}878889