Table of contents
Uploads
What you can upload
Every method that takes a file accepts a TUpload:
| Form | Use it for |
|---|---|
"path/to/file.pdf" |
A file on disk, streamed from it. Node and Bun only: in a browser bundle a path is refused with a pointer to File / Blob |
a File |
Browser uploads; carries its own name |
a Blob |
Bytes with a type. Needs a name from somewhere: the method's name option, or use { data, name } instead |
{ data, name, type? } |
Bytes already in memory. This is the form for a Node Buffer |
Content Server only treats a multipart part as a file when it has a filename, which a bare Buffer or Blob lacks. That is why { data, name } exists, and why a nameless Blob is refused before sending.
await session.nodes.addDocument({ parent_id: 2000, fileHandler: "./report.pdf" });
await session.nodes.addDocument({ parent_id: 2000, fileHandler: { data: buffer, name: "report.pdf" } });
await session.versions.add(12345, "./report-v2.pdf");
addDocument takes parent_id and fileHandler, and optionally:
name, which defaults to the file's name;description;options, for further create fields such asadvanced_versioningorroles;largeFile.
Large File Upload
For files too big for one request, Content Server 21.4+ accepts a file in parts. addDocument does this for you:
await session.nodes.addDocument({ parent_id: 2000, fileHandler: "./huge.iso", largeFile: true });
largeFile: "auto" uses Large File Upload only when the server will take that file that way. The server decides what counts as large, and it can set the minimum high: one 26.1 server refuses anything under 2050 MB.
await session.uploads.accepts(sizeInBytes); // the same question, asked directly
Doing it by hand
const uploadKey = await session.uploads.upload("./huge.iso", {
onProgress: (done, total) => console.log(`${done}/${total}`),
});
await session.nodes.create({ type: 144, parent_id: 2000, name: "huge.iso", upload_key: uploadKey });
upload() asks the server for the part size, sends the parts in order (numbered from 1), retries each up to the server's max_retries, and cancels the upload session if anything fails. A path is read one part at a time, so files larger than memory work.
Its steps are also available individually: settings(), start(), uploadPart(), complete() and cancel().
Downloads
await session.nodes.download(12345, "./out.pdf"); // Node / Bun: streamed to disk
const { data } = await session.nodes.content(12345); // ArrayBuffer, everywhere
const blob = await session.nodes.content(12345, "blob"); // Blob in .data
const stream = await session.nodes.content(12345, "stream"); // web ReadableStream in .data
await session.versions.download(12345, 2, "./out-v2.pdf"); // a specific version
In Node, Buffer.from(data) turns the ArrayBuffer into a Buffer, and Readable.fromWeb(stream.data) (from node:stream) the web stream into a Node one.
cs-rest 4.0.0
Using it
Working on it