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

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)
}