diff --git a/Cargo.lock b/Cargo.lock index a31f5ba..9ef05b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -430,7 +430,6 @@ dependencies = [ "clap", "cookie", "dirs", - "filenamify", "float-ord", "futures", "handlebars", @@ -953,16 +952,6 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" -[[package]] -name = "filenamify" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9be5c5c7738f71d690d14e021cc0bd91888b07b015c5c95df22463e78ca09e" -dependencies = [ - "lazy_static", - "regex", -] - [[package]] name = "finl_unicode" version = "1.2.0" diff --git a/Cargo.toml b/Cargo.toml index 7366f13..6fd5bd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ chrono = { version = "0.4.38", features = ["serde"] } clap = { version = "4.5.9", features = ["env"] } cookie = "0.18.1" dirs = "5.0.1" -filenamify = "0.1.1" float-ord = "0.3.2" futures = "0.3.30" handlebars = "6.0.0" diff --git a/crates/bili_sync/Cargo.toml b/crates/bili_sync/Cargo.toml index e2e6835..fc63ee7 100644 --- a/crates/bili_sync/Cargo.toml +++ b/crates/bili_sync/Cargo.toml @@ -19,7 +19,6 @@ chrono = { workspace = true } clap = { workspace = true } cookie = { workspace = true } dirs = { workspace = true } -filenamify = { workspace = true } float-ord = { workspace = true } futures = { workspace = true } handlebars = { workspace = true } diff --git a/crates/bili_sync/src/adapter/helper/mod.rs b/crates/bili_sync/src/adapter/helper/mod.rs index 178710e..56de789 100644 --- a/crates/bili_sync/src/adapter/helper/mod.rs +++ b/crates/bili_sync/src/adapter/helper/mod.rs @@ -3,7 +3,6 @@ use std::path::Path; use anyhow::Result; use bili_sync_entity::*; -use filenamify::filenamify; use sea_orm::entity::prelude::*; use sea_orm::sea_query::{OnConflict, SimpleExpr}; use sea_orm::ActiveValue::Set; @@ -11,6 +10,7 @@ use sea_orm::{Condition, QuerySelect}; use crate::bilibili::{BiliError, PageInfo, VideoInfo}; use crate::config::TEMPLATE; +use crate::utils::filenamify::filenamify; use crate::utils::id_time_key; /// 使用 condition 筛选视频,返回视频数量 diff --git a/crates/bili_sync/src/utils/filenamify.rs b/crates/bili_sync/src/utils/filenamify.rs new file mode 100644 index 0000000..fab07ce --- /dev/null +++ b/crates/bili_sync/src/utils/filenamify.rs @@ -0,0 +1,61 @@ +macro_rules! regex { + ($re:literal $(,)?) => {{ + static RE: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); + RE.get_or_init(|| regex::Regex::new($re).unwrap()) + }}; +} + +pub fn filenamify>(input: S) -> String { + let reserved = regex!("[<>:\"/\\\\|?*\u{0000}-\u{001F}\u{007F}\u{0080}-\u{009F}]+"); + let windows_reserved = regex!("^(con|prn|aux|nul|com\\d|lpt\\d)$"); + let outer_periods = regex!("^\\.+|\\.+$"); + + let replacement = "_"; + + let input = reserved.replace_all(input.as_ref(), replacement); + let input = outer_periods.replace_all(input.as_ref(), replacement); + + let mut result = input.into_owned(); + if windows_reserved.is_match(result.as_str()) { + result.push_str(replacement); + } + + result +} + +#[cfg(test)] +mod tests { + use super::filenamify; + + #[test] + fn test_filenamify() { + assert_eq!(filenamify("foo/bar"), "foo_bar"); + assert_eq!(filenamify("foo//bar"), "foo_bar"); + assert_eq!(filenamify("//foo//bar//"), "_foo_bar_"); + assert_eq!(filenamify("foo\\bar"), "foo_bar"); + assert_eq!(filenamify("foo\\\\\\bar"), "foo_bar"); + assert_eq!(filenamify(r"foo\\bar"), "foo_bar"); + assert_eq!(filenamify(r"foo\\\\\\bar"), "foo_bar"); + assert_eq!(filenamify("////foo////bar////"), "_foo_bar_"); + assert_eq!(filenamify("foo\u{0000}bar"), "foo_bar"); + assert_eq!(filenamify("\"foo<>bar*"), "_foo_bar_"); + assert_eq!(filenamify("."), "_"); + assert_eq!(filenamify(".."), "_"); + assert_eq!(filenamify("./"), "__"); + assert_eq!(filenamify("../"), "__"); + assert_eq!(filenamify("../../foo/bar"), "__.._foo_bar"); + assert_eq!(filenamify("foo.bar."), "foo.bar_"); + assert_eq!(filenamify("foo.bar.."), "foo.bar_"); + assert_eq!(filenamify("foo.bar..."), "foo.bar_"); + assert_eq!(filenamify("con"), "con_"); + assert_eq!(filenamify("com1"), "com1_"); + assert_eq!(filenamify(":nul|"), "_nul_"); + assert_eq!(filenamify("foo/bar/nul"), "foo_bar_nul"); + assert_eq!(filenamify("file:///file.tar.gz"), "file_file.tar.gz"); + assert_eq!(filenamify("http://www.google.com"), "http_www.google.com"); + assert_eq!( + filenamify("https://www.youtube.com/watch?v=dQw4w9WgXcQ"), + "https_www.youtube.com_watch_v=dQw4w9WgXcQ" + ); + } +} diff --git a/crates/bili_sync/src/utils/mod.rs b/crates/bili_sync/src/utils/mod.rs index 81b5500..6599fb2 100644 --- a/crates/bili_sync/src/utils/mod.rs +++ b/crates/bili_sync/src/utils/mod.rs @@ -1,4 +1,5 @@ pub mod convert; +pub mod filenamify; pub mod model; pub mod nfo; pub mod status; diff --git a/crates/bili_sync/src/workflow.rs b/crates/bili_sync/src/workflow.rs index 6f876d6..c6f66dc 100644 --- a/crates/bili_sync/src/workflow.rs +++ b/crates/bili_sync/src/workflow.rs @@ -4,7 +4,6 @@ use std::pin::Pin; use anyhow::{bail, Result}; use bili_sync_entity::*; -use filenamify::filenamify; use futures::stream::{FuturesOrdered, FuturesUnordered}; use futures::{Future, Stream, StreamExt}; use sea_orm::entity::prelude::*; @@ -19,6 +18,7 @@ use crate::config::{ARGS, CONFIG, TEMPLATE}; use crate::downloader::Downloader; use crate::error::{DownloadAbortError, ProcessPageError}; use crate::utils::delay; +use crate::utils::filenamify::filenamify; use crate::utils::model::{create_videos, update_pages_model, update_videos_model}; use crate::utils::nfo::{ModelWrapper, NFOMode, NFOSerializer}; use crate::utils::status::{PageStatus, VideoStatus};