Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files API Implementation #51

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ version = "0.1.0"
[dependencies]
gloo-timers = { version = "0.1.0", path = "crates/timers" }
gloo-console-timer = { version = "0.1.0", path = "crates/console-timer" }
gloo-file = { version = "0.1.0", path = "crates/file" }


[features]
default = []
Expand Down
18 changes: 18 additions & 0 deletions crates/file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "gloo-file"
version = "0.1.0"
authors = ["Rust and WebAssembly Working Group"]
edition = "2018"

[dependencies]
futures = "0.1.20"
wasm-bindgen = "0.2.40"
js-sys = "0.3.17"
mime = "0.3.13"

[dependencies.web-sys]
version = "0.3.17"
features = ["Blob", "File", "FileList", "FileReader", "HtmlInputElement"]

[dev-dependencies]
wasm-bindgen-test = "0.2.37"
44 changes: 44 additions & 0 deletions crates/file/src/blob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub use crate::file::File;

pub trait Blob: raw::Blob {
rylev marked this conversation as resolved.
Show resolved Hide resolved
fn size(&self) -> usize {
self.raw().size() as usize
}

fn mime_type(&self) -> Option<mime::Mime> {
Some(self.raw().type_().parse().ok()?)
rylev marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub struct DataBlob {
inner: web_sys::Blob,
}

impl DataBlob {
pub fn new(content: &str) -> DataBlob {
let parts = js_sys::Array::of1(&wasm_bindgen::JsValue::from_str(content));
let inner = web_sys::Blob::new_with_str_sequence(&parts).unwrap();
DataBlob { inner }
}
}

impl Blob for DataBlob {}
impl Blob for File {}

mod raw {
pub trait Blob {
fn raw(&self) -> &web_sys::Blob;
}

impl Blob for super::File {
fn raw(&self) -> &web_sys::Blob {
&self.inner
}
}

impl Blob for super::DataBlob {
fn raw(&self) -> &web_sys::Blob {
&self.inner
}
}
}
9 changes: 9 additions & 0 deletions crates/file/src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub struct File {
pub(crate) inner: web_sys::File,
}

impl File {
pub fn from_raw(inner: web_sys::File) -> File {
rylev marked this conversation as resolved.
Show resolved Hide resolved
File { inner }
}
}
57 changes: 57 additions & 0 deletions crates/file/src/file_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::file::File;

pub struct FileList {
inner: web_sys::FileList,
length: usize,
}

impl FileList {
pub fn new(input: &web_sys::HtmlInputElement) -> Option<Self> {
input.files().map(Self::from_raw)
}

pub fn from_raw(inner: web_sys::FileList) -> Self {
let length = inner.length() as usize;
FileList { inner, length }
}

pub fn get(&self, index: usize) -> Option<File> {
self.inner.get(index as u32).map(File::from_raw)
}

pub fn len(&self) -> usize {
self.length
}

pub fn iter(&self) -> FileListIter {
FileListIter {
file_list: self,
current: 0,
}
}

pub fn into_vec(self) -> Vec<File> {
self.iter().collect()
rylev marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub struct FileListIter<'a> {
rylev marked this conversation as resolved.
Show resolved Hide resolved
file_list: &'a FileList,
current: usize,
}

impl<'a> Iterator for FileListIter<'a> {
type Item = File;

fn next(&mut self) -> Option<Self::Item> {
if self.current >= self.file_list.len() {
return None;
}
let file = self.file_list.get(self.current);
self.current += 1;

assert!(file.is_some());

file
}
}
37 changes: 37 additions & 0 deletions crates/file/src/file_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::rc::Rc;

use futures::Future;
use wasm_bindgen::JsCast;

use crate::blob::Blob;

pub struct FileReader {
inner: web_sys::FileReader,
}

impl FileReader {
pub fn new() -> FileReader {
FileReader {
inner: web_sys::FileReader::new().unwrap(),
}
}

pub fn read_as_string(
self,
blob: &impl Blob,
) -> impl futures::Future<Item = String, Error = ()> {
rylev marked this conversation as resolved.
Show resolved Hide resolved
let (tx, rx) = futures::sync::oneshot::channel();
let reader = Rc::new(self.inner);
let cloned_reader = reader.clone();
rylev marked this conversation as resolved.
Show resolved Hide resolved
let cb = wasm_bindgen::closure::Closure::once_into_js(move || {
let _ = cloned_reader.result().map(|r| {
let _ = tx.send(r.as_string().unwrap());
rylev marked this conversation as resolved.
Show resolved Hide resolved
});
});
let reader = reader.clone();
let function = cb.dyn_into().unwrap();
reader.set_onload(Some(&function));
reader.read_as_text(&blob.raw()).unwrap();
rx.map_err(|_| ())
}
}
9 changes: 9 additions & 0 deletions crates/file/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod blob;
mod file;
mod file_list;
mod file_reader;

pub use blob::{Blob, DataBlob};
pub use file::File;
pub use file_list::FileList;
pub use file_reader::FileReader;
20 changes: 20 additions & 0 deletions crates/file/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Test suite for the Web and headless browsers.

#![cfg(target_arch = "wasm32")]
use futures::Future;
use wasm_bindgen_test::*;

use gloo_file::{DataBlob, FileReader};

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test(async)]
fn reading_a_blob() -> impl Future<Item = (), Error = wasm_bindgen::JsValue> {
let reader = FileReader::new();
let blob = DataBlob::new("hello");

reader
.read_as_string(&blob)
.map(|str| assert_eq!(str, "hello"))
.map_err(|_| panic!("Errored"))
}