fix: 添加 file_size 和 modified_time 到 videos 表和 API
问题: - /library 页面已注册文件卡片不显示 size 和 mdate - 数据库 videos 表缺少 file_size 和 modified_time 列 - API 查询未返回这些字段 修复: - 添加数据库迁移 (migrations/20261012_add_file_size_columns.sql) - 修改 register_single_file() 获取并保存 file_size 和 modified_time - 修改 list_files() API 查询包含新字段 - 创建脚本更新历史数据 (scripts/update_file_metadata.py) 影响: - 新注册文件自动填充 size 和 mtime - API 正确返回 file_size 和 modified_time - 历史数据已批量更新 (188 个文件) 测试: - API 测试通过 - 数据库验证通过 - 文件大小匹配验证通过
This commit is contained in:
+22
-1
@@ -494,15 +494,36 @@ pub(crate) async fn register_single_file(
|
||||
}
|
||||
}
|
||||
|
||||
// Get file size and modification time
|
||||
let file_size = std::fs::metadata(&path)
|
||||
.ok()
|
||||
.map(|m| m.len() as i64);
|
||||
|
||||
let modified_time = std::fs::metadata(&path)
|
||||
.ok()
|
||||
.and_then(|m| m.modified().ok())
|
||||
.and_then(|t| {
|
||||
chrono::DateTime::from_timestamp(
|
||||
t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() as i64,
|
||||
0
|
||||
)
|
||||
});
|
||||
|
||||
tracing::info!(
|
||||
"[REGISTER] File metadata: size={:?}, modified={:?}",
|
||||
file_size, modified_time
|
||||
);
|
||||
|
||||
let status = "registered";
|
||||
let insert_result = match sqlx::query(&format!(
|
||||
"INSERT INTO {} (file_uuid, file_path, file_name, file_type, duration, width, height, fps, total_frames, probe_json, status, content_hash, registration_time) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, NOW()) ON CONFLICT (file_uuid) DO UPDATE SET file_path = EXCLUDED.file_path, file_name = EXCLUDED.file_name, status = EXCLUDED.status, content_hash = EXCLUDED.content_hash, duration = EXCLUDED.duration, width = EXCLUDED.width, height = EXCLUDED.height, fps = EXCLUDED.fps, total_frames = EXCLUDED.total_frames, probe_json = EXCLUDED.probe_json",
|
||||
"INSERT INTO {} (file_uuid, file_path, file_name, file_type, duration, width, height, fps, total_frames, probe_json, status, content_hash, registration_time, file_size, modified_time) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, NOW(), $13, $14) ON CONFLICT (file_uuid) DO UPDATE SET file_path = EXCLUDED.file_path, file_name = EXCLUDED.file_name, status = EXCLUDED.status, content_hash = EXCLUDED.content_hash, duration = EXCLUDED.duration, width = EXCLUDED.width, height = EXCLUDED.height, fps = EXCLUDED.fps, total_frames = EXCLUDED.total_frames, probe_json = EXCLUDED.probe_json, file_size = EXCLUDED.file_size, modified_time = EXCLUDED.modified_time",
|
||||
videos_table
|
||||
))
|
||||
.bind(&file_uuid).bind(&canonical_path).bind(&final_name).bind(&final_file_type)
|
||||
.bind(duration).bind(width as i32).bind(height as i32).bind(fps)
|
||||
.bind(total_frames as i64)
|
||||
.bind(&probe_json).bind(status).bind(&content_hash_val)
|
||||
.bind(&file_size).bind(&modified_time)
|
||||
.execute(db.pool()).await
|
||||
{
|
||||
Ok(result) => result,
|
||||
|
||||
+10
-8
@@ -166,8 +166,8 @@ async fn list_files(
|
||||
|
||||
// Get registered files from database
|
||||
let table = crate::core::db::schema::table_name("videos");
|
||||
let registered: Vec<(String, String, String, String)> = sqlx::query_as(&format!(
|
||||
"SELECT file_uuid, file_name, file_path, status FROM {} ORDER BY id",
|
||||
let registered: Vec<(String, String, String, String, Option<i64>, Option<String>)> = sqlx::query_as(&format!(
|
||||
"SELECT file_uuid, file_name, file_path, status, file_size, modified_time::text FROM {} ORDER BY id",
|
||||
table
|
||||
))
|
||||
.fetch_all(state.db.pool())
|
||||
@@ -175,23 +175,25 @@ async fn list_files(
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
// Build registered map
|
||||
let registered_map: std::collections::HashMap<String, (String, String, String)> = registered
|
||||
let registered_map: std::collections::HashMap<String, (String, String, String, Option<i64>, Option<String>)> = registered
|
||||
.into_iter()
|
||||
.map(|(uuid, name, path, status)| (path, (uuid, name, status)))
|
||||
.map(|(uuid, name, path, status, size, mtime)| (path, (uuid, name, status, size, mtime)))
|
||||
.collect();
|
||||
|
||||
// Merge: update registration status for matching files
|
||||
for file in &mut all_files {
|
||||
if let Some((uuid, name, status)) = registered_map.get(&file.file_path) {
|
||||
if let Some((uuid, name, status, size, mtime)) = registered_map.get(&file.file_path) {
|
||||
file.file_uuid = uuid.clone();
|
||||
file.status = status.clone();
|
||||
file.is_registered = status != "pending";
|
||||
file.file_size = *size;
|
||||
file.modified_time = mtime.clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Add files that are in database but not in MarkBase (registered but moved/deleted)
|
||||
let all_paths: std::collections::HashSet<String> = all_files.iter().map(|f| f.file_path.clone()).collect();
|
||||
for (path, (uuid, name, status)) in ®istered_map {
|
||||
for (path, (uuid, name, status, size, mtime)) in ®istered_map {
|
||||
if !all_paths.contains(path) {
|
||||
all_files.push(FileItem {
|
||||
file_uuid: uuid.clone(),
|
||||
@@ -200,8 +202,8 @@ async fn list_files(
|
||||
status: status.clone(),
|
||||
is_registered: status != "pending",
|
||||
total_chunks: 0,
|
||||
file_size: None,
|
||||
modified_time: None,
|
||||
file_size: *size,
|
||||
modified_time: mtime.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user