script/dom/file/
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 dom_struct::dom_struct;
8use embedder_traits::SelectedFile;
9use js::rust::HandleObject;
10use servo_base::id::{FileId, FileIndex};
11use servo_constellation_traits::{BlobImpl, SerializableFile};
12use time::{Duration, OffsetDateTime};
13
14use crate::dom::bindings::codegen::Bindings::FileBinding;
15use crate::dom::bindings::codegen::Bindings::FileBinding::FileMethods;
16use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString;
17use crate::dom::bindings::error::{Error, Fallible};
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::serializable::Serializable;
22use crate::dom::bindings::str::DOMString;
23use crate::dom::bindings::structuredclone::StructuredData;
24use crate::dom::blob::{Blob, blob_parts_to_bytes, normalize_type_string};
25use crate::dom::globalscope::GlobalScope;
26use crate::dom::window::Window;
27use crate::script_runtime::CanGc;
28
29#[dom_struct]
30pub(crate) struct File {
31    blob: Blob,
32    name: DOMString,
33    modified: SystemTime,
34}
35
36impl File {
37    fn new_inherited(blob_impl: &BlobImpl, name: DOMString, modified: Option<SystemTime>) -> File {
38        File {
39            blob: Blob::new_inherited(blob_impl),
40            name,
41            // https://w3c.github.io/FileAPI/#dfn-lastModified
42            modified: modified.unwrap_or_else(SystemTime::now),
43        }
44    }
45
46    pub(crate) fn new(
47        global: &GlobalScope,
48        blob_impl: BlobImpl,
49        name: DOMString,
50        modified: Option<SystemTime>,
51        can_gc: CanGc,
52    ) -> DomRoot<File> {
53        Self::new_with_proto(global, None, blob_impl, name, modified, can_gc)
54    }
55
56    fn new_with_proto(
57        global: &GlobalScope,
58        proto: Option<HandleObject>,
59        blob_impl: BlobImpl,
60        name: DOMString,
61        modified: Option<SystemTime>,
62        can_gc: CanGc,
63    ) -> DomRoot<File> {
64        let file = reflect_dom_object_with_proto(
65            Box::new(File::new_inherited(&blob_impl, name, modified)),
66            global,
67            proto,
68            can_gc,
69        );
70        global.track_file(&file, blob_impl);
71        file
72    }
73
74    // Construct from selected file message from file manager thread
75    pub(crate) fn new_from_selected(
76        window: &Window,
77        selected: SelectedFile,
78        can_gc: CanGc,
79    ) -> DomRoot<File> {
80        let name = DOMString::from(
81            selected
82                .filename
83                .to_str()
84                .expect("File name encoding error"),
85        );
86
87        File::new(
88            window.upcast(),
89            BlobImpl::new_from_file(
90                selected.id,
91                selected.filename,
92                selected.size,
93                normalize_type_string(&selected.type_string.to_string()),
94            ),
95            name,
96            Some(selected.modified),
97            can_gc,
98        )
99    }
100
101    pub(crate) fn file_bytes(&self) -> Result<Vec<u8>, ()> {
102        self.blob.get_bytes()
103    }
104
105    pub(crate) fn name(&self) -> &DOMString {
106        &self.name
107    }
108
109    pub(crate) fn file_type(&self) -> String {
110        self.blob.type_string()
111    }
112
113    pub(crate) fn get_modified(&self) -> SystemTime {
114        self.modified
115    }
116
117    pub(crate) fn serialized_data(&self) -> Result<SerializableFile, ()> {
118        let (_, blob_impl) = self.upcast::<Blob>().serialize()?;
119        Ok(SerializableFile {
120            blob_impl,
121            name: self.name.to_string(),
122            modified: self.LastModified(),
123        })
124    }
125}
126
127impl Serializable for File {
128    type Index = FileIndex;
129    type Data = SerializableFile;
130
131    /// <https://html.spec.whatwg.org/multipage/#serialization-steps>
132    fn serialize(&self) -> Result<(FileId, SerializableFile), ()> {
133        Ok((FileId::new(), self.serialized_data()?))
134    }
135
136    /// <https://html.spec.whatwg.org/multipage/#deserialization-steps>
137    fn deserialize(
138        owner: &GlobalScope,
139        serialized: SerializableFile,
140        can_gc: CanGc,
141    ) -> Result<DomRoot<Self>, ()> {
142        let modified = OffsetDateTime::UNIX_EPOCH + Duration::milliseconds(serialized.modified);
143        Ok(File::new(
144            owner,
145            serialized.blob_impl,
146            serialized.name.into(),
147            Some(modified.into()),
148            can_gc,
149        ))
150    }
151
152    fn serialized_storage<'a>(
153        reader: StructuredData<'a, '_>,
154    ) -> &'a mut Option<rustc_hash::FxHashMap<FileId, Self::Data>> {
155        match reader {
156            StructuredData::Reader(r) => &mut r.files,
157            StructuredData::Writer(w) => &mut w.files,
158        }
159    }
160}
161
162impl FileMethods<crate::DomTypeHolder> for File {
163    // https://w3c.github.io/FileAPI/#file-constructor
164    #[expect(non_snake_case)]
165    fn Constructor(
166        global: &GlobalScope,
167        proto: Option<HandleObject>,
168        can_gc: CanGc,
169        fileBits: Vec<ArrayBufferOrArrayBufferViewOrBlobOrString>,
170        filename: DOMString,
171        filePropertyBag: &FileBinding::FilePropertyBag,
172    ) -> Fallible<DomRoot<File>> {
173        let bytes: Vec<u8> = match blob_parts_to_bytes(fileBits) {
174            Ok(bytes) => bytes,
175            Err(_) => return Err(Error::InvalidCharacter(None)),
176        };
177
178        let blobPropertyBag = &filePropertyBag.parent;
179        let modified = filePropertyBag
180            .lastModified
181            .map(|modified| OffsetDateTime::UNIX_EPOCH + Duration::milliseconds(modified))
182            .map(Into::into);
183
184        let type_string = normalize_type_string(&blobPropertyBag.type_.str());
185        Ok(File::new_with_proto(
186            global,
187            proto,
188            BlobImpl::new_from_bytes(bytes, type_string),
189            filename,
190            modified,
191            can_gc,
192        ))
193    }
194
195    /// <https://w3c.github.io/FileAPI/#dfn-name>
196    fn Name(&self) -> DOMString {
197        self.name.clone()
198    }
199
200    /// <https://w3c.github.io/FileAPI/#dfn-lastModified>
201    fn LastModified(&self) -> i64 {
202        // This is first converted to a `time::OffsetDateTime` because it might be from before the
203        // Unix epoch in which case we will need to return a negative duration to script.
204        (OffsetDateTime::from(self.modified) - OffsetDateTime::UNIX_EPOCH).whole_milliseconds()
205            as i64
206    }
207}