diff --git a/src/main.rs b/src/main.rs index 176f2d1..2c20f6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,20 +51,23 @@ async fn launch() -> _ { ..Default::default() }) .mount_assets() - .mount("/", routes![ - routes::index_page, - routes::pkg_route, - routes::push::upload_pkg, - routes::user::login, - routes::user::login_post, - routes::user::account_page, - routes::ui::pkg_ui, - routes::ui::repo_ui, - routes::user::new_api_key, - routes::user::end_session, - routes::user::change_password, - routes::user::change_password_post - ]) + .mount( + "/", + routes![ + routes::index_page, + routes::pkg_route, + routes::push::upload_pkg, + routes::user::login, + routes::user::login_post, + routes::user::account_page, + routes::ui::pkg_ui, + routes::ui::repo_ui, + routes::user::new_api_key, + routes::user::end_session, + routes::user::change_password, + routes::user::change_password_post + ], + ) .manage(config) .manage(shell) } diff --git a/src/pkg/package.rs b/src/pkg/package.rs index 31a91fe..51d4ccc 100644 --- a/src/pkg/package.rs +++ b/src/pkg/package.rs @@ -1,12 +1,13 @@ use std::{ fs::File, - io::{Cursor, Read}, + io::{BufReader, Cursor, Read}, path::{Path, PathBuf}, }; use based::get_pg; use sqlx::FromRow; use tar::Archive; +use zstd::Decoder; use super::{Repository, arch::Architecture}; @@ -476,46 +477,48 @@ pub fn repo_add(db_file: &str, pkg_file: &str) { run_command(vec!["repo-add", db_file, pkg_file]); } -pub fn read_file_tar(tar: &PathBuf, file_path: &str) -> Option { - let mut file = File::open(tar).ok()?; - let mut buf = Vec::new(); - file.read_to_end(&mut buf).unwrap(); - read_file_tar_raw(&buf, file_path) -} +pub fn read_file_tar(tar: &Path, file_path: &str) -> Option { + let file = File::open(tar).ok()?; + let decoder = Decoder::new(BufReader::new(file)).ok()?; + let mut archive = Archive::new(decoder); -pub fn read_file_tar_raw(tar: &[u8], file_path: &str) -> Option { - let uncompressed = zstd::decode_all(Cursor::new(tar)).unwrap(); - let content = Cursor::new(uncompressed); - let mut a = Archive::new(content); - - for e in a.entries().ok()? { - let mut e = e.ok()?; - let path = e.path().unwrap(); - let path = path.to_str().unwrap(); - if path == file_path { - let mut file_content = Vec::new(); - e.read_to_end(&mut file_content).unwrap(); - return String::from_utf8(file_content).ok(); + for entry in archive.entries().ok()? { + let mut entry = entry.ok()?; + if entry.path().ok()?.to_str()? == file_path { + let mut file_content = String::new(); + entry.read_to_string(&mut file_content).ok()?; + return Some(file_content); } } None } -pub fn list_tar_file(tar: &PathBuf) -> Option> { - let mut file = File::open(tar).ok()?; - let mut buf = Vec::new(); - file.read_to_end(&mut buf).unwrap(); - let uncompressed = zstd::decode_all(Cursor::new(buf)).unwrap(); - let content = Cursor::new(uncompressed); - let mut a = Archive::new(content); +pub fn read_file_tar_raw(tar_data: &[u8], file_path: &str) -> Option { + let decoder = Decoder::new(Cursor::new(tar_data)).ok()?; + let mut archive = Archive::new(decoder); + + for entry in archive.entries().ok()? { + let mut entry = entry.ok()?; + if entry.path().ok()?.to_str()? == file_path { + let mut file_content = String::new(); + entry.read_to_string(&mut file_content).ok()?; + return Some(file_content); + } + } + + None +} + +pub fn list_tar_file(tar: &Path) -> Option> { + let file = File::open(tar).ok()?; + let decoder = Decoder::new(BufReader::new(file)).ok()?; + let mut archive = Archive::new(decoder); let mut paths = Vec::new(); - for e in a.entries().ok()? { - let e = e.ok()?; - let path = e.path().ok()?; - let path = path.to_str()?; - paths.push(path.to_string()); + for entry in archive.entries().ok()? { + let entry = entry.ok()?; + paths.push(entry.path().ok()?.to_string_lossy().into_owned()); } Some(paths)