runtime/globals/file/
mod.rs1pub use blob::{Blob, BufferSource};
8use chrono::{DateTime, TimeZone as _, Utc};
9use ion::function::{Opt, Wrap};
10use ion::{ClassDefinition as _, Context, FromValue, Object, js_class};
11
12use crate::globals::file::blob::{BlobOptions, BlobPart};
13use crate::globals::file::reader::{FileReader, FileReaderSync};
14
15mod blob;
16mod reader;
17
18#[derive(Debug, Default, FromValue)]
19pub struct FileOptions {
20 #[ion(inherit)]
21 blob: BlobOptions,
22 modified: Option<Wrap<i64>>,
23}
24
25#[js_class]
26pub struct File {
27 blob: Blob,
28 name: String,
29 #[trace(no_trace)]
30 modified: DateTime<Utc>,
31}
32
33#[js_class]
34impl File {
35 #[ion(constructor)]
36 pub fn constructor(parts: Vec<BlobPart>, name: String, Opt(options): Opt<FileOptions>) -> File {
37 let options = options.unwrap_or_default();
38 let blob = Blob::constructor(Opt(Some(parts)), Opt(Some(options.blob)));
39 let modified = options
40 .modified
41 .and_then(|d| Utc.timestamp_millis_opt(d.0).single())
42 .unwrap_or_else(Utc::now);
43
44 File { blob, name, modified }
45 }
46
47 #[ion(get)]
48 pub fn get_name(&self) -> &str {
49 &self.name
50 }
51
52 #[ion(get)]
53 pub fn get_last_modified(&self) -> i64 {
54 self.modified.timestamp_millis()
55 }
56}
57
58pub fn define(cx: &Context, object: &Object) -> bool {
59 Blob::init_class(cx, object).0
60 && File::init_class(cx, object).0
61 && FileReader::init_class(cx, object).0
62 && FileReaderSync::init_class(cx, object).0
63}