Skip to main content

ferrijs_std/web/
blob_bytes.rs

1//! Reading bytes back out of a `Blob` or `File`.
2//!
3//! The classes are the vendored ones (`crate::buffer`); this is
4//! the synchronous accessor the request-body and form-data paths need,
5//! which the JS surface only exposes as promises.
6
7use crate::buffer::{Blob, File};
8use rquickjs::{Class, Value};
9
10/// Bytes + MIME type of a value that is a `Blob` — or a `File`, which is
11/// one.
12pub fn blob_parts(value: &Value<'_>) -> Option<(Vec<u8>, String)> {
13  if let Ok(blob) = Class::<Blob<'_>>::from_value(value) {
14    let blob = blob.borrow();
15    return Some((blob.get_bytes(), blob.mime_type()));
16  }
17  file_parts(value).map(|(bytes, mime, _)| (bytes, mime))
18}
19
20/// Bytes + MIME type + filename of a value that is a `File`.
21pub fn file_parts(value: &Value<'_>) -> Option<(Vec<u8>, String, String)> {
22  let file = Class::<File<'_>>::from_value(value).ok()?;
23  let file = file.borrow();
24  let blob = file.get_blob();
25  Some((blob.get_bytes(), file.mime_type(), file.name()))
26}