enhance(fs):缩短文件读取流程,减少内存复制。

This commit is contained in:
徐涛 2023-03-21 06:13:20 +08:00
parent 48fbe067a0
commit c12a0075e7
3 changed files with 32 additions and 6 deletions

26
src-tauri/Cargo.lock generated
View File

@ -332,6 +332,7 @@ dependencies = [
"chrono",
"image",
"md-5",
"mime_guess",
"mountpoints",
"once_cell",
"serde",
@ -1617,6 +1618,22 @@ dependencies = [
"autocfg",
]
[[package]]
name = "mime"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.6.2"
@ -3208,6 +3225,15 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.10"

View File

@ -28,6 +28,7 @@ uuid = "1.3.0"
mountpoints = "0.2.1"
md-5 = "0.10.5"
urlencoding = "2.1.2"
mime_guess = "2.0.4"
[features]
# this feature is used for production builds or when `devPath` points to the filesystem

View File

@ -1,4 +1,4 @@
use std::{error::Error, io::Cursor, path::Path};
use std::{error::Error, fs, path::Path};
use tauri::{
http::{Request, Response, ResponseBuilder},
@ -32,12 +32,11 @@ pub fn comic_protocol<R: Runtime>(
.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)?;
let mimetype = mime_guess::from_path(path);
let content = fs::read(path)?;
response_builder
.mimetype("image/png")
.mimetype(mimetype.first_or_text_plain().essence_str())
.status(200)
.body(bytes)
.body(content)
}