feat(fs):通过实现自定义协议支持图片的读取。
This commit is contained in:
		
							
								
								
									
										7
									
								
								src-tauri/Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										7
									
								
								src-tauri/Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -341,6 +341,7 @@ dependencies = [ | |||||||
|  "tauri-build", |  "tauri-build", | ||||||
|  "thiserror", |  "thiserror", | ||||||
|  "tokio", |  "tokio", | ||||||
|  |  "urlencoding", | ||||||
|  "uuid 1.3.0", |  "uuid 1.3.0", | ||||||
|  "walkdir", |  "walkdir", | ||||||
| ] | ] | ||||||
| @@ -3252,6 +3253,12 @@ dependencies = [ | |||||||
|  "serde", |  "serde", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "urlencoding" | ||||||
|  | version = "2.1.2" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "utf-8" | name = "utf-8" | ||||||
| version = "0.7.6" | version = "0.7.6" | ||||||
|   | |||||||
| @@ -14,7 +14,7 @@ tauri-build = { version = "1.2", features = [] } | |||||||
|  |  | ||||||
| [dependencies] | [dependencies] | ||||||
| once_cell = "1.17.0" | once_cell = "1.17.0" | ||||||
| tauri = { version = "1.2", features = ["dialog-open", "fs-exists", "fs-read-dir", "fs-read-file", "protocol-asset", "shell-open"] } | tauri = { version = "1.2", features = ["dialog-open", "fs-exists", "fs-read-dir", "fs-read-file", "protocol-all", "shell-open"] } | ||||||
| serde = { version = "1.0", features = ["derive"] } | serde = { version = "1.0", features = ["derive"] } | ||||||
| serde_json = "1.0" | serde_json = "1.0" | ||||||
| chrono = { version = "0.4.23", features = ["serde"] } | chrono = { version = "0.4.23", features = ["serde"] } | ||||||
| @@ -27,6 +27,7 @@ image = "0.24.5" | |||||||
| uuid = "1.3.0" | uuid = "1.3.0" | ||||||
| mountpoints = "0.2.1" | mountpoints = "0.2.1" | ||||||
| md-5 = "0.10.5" | md-5 = "0.10.5" | ||||||
|  | urlencoding = "2.1.2" | ||||||
|  |  | ||||||
| [features] | [features] | ||||||
| # this feature is used for production builds or when `devPath` points to the filesystem | # this feature is used for production builds or when `devPath` points to the filesystem | ||||||
|   | |||||||
| @@ -2,13 +2,16 @@ | |||||||
| #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] | #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] | ||||||
|  |  | ||||||
| mod commands; | mod commands; | ||||||
|  | mod protocol; | ||||||
| mod utils; | mod utils; | ||||||
|  |  | ||||||
| use commands::AppHold; | use commands::AppHold; | ||||||
|  | use protocol::comic_protocol; | ||||||
| use tauri::Manager; | use tauri::Manager; | ||||||
|  |  | ||||||
| fn main() { | fn main() { | ||||||
|     tauri::Builder::default() |     tauri::Builder::default() | ||||||
|  |         .register_uri_scheme_protocol("comic", comic_protocol) | ||||||
|         .invoke_handler(tauri::generate_handler![ |         .invoke_handler(tauri::generate_handler![ | ||||||
|             commands::prelude::scan_directory, |             commands::prelude::scan_directory, | ||||||
|             commands::prelude::show_drives, |             commands::prelude::show_drives, | ||||||
|   | |||||||
							
								
								
									
										43
									
								
								src-tauri/src/protocol.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src-tauri/src/protocol.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | use std::{error::Error, io::Cursor, path::Path}; | ||||||
|  |  | ||||||
|  | use tauri::{ | ||||||
|  |     http::{Request, Response, ResponseBuilder}, | ||||||
|  |     AppHandle, Runtime, | ||||||
|  | }; | ||||||
|  | use urlencoding::decode; | ||||||
|  |  | ||||||
|  | pub fn comic_protocol<R: Runtime>( | ||||||
|  |     _app_handler: &AppHandle<R>, | ||||||
|  |     request: &Request, | ||||||
|  | ) -> Result<Response, Box<dyn Error>> { | ||||||
|  |     let response_builder = ResponseBuilder::new(); | ||||||
|  |     let path = request | ||||||
|  |         .uri() | ||||||
|  |         .strip_prefix("comic://localhost/") | ||||||
|  |         .map(|u| decode(u).unwrap()) | ||||||
|  |         .unwrap(); | ||||||
|  |     println!("[debug]request: {}", path); | ||||||
|  |     if path.len() == 0 { | ||||||
|  |         return response_builder | ||||||
|  |             .mimetype("text/plain") | ||||||
|  |             .status(404) | ||||||
|  |             .body(Vec::new()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     let path = Path::new(path.as_ref()); | ||||||
|  |     if !path.exists() { | ||||||
|  |         return response_builder | ||||||
|  |             .mimetype("text/plain") | ||||||
|  |             .status(404) | ||||||
|  |             .body(Vec::new()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     let content = image::io::Reader::open(path)?.decode()?; | ||||||
|  |     let mut bytes: Vec<u8> = Vec::new(); | ||||||
|  |     content.write_to(&mut Cursor::new(&mut bytes), image::ImageOutputFormat::Png)?; | ||||||
|  |  | ||||||
|  |     response_builder | ||||||
|  |         .mimetype("image/png") | ||||||
|  |         .status(200) | ||||||
|  |         .body(bytes) | ||||||
|  | } | ||||||
| @@ -29,11 +29,7 @@ | |||||||
|         "writeFile": false |         "writeFile": false | ||||||
|       }, |       }, | ||||||
|       "protocol": { |       "protocol": { | ||||||
|         "all": false, |         "all": true | ||||||
|         "asset": true, |  | ||||||
|         "assetScope": [ |  | ||||||
|           "*" |  | ||||||
|         ] |  | ||||||
|       }, |       }, | ||||||
|       "shell": { |       "shell": { | ||||||
|         "all": false, |         "all": false, | ||||||
| @@ -66,7 +62,7 @@ | |||||||
|       } |       } | ||||||
|     }, |     }, | ||||||
|     "security": { |     "security": { | ||||||
|       "csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost" |       "csp": null | ||||||
|     }, |     }, | ||||||
|     "updater": { |     "updater": { | ||||||
|       "active": false |       "active": false | ||||||
|   | |||||||
| @@ -24,7 +24,8 @@ export const useFileListStore = createStoreHook<FileListState & FileListActions> | |||||||
|   updateFiles(files) { |   updateFiles(files) { | ||||||
|     set(df => { |     set(df => { | ||||||
|       df.files = addIndex<Omit<FileItem, 'sort'>, FileItem>(map)( |       df.files = addIndex<Omit<FileItem, 'sort'>, FileItem>(map)( | ||||||
|         (item, index) => mergeLeft({ sort: index * 10, path: convertFileSrc(item.path) }, item), |         (item, index) => | ||||||
|  |           mergeLeft({ sort: index * 10, path: convertFileSrc(item.path, 'comic') }, item), | ||||||
|         files |         files | ||||||
|       ); |       ); | ||||||
|       df.actives = []; |       df.actives = []; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user