feat: 重构下载模块,将文件下载到临时目录再最终移动至目标路径 (#495)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2025-10-13 01:59:50 +08:00
committed by GitHub
parent eb2606f120
commit de702435af
6 changed files with 145 additions and 95 deletions

View File

@@ -32,6 +32,8 @@ pub async fn process_video_source(
bili_client: &BiliClient,
connection: &DatabaseConnection,
) -> Result<()> {
// 预创建视频源目录,提前检测目录是否可写
video_source.create_dir_all().await?;
// 从参数中获取视频列表的 Model 与视频流
let (video_source, video_streams) = video_source.refresh(bili_client, connection).await?;
// 从视频流中获取新视频的简要信息,写入数据库
@@ -572,32 +574,18 @@ pub async fn fetch_page_video(
.await?
.best_stream(&VersionedConfig::get().load().filter_option)?;
match streams {
BestStream::Mixed(mix_stream) => downloader.fetch_with_fallback(&mix_stream.urls(), page_path).await?,
BestStream::Mixed(mix_stream) => downloader.multi_fetch(&mix_stream.urls(), page_path).await?,
BestStream::VideoAudio {
video: video_stream,
audio: None,
} => downloader.fetch_with_fallback(&video_stream.urls(), page_path).await?,
} => downloader.multi_fetch(&video_stream.urls(), page_path).await?,
BestStream::VideoAudio {
video: video_stream,
audio: Some(audio_stream),
} => {
let (tmp_video_path, tmp_audio_path) = (
page_path.with_extension("tmp_video"),
page_path.with_extension("tmp_audio"),
);
let res = async {
downloader
.fetch_with_fallback(&video_stream.urls(), &tmp_video_path)
.await?;
downloader
.fetch_with_fallback(&audio_stream.urls(), &tmp_audio_path)
.await?;
downloader.merge(&tmp_video_path, &tmp_audio_path, page_path).await
}
.await;
let _ = fs::remove_file(tmp_video_path).await;
let _ = fs::remove_file(tmp_audio_path).await;
res?
downloader
.multi_fetch_and_merge(&video_stream.urls(), &audio_stream.urls(), page_path)
.await?
}
}
Ok(ExecutionStatus::Succeeded)