Skip to content

Commit

Permalink
feat: notify target filename instead of source filename. add debug lo…
Browse files Browse the repository at this point in the history
…g for `cargo build --debug`
  • Loading branch information
Yangmoooo committed Dec 24, 2024
1 parent f181e9b commit 468d3cf
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 36 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- 在密码库中未找到密码时,弹窗提示输入密码(仅限 Windows)

## [1.1.2] - 2024-12-24

### Changed

- 现在仅当压缩包内只含一个文件时,才直接提取至当前文件夹
- 桌面通知在完成时会展示提取出的文件(夹)名,而非原压缩包名

### Fixed

- 修复了 zip 分卷的残余(`.zip``.z01``.z02`...)文件未被清理的问题
- 修复了提取隐写文件后,桌面通知的文件名为 `2.zip` 的问题
- 修复了在 Windows 上当压缩包名以空格结尾时,创建的解压文件夹无法访问的问题(这是由于 NTFS 不允许文件名以空格结尾,但强行创建了该文件夹导致的)

## [1.1.1] - 2024-12-06

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ezz"
version = "1.1.1"
version = "1.1.2"
edition = "2021"
build = "build.rs"

Expand All @@ -15,4 +15,7 @@ simplelog = "0.12"
notify-rust = "4"
home = "0.5"
encoding_rs = "0.8"
windows = { version = "0.58", features = ["Win32_Globalization", "Win32_UI_WindowsAndMessaging"] }
windows = { version = "0.58", features = [
"Win32_Globalization",
"Win32_UI_WindowsAndMessaging",
] }
34 changes: 22 additions & 12 deletions src/decompress/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ pub fn derive_dir(archive: &Path) -> EzzResult<PathBuf> {
let archive_stem = archive
.file_stem()
.and_then(|s| s.to_str())
.ok_or(EzzError::FileNameError)?
.ok_or(EzzError::PathError)?
.trim_end();
let dir = archive
.parent()
.ok_or(EzzError::FilePathError)?
.ok_or(EzzError::PathError)?
.join(archive_stem);
Ok(dir)
}
Expand All @@ -23,26 +23,36 @@ pub fn remove_dir(dir: &Path) -> EzzResult<()> {
Ok(())
}

