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/frontend/compute/project.ts
Views: 687
import { webapp_client } from "@cocalc/frontend/webapp-client";1// Write a (relatively SMALL) text file to the file system2// on a compute server, using the file system exec api call.3// This writes to a tmp file, then moves it, so that the4// write is atomic, e.g., because an application of this is5// to our proxy, which watches for file changes and reads the6// file, and reading a file while it is being written can7// be corrupt.89export async function writeTextFileToComputeServer({10value,11project_id,12compute_server_id,13path, // won't work if path has double quotes in it!14sudo,15}: {16value: string;17project_id: string;18compute_server_id: number;19path: string;20sudo?: boolean;21}) {22// Base64 encode the value.23const base64value = Buffer.from(value).toString("base64");24const random = `.tmp${Math.random()}`;25if (sudo) {26// Decode the base64 string before echoing it.27const args = [28"sh",29"-c",30`echo "${base64value}" | base64 --decode > "${path}${random}" && mv "${path}${random}" "${path}"`,31];3233await webapp_client.exec({34filesystem: true,35compute_server_id,36project_id,37command: "sudo",38args,39});40} else {41await webapp_client.exec({42filesystem: true,43compute_server_id,44project_id,45command: `echo "${base64value}" | base64 --decode > "${path}${random}" && mv "${path}${random}" "${path}"`,46});47}48}495051