script/dom/
file.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::time::SystemTime;
6
7use constellation_traits::BlobImpl;
8use dom_struct::dom_struct;
9use js::rust::HandleObject;
10use net_traits::filemanager_thread::SelectedFile;
11use time::{Duration, OffsetDateTime};
12
13use crate::dom::bindings::codegen::Bindings::FileBinding;
14use crate::dom::bindings::codegen::Bindings::FileBinding::FileMethods;
15use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString;
16use crate::dom::bindings::error::{Error, Fallible};
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::bindings::str::DOMString;
21use crate::dom::blob::{Blob, blob_parts_to_bytes, normalize_type_string};
22use crate::dom::globalscope::GlobalScope;
23use crate::dom::window::Window;
24use crate::script_runtime::CanGc;
25
26#[dom_struct]
27pub(crate) struct File {
28    blob: Blob,
29    name: DOMString,
30    modified: SystemTime,
31}
32
33impl File {
34    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
35    fn new_inherited(blob_impl: &BlobImpl, name: DOMString, modified: Option<SystemTime>) -> File {
36        File {
37            blob: Blob::new_inherited(blob_impl),
38            name,
39            // https://w3c.github.io/FileAPI/#dfn-lastModified
40            modified: modified.unwrap_or_else(SystemTime::now),
41        }
42    }
43
44    pub(crate) fn new(
45        global: &GlobalScope,
46        blob_impl: BlobImpl,
47        name: DOMString,
48        modified: Option<SystemTime>,
49        can_gc: CanGc,
50    ) -> DomRoot<File> {
51        Self::new_with_proto(global, None, blob_impl, name, modified, can_gc)
52    }
53
54    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
55    fn new_with_proto(
56        global: &GlobalScope,
57        proto: Option<HandleObject>,
58        blob_impl: BlobImpl,
59        name: DOMString,
60        modified: Option<SystemTime>,
61        can_gc: CanGc,
62    ) -> DomRoot<File> {
63        let file = reflect_dom_object_with_proto(
64            Box::new(File::new_inherited(&blob_impl, name, modified)),
65            global,
66            proto,
67            can_gc,
68        );
69        global.track_file(&file, blob_impl);
70        file
71    }
72
73    // Construct from selected file message from file manager thread
74    pub(crate) fn new_from_selected(
75        window: &Window,
76        selected: SelectedFile,
77        can_gc: CanGc,
78    ) -> DomRoot<File> {
79        let name = DOMString::from(
80            selected
81                .filename
82                .to_str()
83                .expect("File name encoding error"),
84        );
85
86        File::new(
87            window.upcast(),
88            BlobImpl::new_from_file(
89                selected.id,
90                selected.filename,
91                selected.size,
92                normalize_type_string(&selected.type_string.to_string()),
93            ),
94            name,
95            Some(selected.modified),
96            can_gc,
97        )
98    }
99
100    pub(crate) fn file_bytes(&self) -> Result<Vec<u8>, ()> {
101        self.blob.get_bytes()
102    }
103
104    pub(crate) fn name(&self) -> &DOMString {
105        &self.name
106    }
107
108    pub(crate) fn file_type(&self) -> String {
109        self.blob.type_string()
110    }
111
112    pub(crate) fn get_modified(&self) -> SystemTime {
113        self.modified
114    }
115}
116
117impl FileMethods<crate::DomTypeHolder> for File {
118    // https://w3c.github.io/FileAPI/#file-constructor
119    #[allow(non_snake_case)]
120    fn Constructor(
121        global: &GlobalScope,
122        proto: Option<HandleObject>,
123        can_gc: CanGc,
124        fileBits: Vec<ArrayBufferOrArrayBufferViewOrBlobOrString>,
125        filename: DOMString,
126        filePropertyBag: &FileBinding::FilePropertyBag,
127    ) -> Fallible<DomRoot<File>> {
128        let bytes: Vec<u8> = match blob_parts_to_bytes(fileBits) {
129            Ok(bytes) => bytes,
130            Err(_) => return Err(Error::InvalidCharacter),
131        };
132
133        let blobPropertyBag = &filePropertyBag.parent;
134        let modified = filePropertyBag
135            .lastModified
136            .map(|modified| OffsetDateTime::UNIX_EPOCH + Duration::milliseconds(modified))
137            .map(Into::into);
138
139        let type_string = normalize_type_string(blobPropertyBag.type_.as_ref());
140        Ok(File::new_with_proto(
141            global,
142            proto,
143            BlobImpl::new_from_bytes(bytes, type_string),
144            filename,
145            modified,
146            can_gc,
147        ))
148    }
149
150    // https://w3c.github.io/FileAPI/#dfn-name
151    fn Name(&self) -> DOMString {
152        self.name.clone()
153    }
154
155    // https://w3c.github.io/FileAPI/#dfn-lastModified
156    fn LastModified(&self) -> i64 {
157        // This is first converted to a `time::OffsetDateTime` because it might be from before the
158        // Unix epoch in which case we will need to return a negative duration to script.
159        (OffsetDateTime::from(self.modified) - OffsetDateTime::UNIX_EPOCH).whole_milliseconds()
160            as i64
161    }
162}