feat(P1+P2): server-authoritative favorites/recent + pre-computed display fields
P1-A: Enrich FavoriteItemDto & RecentItemDto with item_name, item_size,
item_mime_type, parent_id, modified_at via SQL LEFT JOINs — eliminates
N+1 per-item fetches.
P1-B: Rewrite favorites.js as server-authoritative (724→380 lines).
In-memory cache backed by GET /api/favorites; no localStorage.
P1-C: Rewrite recent.js as server-authoritative (341→249 lines).
GET /api/recent + POST /api/recent/{type}/{id}; no localStorage.
P1 cleanup: Remove dead localStorage cleanup from auth.js logout,
fix async clearRecentFiles in app.js.
P2: Add icon_class, icon_special_class, category, size_formatted to
FileDto, FolderDto, FavoriteItemDto, RecentItemDto. New shared
display_helpers.rs module centralises mime→icon/category/size logic.
Frontend (ui.js, fileRenderer.js, favorites.js, recent.js) now reads
pre-computed fields from the API with fallback defaults — eliminates
5 duplicated mime→icon mapping blocks (~89 lines removed).
Also fixes: synthetic FolderDto in webdav_handler.rs, pre-existing
missing item_name field in share_service.rs test.
Net: -307 lines across 15 files. cargo check: 0 errors, 0 warnings.
cargo test display_helpers: 3/3 pass.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// DTO for recent items
|
||||
use super::display_helpers::{format_file_size, mime_to_category, mime_to_icon_class, mime_to_icon_special_class};
|
||||
|
||||
/// DTO for recent items, enriched with item metadata via SQL JOIN
|
||||
/// so the frontend does not need N+1 requests to resolve names/sizes.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RecentItemDto {
|
||||
/// Unique identifier for the recent item
|
||||
@@ -18,4 +21,56 @@ pub struct RecentItemDto {
|
||||
|
||||
/// When the item was accessed
|
||||
pub accessed_at: DateTime<Utc>,
|
||||
|
||||
// ── Enriched metadata (resolved via JOIN) ──
|
||||
|
||||
/// Display name of the file or folder
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub item_name: Option<String>,
|
||||
|
||||
/// Size in bytes (files only; folders → None)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub item_size: Option<i64>,
|
||||
|
||||
/// MIME type (files only)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub item_mime_type: Option<String>,
|
||||
|
||||
/// Parent folder ID (folder_id for files, parent_id for folders)
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub parent_id: Option<String>,
|
||||
|
||||
// ── Pre-computed display fields ──
|
||||
|
||||
/// FontAwesome icon CSS class (e.g. "fas fa-file-image", "fas fa-folder")
|
||||
pub icon_class: String,
|
||||
|
||||
/// Extra CSS class for icon styling (e.g. "image-icon", "folder-icon")
|
||||
pub icon_special_class: String,
|
||||
|
||||
/// Human-readable category (e.g. "Image", "Folder")
|
||||
pub category: String,
|
||||
|
||||
/// Formatted file size (e.g. "3.27 MB"); "--" for folders
|
||||
pub size_formatted: String,
|
||||
}
|
||||
|
||||
impl RecentItemDto {
|
||||
/// Populate display fields from the enriched metadata.
|
||||
/// Call this after constructing from the SQL row.
|
||||
pub fn with_display_fields(mut self) -> Self {
|
||||
if self.item_type == "folder" {
|
||||
self.icon_class = "fas fa-folder".to_string();
|
||||
self.icon_special_class = "folder-icon".to_string();
|
||||
self.category = "Folder".to_string();
|
||||
self.size_formatted = "--".to_string();
|
||||
} else {
|
||||
let mime = self.item_mime_type.as_deref().unwrap_or("application/octet-stream");
|
||||
self.icon_class = mime_to_icon_class(mime).to_string();
|
||||
self.icon_special_class = mime_to_icon_special_class(mime).to_string();
|
||||
self.category = mime_to_category(mime).to_string();
|
||||
self.size_formatted = format_file_size(self.item_size.unwrap_or(0) as u64);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user