1use dioxus::prelude::*;
12
13use super::types::RebuildAssetsResponse;
14
15#[cfg(feature = "server")]
17const IMAGE_EXTS: &[&str] = &["jpg", "jpeg", "png", "gif", "webp"];
18
19#[cfg(feature = "server")]
20struct ScannedFile {
22 rel_path: String,
24 filename: String,
25 mime: &'static str,
26 size_bytes: i64,
27 width: i32,
28 height: i32,
29}
30
31#[cfg(feature = "server")]
32fn walk_images(
36 dir: &std::path::Path,
37 base: &std::path::Path,
38 out: &mut Vec<ScannedFile>,
39 unreadable: &mut Vec<String>,
40) {
41 let Ok(entries) = std::fs::read_dir(dir) else {
42 return;
43 };
44 for entry in entries.flatten() {
45 let name = entry.file_name();
46 let Some(name_str) = name.to_str() else {
47 continue;
48 };
49 if name_str.starts_with('.') {
50 continue;
51 }
52 let path = entry.path();
53 if path.is_dir() {
54 walk_images(&path, base, out, unreadable);
55 continue;
56 }
57 let ext = name_str.rsplit('.').next().unwrap_or("");
58 if !IMAGE_EXTS.iter().any(|e| ext.eq_ignore_ascii_case(e)) {
59 continue;
60 }
61 let Ok(rel) = path.strip_prefix(base) else {
62 continue;
63 };
64 let rel_path = rel.to_string_lossy().replace('\\', "/");
65 let Some((w, h)) = crate::api::image::get_image_dimensions(&rel_path) else {
68 tracing::warn!("Rebuild: skip unreadable image {}", rel_path);
69 unreadable.push(rel_path);
70 continue;
71 };
72 let size_bytes = entry.metadata().map(|m| m.len() as i64).unwrap_or(0);
73 let mime = match ext.to_ascii_lowercase().as_str() {
74 "jpg" | "jpeg" => "image/jpeg",
75 "png" => "image/png",
76 "gif" => "image/gif",
77 _ => "image/webp",
78 };
79 out.push(ScannedFile {
80 rel_path,
81 filename: name_str.to_string(),
82 mime,
83 size_bytes,
84 width: w as i32,
85 height: h as i32,
86 });
87 }
88}
89
90#[server(RebuildAssetsIndex, "/api")]
92pub async fn rebuild_assets_index() -> Result<RebuildAssetsResponse, ServerFnError> {
93 #[cfg(feature = "server")]
94 {
95 use crate::api::auth::get_current_admin_user;
96 use crate::api::error::AppError;
97 use crate::db::pool::get_conn;
98
99 let _admin = get_current_admin_user().await?;
100
101 let (scanned, unreadable) = tokio::task::spawn_blocking(|| {
103 let base = std::path::Path::new("uploads");
104 let mut files = Vec::new();
105 let mut unreadable = Vec::new();
106 walk_images(base, base, &mut files, &mut unreadable);
107 (files, unreadable)
108 })
109 .await
110 .map_err(|_| AppError::Internal("素材扫描任务失败"))?;
111
112 let mut client = get_conn().await.map_err(AppError::db_conn)?;
113 let tx = client.transaction().await.map_err(AppError::tx)?;
114
115 let mut inserted: i64 = 0;
120 let mut updated: i64 = 0;
121 for f in &scanned {
122 let asset_id = uuid::Uuid::new_v4();
124 let row = tx
125 .query_opt(
126 "INSERT INTO assets (id, path, filename, mime, size_bytes, width, height) \
127 VALUES ($1, $2, $3, $4, $5, $6, $7) \
128 ON CONFLICT (path) DO UPDATE SET \
129 mime = EXCLUDED.mime, \
130 size_bytes = EXCLUDED.size_bytes, \
131 width = EXCLUDED.width, \
132 height = EXCLUDED.height, \
133 updated_at = NOW() \
134 WHERE assets.size_bytes IS DISTINCT FROM EXCLUDED.size_bytes \
135 OR assets.width IS DISTINCT FROM EXCLUDED.width \
136 OR assets.height IS DISTINCT FROM EXCLUDED.height \
137 OR assets.mime IS DISTINCT FROM EXCLUDED.mime \
138 RETURNING (xmax = 0) AS was_inserted",
139 &[
140 &asset_id,
141 &f.rel_path,
142 &f.filename,
143 &f.mime,
144 &f.size_bytes,
145 &f.width,
146 &f.height,
147 ],
148 )
149 .await
150 .map_err(AppError::tx)?;
151 match row {
152 Some(r) if r.get::<_, bool>("was_inserted") => inserted += 1,
153 Some(_) => updated += 1,
154 None => {} }
156 }
157
158 let mut keep_paths: Vec<String> = scanned.iter().map(|f| f.rel_path.clone()).collect();
162 keep_paths.extend(unreadable.iter().cloned());
163 let removed = tx
164 .execute(
165 "DELETE FROM assets WHERE NOT (path = ANY($1))",
166 &[&keep_paths],
167 )
168 .await
169 .map_err(AppError::tx)?;
170
171 let post_rows = tx
173 .query("SELECT id, content_html, cover_image FROM posts", &[])
174 .await
175 .map_err(AppError::query)?;
176 tx.execute("DELETE FROM asset_refs", &[])
177 .await
178 .map_err(AppError::tx)?;
179 let mut ref_count: i64 = 0;
180 for pr in &post_rows {
181 let post_id: i32 = pr.get("id");
182 let content_html: Option<String> = pr.get("content_html");
183 let cover_image: Option<String> = pr.get("cover_image");
184 let found = crate::api::posts::helpers::extract_asset_paths(
185 content_html.as_deref().unwrap_or(""),
186 cover_image.as_deref(),
187 );
188 if found.is_empty() {
189 continue;
190 }
191 let n = tx
192 .execute(
193 "INSERT INTO asset_refs (asset_id, post_id) \
194 SELECT id, $1 FROM assets WHERE path = ANY($2) \
195 ON CONFLICT DO NOTHING",
196 &[&post_id, &found],
197 )
198 .await
199 .map_err(AppError::tx)?;
200 ref_count += n as i64;
201 }
202
203 tx.commit().await.map_err(AppError::tx)?;
204
205 let scanned_count = scanned.len() as i64;
206 let skipped_count = unreadable.len() as i64;
207 let message = if skipped_count > 0 {
208 format!(
209 "重建完成:扫描 {} 个文件,新增 {},更新 {},移除 {},跳过 {} 个无法读取的文件(已保留)",
210 scanned_count, inserted, updated, removed, skipped_count
211 )
212 } else {
213 format!(
214 "重建完成:扫描 {} 个文件,新增 {},更新 {},移除 {}",
215 scanned_count, inserted, updated, removed
216 )
217 };
218 Ok(RebuildAssetsResponse {
219 success: true,
220 message,
221 scanned: scanned_count,
222 inserted,
223 updated,
224 removed: removed as i64,
225 ref_count,
226 skipped: skipped_count,
227 })
228 }
229 #[cfg(not(feature = "server"))]
230 unreachable!()
231}