Read file content: byte-exact downloads (Box answers with a redirect
to dl.boxcloud.com, which Hot follows without forwarding the
token), Range and chunked downloads past the response cap, Box's
extracted_text representation, and read-document for one-call
text reads.
Extracted text. Box makes a plain-text rendition of documents
(PDF, Office, Google, iWork, ...) on request. extracted-text never
waits: it returns an ExtractedText whose state is "success"
(with text), "pending" (generation is running), "none" (just
asked Box to generate it), "unavailable" (Box offers no text for
this file type) or "error". Poll again from a later step or a Hot
schedule (for example every few minutes) until is-done; small
documents are usually ready within seconds, large ones can take
minutes.
Limits. Every single response is capped by the runtime's
http.max-response-bytes (16 MiB by default, 120 s timeout):
download-file switches to Range-chunked downloads when a file is
larger. The whole file is still held in memory. Box answers 202 with
Retry-After while a just-uploaded file is not yet downloadable; that
comes back as a retryable HttpError.
Values
ASSET_PATH_PLACEHOLDER
ASSET_PATH_PLACEHOLDER: Str "{+asset_path}"
DEFAULT_DOWNLOAD_CHUNK
DEFAULT_DOWNLOAD_CHUNK: Int 8388608
DONE_STATES
DONE_STATES: Vec [
"success",
"unavailable",
"error"
]
DOWNLOAD_LOCAL_KEYS
DOWNLOAD_LOCAL_KEYS: Vec [
"range",
"chunk-size",
"name",
"mime-type",
"boxapi"
]
READ_FIELDS
READ_FIELDS: Str "id,type,name,size,sha1,etag,sequence_id,file_version,parent,path_collection,created_at,modified_at,content_created_at,content_modified_at,trashed_at,item_status,extension,description,representations"
READ_LOCAL_KEYS
READ_LOCAL_KEYS: Vec [
"fields",
"download-fallback",
"rep-hints"
]
TEXT_REP
TEXT_REP: Str "extracted_text"
TEXT_REP_HINT
TEXT_REP_HINT: Str "[extracted_text]"
Functions
download-file
fn (file-id: Str): ::box/FileContent
fn (file-id: Str, opts: Map): ::box/FileContent
GET /files/{id}/content - download a file's bytes as a FileContent.
When the file is larger than http.max-response-bytes, it is fetched
again in Range chunks (download-file-chunked).
Options: range: {start, end} (inclusive; end null for the rest)
for a partial download, version (an older file version id),
chunk-size for the chunked fallback, boxapi for shared-link
access, plus credentials.
pdf ::box::content/download-file("12345")
pdf.bytes // exact bytes
download-file-chunked
fn (file-id: Str): ::box/FileContent
fn (file-id: Str, opts: Map): ::box/FileContent
Download a file in Range requests of opts.chunk-size bytes (default
8 MiB; keep it under http.max-response-bytes) and join them, so
files larger than one response can be read. The whole file is still
held in memory.
Example
video ::box::content/download-file-chunked("12345", {chunk-size: 4194304})
video.size
download-headers
fn (opts: Map): Map
The download request headers (pure): Range from opts.range ({start, end}, inclusive) and boxapi for shared-link access.
Example
::box::content/download-headers({range: {start: 0, end: 9}}) // {Range: "bytes=0-9"}
download-params
fn (opts: Map): Map
Query parameters for a download (pure): Box parameters from opts, such
as version (a file version id for an older version).
Example
::box::content/download-params({version: "99", chunk-size: 1024}) // {version: "99"}
download-range
fn (file-id: Str, start: Int, end: Int): ::box/FileContent
fn (file-id: Str, start: Int, end: Int, opts: Map): ::box/FileContent
Download bytes start..end (inclusive) of a file.
Example
// The first kilobyte
head ::box::content/download-range("12345", 0, 1023)
head.size
// From an older version
old ::box::content/download-range("12345", 0, 1023, {version: "99"})
old.bytes
download-ranges
fn (size: Int, chunk-size: Int): Vec
Split size bytes into inclusive [start, end] ranges of at most
chunk-size bytes (pure). download-ranges(10, 4) is [[0, 3], [4, 7], [8, 9]].
Example
::box::content/download-ranges(10, 4) // [[0, 3], [4, 7], [8, 9]]
download-text
fn (file-id: Str): Str
fn (file-id: Str, opts: Map): Str
Download a file and decode it as UTF-8 text.
Example
notes ::box::content/download-text("12345")
length(notes)
extracted-text
fn (file-id: Str): ExtractedText
fn (file-id: Str, opts: Map): ExtractedText
Check (and when needed request) a file's extracted_text
representation, and fetch the text when it is ready. Never waits:
returns an ExtractedText in whatever state Box reports. Call it
again later (a later step, or a Hot schedule) until is-done.
status ::box::content/extracted-text("12345")
cond {
::box::content/is-ready(status) => { println(status.text) }
::box::content/is-done(status) => { println(`box: no text for this file (${status.state})`) }
=> { println("box: text is still being generated; try again later") }
}
extracted-text-from
fn (file-id: Str, file: Map, text: Str?): ExtractedText
Build an ExtractedText from file information that carries
representations (pure; no text fetched).
Example
file {representations: {entries: [{representation: "extracted_text", status: {state: "pending"}}]}}
status ::box::content/extracted-text-from("12345", file, null)
status.state // "pending"
status.ready // false
find-representation
fn (file: Map, rep: Str): Map?
The entry of file.representations.entries whose representation is
rep (e.g. "extracted_text"), or null (pure).
Example
file {representations: {entries: [{representation: "extracted_text", status: {state: "success"}}]}}
::box::content/find-representation(file, "extracted_text").status.state // "success"
::box::content/find-representation(file, "pdf") // null
get-representations
fn (file-id: Str, hints: Str): Map
fn (file-id: Str, hints: Str, opts: Map): Map
GET /files/{id}?fields=representations with an x-rep-hints header
(e.g. "[extracted_text]", "[pdf][jpg?dimensions=320x320]"). Returns
the file information with representations.entries.
Example
file ::box::content/get-representations("12345", "[extracted_text]")
file.representations.entries
// A thumbnail and a PDF rendition
thumbs ::box::content/get-representations("12345", "[pdf][jpg?dimensions=320x320]", {fields: "representations"})
thumbs.representations.entries
is-done
fn (status: Map): Bool
True when polling an ExtractedText again would not change it: state
is "success", "unavailable" or "error".
Example
::box::content/is-done({state: "unavailable"}) // true
::box::content/is-done({state: "pending"}) // false
is-ready
fn (status: Map): Bool
True when an ExtractedText carries the text.
Example
::box::content/is-ready({ready: true}) // true
::box::content/is-ready({ready: false}) // false
is-size-limit-error
fn (result: Result): Bool
True when a request failed because the response exceeded
http.max-response-bytes.
Example
::box::content/is-size-limit-error(err("response exceeds http.max-response-bytes")) // true
::box::content/is-size-limit-error(err("request failed")) // false
range-header
fn (start: Int, end: Int?): Str
An HTTP Range value for bytes start..end inclusive (end null for
"to the end"): range-header(0, 1023) is "bytes=0-1023".
Example
::box::content/range-header(0, 1023) // "bytes=0-1023"
::box::content/range-header(1024, null) // "bytes=1024-"
read-document
fn (file-id: Str): ::box/FileContent
fn (file-id: Str, format: Str?): ::box/FileContent
fn (file-id: Str, format: Str?, opts: Map): ::box/FileContent
Read any file as a FileContent with its DocFile attached, in one
metadata call plus one content call.
format"auto"(default) or"text": textual files (txt, md, csv, json, ...) are downloaded as is; other documents use Box'sextracted_textwhen it is ready (source: "extracted-text"). When it is not ready, generation is requested and the original is downloaded instead (textis then null for binary files);text-statesays what Box reported, so read again later for the text.{download-fallback: false}skips that download (emptybytes)."original"(or"download","raw"): download the stored bytes.
doc ::box::content/read-document("12345", "text")
doc.text // ready for chunking and embedding, or null
doc.text-state // "success", "pending", "unavailable", ...
doc.file.sha1 // for incremental re-indexing
read-document-text
fn (file-id: Str): Str
fn (file-id: Str, format: Str?): Str
fn (file-id: Str, format: Str?, opts: Map): Str
read-document, returning just the text; an Err when there is no text
yet (the message says whether Box is still generating it, so a schedule
can try again).
Example
text ::box::content/read-document-text("12345")
length(text)
// Inside a schedule: an Err means try again later
result ::box::content/read-document-text("12345", "text", {as-user: "123"})
if(is-err(result), err-message(result), result)
representation-state
fn (entry: Map?): Str
The status.state of a representation entry (pure), or "unavailable"
for a null entry.
Example
::box::content/representation-state({status: {state: "pending"}}) // "pending"
::box::content/representation-state(null) // "unavailable"
representation-url
fn (template: Str, asset-path: Str): Str
Fill a representation url_template (pure): {+asset_path} becomes
asset-path ("" for unpaged representations such as extracted_text,
"1.png" for page 1 of a paged one).
Example
::box::content/representation-url("https://dl.boxcloud.com/rep/{+asset_path}", "1.png") // "https://dl.boxcloud.com/rep/1.png"
wants-original
fn (format: Str?): Bool
True when a read-document format asks for the stored bytes
("original", "download", "raw", "bytes"), not text (pure).
Example
::box::content/wants-original("raw") // true
::box::content/wants-original("text") // false
Types
ExtractedText
ExtractedText type {
file-id: Str,
state: Str,
text: Str?,
ready: Bool,
info-url: Str?,
content-url: Str?,
file: Map?
}
The state of a file's extracted_text representation.
Fields
file-idstate-"success","pending","none"(generation was just requested),"unavailable"(Box offers no text rendition for this file) or"error"text- the text whenstateis"success", else nullready- true whentextis setinfo-url- the representation info URL (GET it to check or trigger generation)content-url- the URL the text is fetched fromfile- the file information returned with the representation