Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/conat/files/write.ts
1449 views
1
/*
2
3
DEVELOPMENT:
4
5
6
1. Stop the files:write service running in the project by running this in your browser:
7
8
await cc.client.conat_client.projectApi(cc.current()).system.terminate({service:'files:write'})
9
10
{status: 'terminated', service: 'files:write'}
11
12
You can also skip step 1 if you instead set COMPUTE_SERVER_ID to something nonzero...
13
14
2. Setup the project environment variables. Then start the server in node:
15
16
17
~/cocalc/src/packages/project/conat$ . project-env.sh
18
$ node
19
Welcome to Node.js v18.17.1.
20
Type ".help" for more information.
21
22
require('@cocalc/project/conat/files/write').init()
23
24
25
*/
26
27
import "@cocalc/project/conat/env"; // ensure conat env available
28
import ensureContainingDirectoryExists from "@cocalc/backend/misc/ensure-containing-directory-exists";
29
import { createWriteStream as fs_createWriteStream } from "fs";
30
import { rename } from "fs/promises";
31
import { compute_server_id, project_id } from "@cocalc/project/data";
32
import { join } from "path";
33
import {
34
createServer,
35
close as closeWriteServer,
36
} from "@cocalc/conat/files/write";
37
import { randomId } from "@cocalc/conat/names";
38
import { rimraf } from "rimraf";
39
40
async function createWriteStream(path: string) {
41
// console.log("createWriteStream", { path });
42
if (path[0] != "/" && process.env.HOME) {
43
path = join(process.env.HOME, path);
44
}
45
await ensureContainingDirectoryExists(path);
46
const partial = path + `.partialupload-${randomId()}`;
47
const stream = fs_createWriteStream(partial);
48
stream.on("remove", async () => {
49
await rimraf(partial);
50
});
51
stream.on("rename", async () => {
52
await rename(partial, path);
53
});
54
55
// TODO: path should be a temporary path to indicate that it is a partial
56
// upload, then get moved to path when done or deleted on error.
57
return stream;
58
}
59
60
// the project should call this on startup:
61
export async function init() {
62
await createServer({ project_id, compute_server_id, createWriteStream });
63
}
64
65
export async function close() {
66
await closeWriteServer({ project_id, compute_server_id });
67
}
68
69