Path: blob/master/src/packages/project/conat/files/write.ts
1449 views
/*12DEVELOPMENT:3451. Stop the files:write service running in the project by running this in your browser:67await cc.client.conat_client.projectApi(cc.current()).system.terminate({service:'files:write'})89{status: 'terminated', service: 'files:write'}1011You can also skip step 1 if you instead set COMPUTE_SERVER_ID to something nonzero...12132. Setup the project environment variables. Then start the server in node:141516~/cocalc/src/packages/project/conat$ . project-env.sh17$ node18Welcome to Node.js v18.17.1.19Type ".help" for more information.2021require('@cocalc/project/conat/files/write').init()222324*/2526import "@cocalc/project/conat/env"; // ensure conat env available27import ensureContainingDirectoryExists from "@cocalc/backend/misc/ensure-containing-directory-exists";28import { createWriteStream as fs_createWriteStream } from "fs";29import { rename } from "fs/promises";30import { compute_server_id, project_id } from "@cocalc/project/data";31import { join } from "path";32import {33createServer,34close as closeWriteServer,35} from "@cocalc/conat/files/write";36import { randomId } from "@cocalc/conat/names";37import { rimraf } from "rimraf";3839async function createWriteStream(path: string) {40// console.log("createWriteStream", { path });41if (path[0] != "/" && process.env.HOME) {42path = join(process.env.HOME, path);43}44await ensureContainingDirectoryExists(path);45const partial = path + `.partialupload-${randomId()}`;46const stream = fs_createWriteStream(partial);47stream.on("remove", async () => {48await rimraf(partial);49});50stream.on("rename", async () => {51await rename(partial, path);52});5354// TODO: path should be a temporary path to indicate that it is a partial55// upload, then get moved to path when done or deleted on error.56return stream;57}5859// the project should call this on startup:60export async function init() {61await createServer({ project_id, compute_server_id, createWriteStream });62}6364export async function close() {65await closeWriteServer({ project_id, compute_server_id });66}676869