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", | ||||
|  "thiserror", | ||||
|  "tokio", | ||||
|  "urlencoding", | ||||
|  "uuid 1.3.0", | ||||
|  "walkdir", | ||||
| ] | ||||
| @@ -3252,6 +3253,12 @@ dependencies = [ | ||||
|  "serde", | ||||
| ] | ||||
|  | ||||
| [[package]] | ||||
| name = "urlencoding" | ||||
| version = "2.1.2" | ||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
| checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" | ||||
|  | ||||
| [[package]] | ||||
| name = "utf-8" | ||||
| version = "0.7.6" | ||||
|   | ||||
| @@ -14,7 +14,7 @@ tauri-build = { version = "1.2", features = [] } | ||||
|  | ||||
| [dependencies] | ||||
| 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_json = "1.0" | ||||
| chrono = { version = "0.4.23", features = ["serde"] } | ||||
| @@ -27,6 +27,7 @@ image = "0.24.5" | ||||
| uuid = "1.3.0" | ||||
| mountpoints = "0.2.1" | ||||
| md-5 = "0.10.5" | ||||
| urlencoding = "2.1.2" | ||||
|  | ||||
| [features] | ||||
| # 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")] | ||||
|  | ||||
| mod commands; | ||||
| mod protocol; | ||||
| mod utils; | ||||
|  | ||||
| use commands::AppHold; | ||||
| use protocol::comic_protocol; | ||||
| use tauri::Manager; | ||||
|  | ||||
| fn main() { | ||||
|     tauri::Builder::default() | ||||
|         .register_uri_scheme_protocol("comic", comic_protocol) | ||||
|         .invoke_handler(tauri::generate_handler![ | ||||
|             commands::prelude::scan_directory, | ||||
|             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 | ||||
|       }, | ||||
|       "protocol": { | ||||
|         "all": false, | ||||
|         "asset": true, | ||||
|         "assetScope": [ | ||||
|           "*" | ||||
|         ] | ||||
|         "all": true | ||||
|       }, | ||||
|       "shell": { | ||||
|         "all": false, | ||||
| @@ -66,7 +62,7 @@ | ||||
|       } | ||||
|     }, | ||||
|     "security": { | ||||
|       "csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost" | ||||
|       "csp": null | ||||
|     }, | ||||
|     "updater": { | ||||
|       "active": false | ||||
|   | ||||
| @@ -24,7 +24,8 @@ export const useFileListStore = createStoreHook<FileListState & FileListActions> | ||||
|   updateFiles(files) { | ||||
|     set(df => { | ||||
|       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 | ||||
|       ); | ||||
|       df.actives = []; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user