1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use dom_struct::dom_struct;
use js::rust::HandleObject;
use net_traits::filemanager_thread::SelectedFile;
use script_traits::serializable::BlobImpl;
use crate::dom::bindings::codegen::Bindings::FileBinding;
use crate::dom::bindings::codegen::Bindings::FileBinding::FileMethods;
use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::blob::{blob_parts_to_bytes, normalize_type_string, Blob};
use crate::dom::globalscope::GlobalScope;
use crate::dom::window::Window;
#[dom_struct]
pub struct File {
blob: Blob,
name: DOMString,
modified: i64,
}
impl File {
#[allow(unrooted_must_root)]
fn new_inherited(blob_impl: &BlobImpl, name: DOMString, modified: Option<i64>) -> File {
File {
blob: Blob::new_inherited(blob_impl),
name: name,
modified: match modified {
Some(m) => m,
None => {
let time = time::get_time();
time.sec * 1000 + (time.nsec / 1000000) as i64
},
},
}
}
pub fn new(
global: &GlobalScope,
blob_impl: BlobImpl,
name: DOMString,
modified: Option<i64>,
) -> DomRoot<File> {
Self::new_with_proto(global, None, blob_impl, name, modified)
}
#[allow(unrooted_must_root)]
fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
blob_impl: BlobImpl,
name: DOMString,
modified: Option<i64>,
) -> DomRoot<File> {
let file = reflect_dom_object_with_proto(
Box::new(File::new_inherited(&blob_impl, name, modified)),
global,
proto,
);
global.track_file(&file, blob_impl);
file
}
pub fn new_from_selected(window: &Window, selected: SelectedFile) -> DomRoot<File> {
let name = DOMString::from(
selected
.filename
.to_str()
.expect("File name encoding error"),
);
File::new(
window.upcast(),
BlobImpl::new_from_file(
selected.id,
selected.filename,
selected.size,
normalize_type_string(&selected.type_string.to_string()),
),
name,
Some(selected.modified as i64),
)
}
#[allow(non_snake_case)]
pub fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
fileBits: Vec<ArrayBufferOrArrayBufferViewOrBlobOrString>,
filename: DOMString,
filePropertyBag: &FileBinding::FilePropertyBag,
) -> Fallible<DomRoot<File>> {
let bytes: Vec<u8> = match blob_parts_to_bytes(fileBits) {
Ok(bytes) => bytes,
Err(_) => return Err(Error::InvalidCharacter),
};
let ref blobPropertyBag = filePropertyBag.parent;
let modified = filePropertyBag.lastModified;
let replaced_filename = DOMString::from_string(filename.replace("/", ":"));
let type_string = normalize_type_string(&blobPropertyBag.type_.to_string());
Ok(File::new_with_proto(
global,
proto,
BlobImpl::new_from_bytes(bytes, type_string),
replaced_filename,
modified,
))
}
pub fn name(&self) -> &DOMString {
&self.name
}
}
impl FileMethods for File {
fn Name(&self) -> DOMString {
self.name.clone()
}
fn LastModified(&self) -> i64 {
self.modified
}
}