Skip to content

Commit

Permalink
šŸž fix: Fix error when non-ascii characters are in the file path for Mā€¦
Browse files Browse the repository at this point in the history
ā€¦acOS
  • Loading branch information
caoccao committed Jan 13, 2025
1 parent 1977a55 commit 1f986b2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/linux_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ jobs:
- name: Build BetterMediaInfo
run: |
pnpm install
cd src-tauri
cargo test -r
cd ..
pnpm tauri build
- name: Upload the deb
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/macos_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ jobs:
- name: Build BetterMediaInfo
run: |
pnpm install
cd src-tauri
cargo test -r
cd ..
pnpm tauri build
- name: Upload the Artifact
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/windows_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ jobs:
- name: Build BetterMediaInfo
run: |
pnpm install
cd src-tauri
cargo test -r
cd ..
pnpm tauri build
- name: Upload the msi
Expand Down
45 changes: 23 additions & 22 deletions docs/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# Release Notes

## 0.4.0

* Upgraded Rust toolchain to v1.82.0
* Upgraded MediaInfoLib to v24.12

## 0.3.0

* Upgraded tauri to v2
* Upgraded MediaInfoLib to v24.11

## 0.2.0

* Supported media file comparison view

## 0.1.0

* Supported MediaInfoLib v24.6
* Supported List View
* Supported Detail View
* Supported Json
# Release Notes

## 0.4.0

* Upgraded Rust toolchain to v1.82.0
* Upgraded MediaInfoLib to v24.12
* Fixed error when non-ascii characters are in the file path for MacOS

## 0.3.0

* Upgraded tauri to v2
* Upgraded MediaInfoLib to v24.11

## 0.2.0

* Supported media file comparison view

## 0.1.0

* Supported MediaInfoLib v24.6
* Supported List View
* Supported Detail View
* Supported Json
65 changes: 65 additions & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ anyhow = "1.0.86"
once_cell = "1.19.0"
env_logger = "0.11.5"
widestring = "1.1.0"
encoding = "0.2.33"

[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]
# https://crates.io/crates/tauri-plugin-cli
Expand Down
63 changes: 62 additions & 1 deletion src-tauri/src/media_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)]

use anyhow::{anyhow, Result};
use encoding::Encoding;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::os::raw;
Expand Down Expand Up @@ -60,7 +61,22 @@ extern "C" {
}

fn to_wchars(s: &str) -> Vec<mi_wchar> {
WideCString::from_str_truncate(s).into_vec()
let wchars = WideCString::from_str_truncate(s).into_vec();
#[cfg(target_os = "windows")]
{
wchars
}
#[cfg(not(target_os = "windows"))]
if wchars.len() > 0 && wchars.iter().any(|&c| c >= 0x100) {
encoding::all::UTF_8
.encode(s, encoding::EncoderTrap::Strict)
.unwrap()
.into_iter()
.map(|c| c as mi_wchar)
.collect::<Vec<mi_wchar>>()
} else {
wchars
}
}

fn from_wchars(pointer: *const mi_wchar) -> String {
Expand Down Expand Up @@ -336,3 +352,48 @@ impl MediaInfoFile {
}
}
}

#[test]
fn test_ascii_path() {
let path = Path::new("./icons/icon.png");
let path = std::path::absolute(path).unwrap();
if let Some(path) = path.to_str() {
let path = to_wchars(path);
let handle = unsafe { MediaInfo_New() };
let error_code = unsafe { MediaInfo_Open(handle, path.as_ptr()) };
assert_eq!(error_code, 1, "Error code should be 1.");
let count = unsafe { MediaInfo_Count_Get(handle, MediaInfoStreamKind::General as mi_kind, usize::MAX) };
assert_eq!(count, 1, "General stream count should be 1.");
} else {
assert!(false, "Failed to convert file path to string in MediaInfo::open().");
}
}

#[test]
fn test_non_ascii_path() {
let source_path = Path::new("./icons/icon.png");
let source_path = std::path::absolute(source_path).unwrap();
let temp_dir = std::env::temp_dir();
let target_path = temp_dir.join("bettermięø¬č©¦.png");
if target_path.exists() {
let result = std::fs::remove_file(&target_path);
assert!(
result.is_ok(),
"Failed to remove file {}",
target_path.to_str().unwrap()
);
}
std::fs::copy(source_path, &target_path).unwrap();
let path = to_wchars(target_path.to_str().unwrap());
let handle = unsafe { MediaInfo_New() };
let error_code = unsafe { MediaInfo_Open(handle, path.as_ptr()) };
assert_eq!(error_code, 1, "Error code should be 1.");
let count = unsafe { MediaInfo_Count_Get(handle, MediaInfoStreamKind::General as mi_kind, usize::MAX) };
let result = std::fs::remove_file(&target_path);
assert!(
result.is_ok(),
"Failed to remove file {}",
target_path.to_str().unwrap()
);
assert_eq!(count, 1, "General stream count should be 1.");
}

0 comments on commit 1f986b2

Please sign in to comment.