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
Implement file related functionalities in htmlinputelement and related #11225
Changes from all commits
File filter...
Jump to…
Implement file-type functionalities in htmlinputelement and related
Changes include: - Implement file selection and other DOM behaviours in htmlinputelement - Integrate IpcSender<FileManagerThreadMsg> into ResourceThreads - Improve filemanager_thread, including adding type_string field to SelectedFile - Improve interfaces in FileList/File/Blob to accommodate the above changes
- Loading branch information
| @@ -3,7 +3,9 @@ | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; | ||
| use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerResult, FileManagerThreadError}; | ||
| use mime_guess::guess_mime_type_opt; | ||
| use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerResult}; | ||
| use net_traits::filemanager_thread::{SelectedFile, FileManagerThreadError}; | ||
| use std::cell::RefCell; | ||
| use std::collections::HashMap; | ||
| use std::fs::File; | ||
| @@ -17,15 +19,13 @@ pub struct FileManager { | ||
| idmap: RefCell<HashMap<Uuid, PathBuf>>, | ||
| } | ||
|
|
||
| impl FileManager { | ||
| fn new(recv: IpcReceiver<FileManagerThreadMsg>) -> FileManager { | ||
| FileManager { | ||
| receiver: recv, | ||
| idmap: RefCell::new(HashMap::new()), | ||
| } | ||
| } | ||
| pub trait FileManagerThreadFactory { | ||
| fn new() -> Self; | ||
| } | ||
|
|
||
| pub fn new_thread() -> IpcSender<FileManagerThreadMsg> { | ||
| impl FileManagerThreadFactory for IpcSender<FileManagerThreadMsg> { | ||
| /// Create a FileManagerThread | ||
| fn new() -> IpcSender<FileManagerThreadMsg> { | ||
| let (chan, recv) = ipc::channel().unwrap(); | ||
|
|
||
| spawn_named("FileManager".to_owned(), move || { | ||
| @@ -34,6 +34,16 @@ impl FileManager { | ||
|
|
||
| chan | ||
| } | ||
| } | ||
|
|
||
|
|
||
| impl FileManager { | ||
| fn new(recv: IpcReceiver<FileManagerThreadMsg>) -> FileManager { | ||
| FileManager { | ||
| receiver: recv, | ||
| idmap: RefCell::new(HashMap::new()), | ||
| } | ||
| } | ||
|
|
||
| /// Start the file manager event loop | ||
| fn start(&mut self) { | ||
| @@ -49,7 +59,7 @@ impl FileManager { | ||
| } | ||
|
|
||
| impl FileManager { | ||
| fn select_file(&mut self, sender: IpcSender<FileManagerResult<(Uuid, PathBuf, u64)>>) { | ||
| fn select_file(&mut self, sender: IpcSender<FileManagerResult<SelectedFile>>) { | ||
| // TODO: Pull the dialog UI in and get selected | ||
| let selected_path = Path::new(""); | ||
|
|
||
| @@ -63,7 +73,7 @@ impl FileManager { | ||
| } | ||
| } | ||
|
|
||
| fn select_files(&mut self, sender: IpcSender<FileManagerResult<Vec<(Uuid, PathBuf, u64)>>>) { | ||
| fn select_files(&mut self, sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>) { | ||
| let selected_paths = vec![Path::new("")]; | ||
|
|
||
| let mut replies = vec![]; | ||
| @@ -81,7 +91,7 @@ impl FileManager { | ||
| let _ = sender.send(Ok(replies)); | ||
| } | ||
|
|
||
| fn create_entry(&mut self, file_path: &Path) -> Option<(Uuid, PathBuf, u64)> { | ||
| fn create_entry(&mut self, file_path: &Path) -> Option<SelectedFile> { | ||
| match File::open(file_path) { | ||
| Ok(handler) => { | ||
| let id = Uuid::new_v4(); | ||
| @@ -100,7 +110,19 @@ impl FileManager { | ||
| let filename = file_path.file_name(); | ||
|
|
||
| match (epoch, filename) { | ||
| (Ok(epoch), Some(filename)) => Some((id, Path::new(filename).to_path_buf(), epoch)), | ||
| (Ok(epoch), Some(filename)) => { | ||
| let filename_path = Path::new(filename); | ||
| let mime = guess_mime_type_opt(filename_path); | ||
izgzhen
Author
Contributor
|
||
| Some(SelectedFile { | ||
| id: id, | ||
| filename: filename_path.to_path_buf(), | ||
| modified: epoch, | ||
| type_string: match mime { | ||
| Some(x) => format!("{}", x), | ||
| None => "".to_string(), | ||
| }, | ||
| }) | ||
| } | ||
| _ => None | ||
| } | ||
| }, | ||
| @@ -27,8 +27,10 @@ impl FileList { | ||
| } | ||
|
|
||
| #[allow(unrooted_must_root)] | ||
| pub fn new(window: &Window, files: Vec<JS<File>>) -> Root<FileList> { | ||
| reflect_dom_object(box FileList::new_inherited(files), GlobalRef::Window(window), FileListBinding::Wrap) | ||
| pub fn new(window: &Window, files: Vec<Root<File>>) -> Root<FileList> { | ||
| reflect_dom_object(box FileList::new_inherited(files.iter().map(|r| JS::from_rooted(&r)).collect()), | ||
Manishearth
Member
|
||
| GlobalRef::Window(window), | ||
| FileListBinding::Wrap) | ||
| } | ||
| } | ||
|
|
||
We shouldn't be guessing the MIME here, right?