gossamer/file

Blobs annotated with a filename and last-modified timestamp. Typical use is form uploads — pair with form_data_extra.append_file. Construct with from_strings for text content or from_blob to wrap existing bytes.

Types

A Blob with a filename and last-modified timestamp.

See File on MDN.

pub type File {
  File(
    blob: blob.Blob,
    name: String,
    mime_type: String,
    last_modified: timestamp.Timestamp,
  )
}

Constructors

Values

pub fn array_buffer(
  file: File,
) -> promise.Promise(Result(array_buffer.ArrayBuffer, Nil))

Reads the file’s contents as an ArrayBuffer. Returns an error if the file’s source can’t be read.

pub fn bytes(
  file: File,
) -> promise.Promise(Result(BitArray, Nil))

Reads the file’s contents as a BitArray. Returns an error if the file’s source can’t be read.

pub fn from_blob(blob: blob.Blob, named name: String) -> File

Creates a File wrapping blob. The file’s mime_type is taken from blob.mime_type; last_modified is set to the current time.

pub fn from_strings(
  parts: List(String),
  named name: String,
) -> File

Creates a File whose contents are the concatenation of parts. last_modified is set to the current time.

pub fn set_blob(file: File, blob: blob.Blob) -> File

Sets the underlying Blob. The file’s existing mime_type is kept; pass through set_mime_type to adopt the blob’s type instead.

pub fn set_last_modified(
  file: File,
  last_modified: timestamp.Timestamp,
) -> File

Sets the last-modified timestamp.

pub fn set_mime_type(file: File, mime_type: String) -> File

Sets the MIME type.

pub fn set_name(file: File, name: String) -> File

Sets the filename used by form_data_extra.append_file’s multipart Content-Disposition header.

pub fn size(file: File) -> Int

The size of the file’s contents in bytes.

pub fn slice(
  file: File,
  from start: Int,
  to end: Int,
  content_type content_type: String,
) -> blob.Blob

Returns a Blob containing the bytes between start (inclusive) and end (exclusive). Negative offsets count from the end. Pass "" for content_type to leave the MIME type unset (the file’s type is never inherited).

pub fn stream(
  file: File,
) -> readable_stream.ReadableStream(BitArray)

Returns a ReadableStream that produces the file’s bytes.

pub fn text(file: File) -> promise.Promise(Result(String, Nil))

Reads the file’s contents as a UTF-8 string. Returns an error if the file’s source can’t be read.

Search Document