Read file content: read-document for any file (Google-native files
are exported, everything else downloaded), byte-exact downloads,
Range and chunked downloads past the response cap, exports, and the
export-format map.
Results are ::google::drive/FileContent: bytes is always the raw
body (a JSON file stays bytes, a file containing 123 stays text),
and text is filled in when the MIME type is textual.
Limits. files.export returns at most 10 MB; larger exports fall
back to the file's exportLinks automatically. 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 blob is larger, and exports cannot be chunked (Drive
does not support Range on exports).
Scopes. drive.readonly by default; drive.file for files the
app created or opened. drive.metadata.readonly cannot read content.
Values
DEFAULT_DOWNLOAD_CHUNK
DEFAULT_DOWNLOAD_CHUNK: Int 8388608
DEFAULT_FORMATS
DEFAULT_FORMATS: Map {
"application/vnd.google-apps.document": "markdown",
"application/vnd.google-apps.spreadsheet": "csv",
"application/vnd.google-apps.presentation": "text",
"application/vnd.google-apps.drawing": "svg",
"application/vnd.google-apps.script": "json"
}
DOWNLOAD_LOCAL_KEYS
DOWNLOAD_LOCAL_KEYS: Vec [
"range",
"chunk-size"
]
EXPORT_FORMATS
EXPORT_FORMATS: Map {
"application/vnd.google-apps.document": {
markdown: "text/markdown",
md: "text/markdown",
text: "text/plain",
txt: "text/plain",
html: "text/html",
zip: "application/zip",
pdf: "application/pdf",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
odt: "application/vnd.oasis.opendocument.text",
rtf: "application/rtf",
epub: "application/epub+zip"
},
"application/vnd.google-apps.spreadsheet": {
csv: "text/csv",
tsv: "text/tab-separated-values",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
ods: "application/vnd.oasis.opendocument.spreadsheet",
pdf: "application/pdf",
zip: "application/zip"
},
"application/vnd.google-apps.presentation": {
text: "text/plain",
txt: "text/plain",
pdf: "application/pdf",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
odp: "application/vnd.oasis.opendocument.presentation"
},
"application/vnd.google-apps.drawing": {
svg: "image/svg+xml",
png: "image/png",
jpeg: "image/jpeg",
jpg: "image/jpeg",
pdf: "application/pdf"
},
"application/vnd.google-apps.script": {
json: "application/vnd.google-apps.script+json"
}
}
READ_LOCAL_KEYS
READ_LOCAL_KEYS: Vec [
"range",
"chunk-size",
"fields"
]
TYPE_LABELS
TYPE_LABELS: Map {
"application/vnd.google-apps.document": "Google Docs",
"application/vnd.google-apps.spreadsheet": "Google Sheets",
"application/vnd.google-apps.presentation": "Google Slides",
"application/vnd.google-apps.drawing": "Google Drawings",
"application/vnd.google-apps.script": "Apps Script"
}
Functions
download-file
fn (id: Str): ::google::drive/FileContent
fn (id: Str, opts: Map): ::google::drive/FileContent
GET /files/{id}?alt=media - download a blob file's bytes as a
FileContent (Google Docs/Sheets/Slides have no bytes: use
read-document or export-file). 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, chunk-size for the chunked fallback,
acknowledgeAbuse: true to download a file flagged as abusive
(owner-only otherwise), plus credentials.
pdf ::google::drive::content/download-file("1AbCfile")
pdf.mime-type // "application/pdf"
pdf.bytes // exact bytes
download-file-chunked
fn (id: Str): ::google::drive/FileContent
fn (id: Str, opts: Map): ::google::drive/FileContent
Download a blob 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 ::google::drive::content/download-file-chunked("1AbCfile", {chunk-size: 4194304})
video.size
download-params
fn (opts: Map): Map
Query parameters for a blob download (pure): alt=media,
supportsAllDrives=true, plus Drive parameters from opts such as
acknowledgeAbuse.
Example
::google::drive::content/download-params({acknowledgeAbuse: true, chunk-size: 1024})
// {alt: "media", supportsAllDrives: true, acknowledgeAbuse: true}
download-range
fn (id: Str, start: Int, end: Int): ::google::drive/FileContent
fn (id: Str, start: Int, end: Int, opts: Map): ::google::drive/FileContent
Download bytes start..end (inclusive) of a blob file.
Example
part ::google::drive::content/download-range("1AbCfile", 0, 1023)
part.size
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
::google::drive::content/download-ranges(10, 4) // [[0, 3], [4, 7], [8, 9]]
download-text
fn (id: Str): Str
fn (id: Str, opts: Map): Str
Download a blob file and decode it as UTF-8 text.
Example
::google::drive::content/download-text("1AbCfile")
export-file
fn (id: Str, mime-type: Str): ::google::drive/FileContent
fn (id: Str, mime-type: Str, opts: Map): ::google::drive/FileContent
GET /files/{id}/export - export a Google-native file to mime-type
(see EXPORT_FORMATS / export-mime-type), as a FileContent. When
Drive refuses with its 10 MB export limit, the file's exportLinks
URL for the same type is used instead.
csv ::google::drive::content/export-file("1AbCsheet", "text/csv")
csv.text
export-formats
fn (native-mime: Str): Map
The {format-name: mime-type} exports for a Google-native MIME type
({} for types that cannot be exported, e.g. Forms, Sites, Vids).
Example
::google::drive::content/export-formats("application/vnd.google-apps.script")
// {json: "application/vnd.google-apps.script+json"}
::google::drive::content/export-formats("application/vnd.google-apps.form") // {}
export-mime-type
fn (native-mime: Str, format: Str?): Str
Pick the export MIME type for a Google-native file (pure). format is
a short name ("markdown", "text", "pdf", "docx", "csv",
"xlsx", "pptx", "png", "svg", ...), a full MIME type (passed
through), or "auto"/null for the type's default: Docs markdown,
Sheets csv (first sheet only), Slides text, Drawings svg, Apps
Script json. Unsupported pairs return an Err listing the options.
::google::drive::content/export-mime-type(::google::drive/SPREADSHEET_MIME, "xlsx")
// "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
export-text
fn (id: Str, mime-type: Str): Str
fn (id: Str, mime-type: Str, opts: Map): Str
Export a Google-native file to a textual MIME type (text/markdown,
text/plain, text/csv, ...) and return the text.
Example
::google::drive::content/export-text("1AbCdoc", "text/markdown")
export-via-link
fn (file-id: Str, link: Str, opts: Map): ::google::drive/FileContent
Fetch an exportLinks URL with the call's credentials (the fallback for
exports over 10 MB).
Example
file ::google::drive::files/get-file("1AbCdoc", {fields: "exportLinks"})
pdf ::google::drive::content/export-via-link("1AbCdoc", get(file.exportLinks, "application/pdf"), {})
pdf.size
is-export-too-large
fn (result: Result): Bool
True when an export failed on Drive's 10 MB export limit
(exportSizeLimitExceeded).
Example
::google::drive::content/is-export-too-large(err("This file is too large to be exported.")) // true
::google::drive::content/is-export-too-large(err("connection reset")) // false
is-size-limit-error
fn (result: Result): Bool
True when a request failed because the response exceeded
http.max-response-bytes.
Example
::google::drive::content/is-size-limit-error(err("response exceeds http.max-response-bytes")) // true
::google::drive::content/is-size-limit-error(err("connection reset")) // 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
::google::drive::content/range-header(0, 1023) // "bytes=0-1023"
::google::drive::content/range-header(1024, null) // "bytes=1024-"
read-document
fn (id: Str): ::google::drive/FileContent
fn (id: Str, format: Str?): ::google::drive/FileContent
fn (id: Str, format: Str?, opts: Map): ::google::drive/FileContent
Read any file as a FileContent with its DocFile attached.
- Shortcuts are followed to their target.
- Google-native files (Docs, Sheets, Slides, Drawings, Apps Script)
are exported in
format("auto"by default: Docsmarkdown, Sheetscsvof the first sheet, Slidestext, Drawingssvg); seeexport-mime-typefor the names. Over 10 MB,exportLinksis used. - Other files are downloaded as stored and
formatis ignored (checkmime-type;textis set for textual types). - Folders return an Err.
doc ::google::drive::content/read-document("1AbCdoc", "markdown")
doc.text // Markdown, ready for chunking and embedding
doc.file.modified // for incremental re-indexing
read-document-text
fn (id: Str): Str
fn (id: Str, format: Str?): Str
fn (id: Str, format: Str?, opts: Map): Str
read-document, returning just the text; an Err when the result is not
textual (e.g. a PDF or image blob, or a "pdf" export).
Example
::google::drive::content/read-document-text("1AbCdoc")
// A Sheet as TSV
::google::drive::content/read-document-text("1AbCsheet", "tsv")