feat(fs):通过实现自定义协议支持图片的读取。
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user