Skip to content

Commit

Permalink
chore: fix typed-path with encoding (#568)
Browse files Browse the repository at this point in the history
* chore: fix typed-path with encoding

* add helper for converting typed path to platform path
  • Loading branch information
vnghia authored Dec 11, 2024
1 parent f7a8825 commit 1d4f5bf
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
3 changes: 1 addition & 2 deletions nghe-backend/src/config/cover_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub struct CoverArt {
.join("nghe")
.join("cache")
.join("cover_art")
.with_platform_encoding_checked()
.unwrap()
.with_platform_encoding()
)
))]
pub dir: Option<Utf8PlatformPathBuf>,
Expand Down
3 changes: 1 addition & 2 deletions nghe-backend/src/config/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pub struct Spotify {
.join("nghe")
.join("spotify")
.join("token.json")
.with_platform_encoding_checked()
.unwrap()
.with_platform_encoding()
)
))]
pub token_path: Option<Utf8PlatformPathBuf>,
Expand Down
3 changes: 1 addition & 2 deletions nghe-backend/src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub struct Server {
.unwrap()
.join("frontend")
.join("dist")
.with_platform_encoding_checked()
.unwrap()
.with_platform_encoding()
))]
pub frontend_dir: Utf8PlatformPathBuf,
}
Expand Down
3 changes: 1 addition & 2 deletions nghe-backend/src/config/transcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub struct Transcode {
.join("nghe")
.join("cache")
.join("transcode")
.with_platform_encoding_checked()
.unwrap()
.with_platform_encoding()
)
))]
pub cache_dir: Option<Utf8PlatformPathBuf>,
Expand Down
34 changes: 24 additions & 10 deletions nghe-backend/src/filesystem/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::Metadata;
use async_walkdir::WalkDir;
use futures_lite::stream::StreamExt;
use time::OffsetDateTime;
use typed_path::Utf8TypedPath;
use typed_path::{Utf8PlatformPathBuf, Utf8TypedPath};

use super::{entry, path};
use crate::file::{self, audio};
Expand All @@ -15,19 +15,38 @@ pub struct Filesystem;

impl Filesystem {
#[cfg(windows)]
fn is_native(path: &Utf8TypedPath<'_>) -> bool {
fn is_native(path: Utf8TypedPath<'_>) -> bool {
path.is_windows()
}

#[cfg(unix)]
fn is_native(path: &Utf8TypedPath<'_>) -> bool {
fn is_native(path: Utf8TypedPath<'_>) -> bool {
path.is_unix()
}

pub fn to_platform(path: Utf8TypedPath<'_>) -> Result<Utf8PlatformPathBuf, Error> {
match path {
Utf8TypedPath::Unix(unix_path) => {
if cfg!(unix) {
Ok(unix_path.with_platform_encoding())
} else {
error::Kind::InvalidTypedPathPlatform(path.to_path_buf()).into()
}
}
Utf8TypedPath::Windows(windows_path) => {
if cfg!(windows) {
Ok(windows_path.with_platform_encoding())
} else {
error::Kind::InvalidTypedPathPlatform(path.to_path_buf()).into()
}
}
}
}
}

impl super::Trait for Filesystem {
async fn check_folder(&self, path: Utf8TypedPath<'_>) -> Result<(), Error> {
if !Self::is_native(&path) {
if !Self::is_native(path) {
error::Kind::InvalidTypedPathPlatform(path.to_path_buf()).into()
} else if !path.is_absolute() {
error::Kind::InvalidAbsolutePath(path.to_path_buf()).into()
Expand Down Expand Up @@ -81,12 +100,7 @@ impl super::Trait for Filesystem {
source: &binary::Source<file::Property<audio::Format>>,
offset: Option<u64>,
) -> Result<binary::Response, Error> {
let path = match source.path.to_path() {
Utf8TypedPath::Unix(path) => path.with_platform_encoding_checked(),
Utf8TypedPath::Windows(path) => path.with_platform_encoding_checked(),
}
.map_err(|_| error::Kind::InvalidTypedPathPlatform(source.path.clone()))?;

let path = Self::to_platform(source.path.to_path())?;
binary::Response::from_path_property(
path,
&source.property,
Expand Down
8 changes: 2 additions & 6 deletions nghe-backend/src/test/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod s3;

pub use common::{Impl, Trait};
use nghe_api::common::filesystem;
use typed_path::{Utf8PlatformPathBuf, Utf8TypedPath};
use typed_path::Utf8PlatformPathBuf;

use crate::filesystem::Filesystem;

Expand Down Expand Up @@ -35,10 +35,6 @@ impl Mock {
}

pub fn prefix(&self) -> Utf8PlatformPathBuf {
match self.local.prefix() {
Utf8TypedPath::Unix(path) => path.with_platform_encoding_checked(),
Utf8TypedPath::Windows(path) => path.with_platform_encoding_checked(),
}
.unwrap()
crate::filesystem::local::Filesystem::to_platform(self.local.prefix()).unwrap()
}
}

0 comments on commit 1d4f5bf

Please sign in to comment.