refactor: 将 filenamify 移动至本地,将正则表达式设置为 static (#156)
This commit is contained in:
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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 筛选视频,返回视频数量
|
||||
|
||||
61
crates/bili_sync/src/utils/filenamify.rs
Normal file
61
crates/bili_sync/src/utils/filenamify.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
macro_rules! regex {
|
||||
($re:literal $(,)?) => {{
|
||||
static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
|
||||
RE.get_or_init(|| regex::Regex::new($re).unwrap())
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn filenamify<S: AsRef<str>>(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"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod convert;
|
||||
pub mod filenamify;
|
||||
pub mod model;
|
||||
pub mod nfo;
|
||||
pub mod status;
|
||||
|
||||
@@ -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};
|
||||
|
||||
Reference in New Issue
Block a user