pub fn flatten_dir(dir: &Path) -> EzzResult<()> {
pub fn flatten_dir(dir: &Path) -> EzzResult<String> {
log::debug!("Flattening directory: {dir:?}");
if !dir.is_dir() {
return Err(EzzError::FilePathError);
return Err(EzzError::PathError);
}
let parent = dir.parent().ok_or(EzzError::FilePathError)?;
let parent = dir.parent().ok_or(EzzError::PathError)?;
let entries: Vec<PathBuf> = fs::read_dir(dir)?
.filter_map(|entry| entry.ok().map(|e| e.path()))
.collect();
let mut filename = dir
.file_name()
.ok_or(EzzError::PathError)?
.to_string_lossy()
.into_owned();

if entries.len() == 1 {
let entry = entries.first().ok_or(EzzError::FilePathError)?;
let target_path = parent.join(entry.file_name().ok_or(EzzError::FileNameError)?);
log::debug!("Moving single entry to parent directory");
let entry = entries.first().ok_or(EzzError::PathError)?;
let entryname = entry.file_name().ok_or(EzzError::PathError)?;
filename = entryname.to_string_lossy().into_owned();

let target_path = parent.join(entryname);
// 若为 `.zip.7z` 这种嵌套的情况,内层压缩包名称可能会与解压目录冲突,故使用临时名称
let tmp_path = target_path.with_extension("tmp");

if target_path.exists() {
if target_path.is_dir() {
fs::rename(entry, &tmp_path)?;
} else {
return Err(EzzError::FilePathError);
return Err(EzzError::PathError);
}
} else {
fs::rename(entry, target_path)?;
Expand All @@ -53,7 +63,7 @@ pub fn flatten_dir(dir: &Path) -> EzzResult<()> {
fs::rename(tmp_path, dir)?;
}
}
Ok(())
Ok(filename)
}

enum MultiVolumeKind {
Expand Down Expand Up @@ -94,10 +104,10 @@ fn get_multivolume_kind(archive: &Path) -> MultiVolumeKind {
}

fn remove_multivolume(kind: MultiVolumeKind, archive: &Path, seq: usize) -> EzzResult<()> {
let parent = archive.parent().ok_or(EzzError::FilePathError)?;
let parent = archive.parent().ok_or(EzzError::PathError)?;
let file_stem = archive
.file_stem()
.ok_or(EzzError::FileNameError)?
.ok_or(EzzError::PathError)?
.to_string_lossy();
let mut volume_path = PathBuf::new();
match kind {
Expand All @@ -110,7 +120,7 @@ fn remove_multivolume(kind: MultiVolumeKind, archive: &Path, seq: usize) -> EzzR
let file_stem = file_stem
.trim_end_matches(char::is_numeric)
.strip_suffix(".part")
.ok_or(EzzError::FileNameError)?;
.ok_or(EzzError::PathError)?;
let volume_name = format!("{}.part{}.rar", file_stem, seq);
volume_path = parent.join(volume_name);
}
Expand Down
20 changes: 9 additions & 11 deletions src/decompress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ use sevenzip::*;

pub fn extract(archive: &Path, pw: Option<&str>, db: Option<&Path>) -> EzzResult<String> {
let mut archive = archive.to_path_buf();
let filename = archive
.file_name()
.ok_or(EzzError::FileNameError)?
.to_string_lossy()
.into_owned();
let zz = setup_7zz()?;
log::debug!("7-Zip Path: {zz:?}");

if is_stego(&archive) {
log::debug!("Stego file detected: {archive:?}");
handle_output(command_for_stego(&zz, &archive)?)?;
remove_archive(&archive)?;
archive = archive.with_file_name("2.zip");
}

if let Some(password) = pw {
extract_with_pw(&zz, &archive, password)?;
let filename = if let Some(password) = pw {
extract_with_pw(&zz, &archive, password)?
} else {
extract_with_db(&zz, &archive, db)?;
}
extract_with_db(&zz, &archive, db)?
};

log::debug!("Removing 7-Zip executable");
teardown_7zz()?;
Ok(filename)
}
Expand All @@ -42,7 +40,7 @@ fn is_stego(file: &Path) -> bool {
)
}

fn extract_with_pw(zz: &str, archive: &Path, pw: &str) -> EzzResult<()> {
fn extract_with_pw(zz: &str, archive: &Path, pw: &str) -> EzzResult<String> {
let output = command_x(zz, archive, pw)?;
let dir = derive_dir(archive)?;
if let Err(e) = handle_output(output) {
Expand All @@ -53,7 +51,7 @@ fn extract_with_pw(zz: &str, archive: &Path, pw: &str) -> EzzResult<()> {
flatten_dir(&dir)
}

fn extract_with_db(zz: &str, archive: &Path, db: Option<&Path>) -> EzzResult<()> {
fn extract_with_db(zz: &str, archive: &Path, db: Option<&Path>) -> EzzResult<String> {
let db = match db {
Some(path) => path,
None => &locate_db()?,
Expand Down
4 changes: 2 additions & 2 deletions src/decompress/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::types::{EzzError, EzzResult};
pub fn locate_db() -> EzzResult<PathBuf> {
let name = "ezz.db.txt";
let ezz_path = env::current_exe()?;
let home_dir = home::home_dir().ok_or(EzzError::FilePathError)?;
let dirs = [ezz_path.parent().ok_or(EzzError::FilePathError)?, &home_dir];
let home_dir = home::home_dir().ok_or(EzzError::PathError)?;
let dirs = [ezz_path.parent().ok_or(EzzError::PathError)?, &home_dir];

dirs.iter()
.map(|dir| dir.join(name))
Expand Down
2 changes: 1 addition & 1 deletion src/decompress/sevenzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn command_x(zz: &str, archive: &Path, pw: &str) -> EzzResult<Output> {
}

pub fn command_for_stego(zz: &str, video: &Path) -> EzzResult<Output> {
let dir = video.parent().ok_or(EzzError::FilePathError)?;
let dir = video.parent().ok_or(EzzError::PathError)?;
let output_switch = format!("-o{}", dir.to_string_lossy().into_owned());
let video_name = video.to_string_lossy().into_owned();
let mut cmd = Command::new(zz);
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn main() {

match run() {
Ok(filename) => {
notify!(Msg::Ok, "解压成功:\n{filename} 处理完毕",);
info!("Done. {filename} has been processed",);
notify!(Msg::Ok, "解压成功:\n已保存至 {filename}",);
info!("Done. Saved to {filename:?}");
}
Err(e) => {
notify!(Msg::Err, "解压失败:\n{e}");
Expand All @@ -48,7 +48,11 @@ fn init_logger() -> EzzResult<()> {
.build();
let log_path = env::current_exe()?.with_file_name("ezz.log");
WriteLogger::init(
LevelFilter::Info,
if cfg!(debug_assertions) {
LevelFilter::Debug
} else {
LevelFilter::Info
},
log_config,
File::options().append(true).create(true).open(log_path)?,
)?;
Expand Down
6 changes: 2 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ pub enum EzzError {
PasswordDbNotFound,
#[error("密码库中无匹配密码")]
NoMatchedPassword,
#[error("文件名错误")]
FileNameError,
#[error("文件路径错误")]
FilePathError,
#[error("文件路径或文件名错误")]
PathError,
}

pub type EzzResult<T> = Result<T, EzzError>;

0 comments on commit 468d3cf

Please sign in to comment.