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,6 +1,8 @@
|
||||
use crate::domain::entities::file::File;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::display_helpers::{format_file_size, mime_to_category, mime_to_icon_class, mime_to_icon_special_class};
|
||||
|
||||
/// DTO for file responses
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileDto {
|
||||
@@ -27,19 +29,40 @@ pub struct FileDto {
|
||||
|
||||
/// Last modification timestamp
|
||||
pub modified_at: u64,
|
||||
|
||||
// ── Pre-computed display fields ──
|
||||
|
||||
/// FontAwesome icon CSS class (e.g. "fas fa-file-image")
|
||||
pub icon_class: String,
|
||||
|
||||
/// Extra CSS class for icon styling (e.g. "image-icon", "" when default)
|
||||
pub icon_special_class: String,
|
||||
|
||||
/// Human-readable file category (e.g. "Image", "Document")
|
||||
pub category: String,
|
||||
|
||||
/// Human-readable formatted size (e.g. "3.27 MB")
|
||||
pub size_formatted: String,
|
||||
}
|
||||
|
||||
impl From<File> for FileDto {
|
||||
fn from(file: File) -> Self {
|
||||
let mime = file.mime_type();
|
||||
let size = file.size();
|
||||
|
||||
Self {
|
||||
id: file.id().to_string(),
|
||||
name: file.name().to_string(),
|
||||
path: file.path_string().to_string(),
|
||||
size: file.size(),
|
||||
mime_type: file.mime_type().to_string(),
|
||||
size,
|
||||
mime_type: mime.to_string(),
|
||||
folder_id: file.folder_id().map(String::from),
|
||||
created_at: file.created_at(),
|
||||
modified_at: file.modified_at(),
|
||||
icon_class: mime_to_icon_class(mime).to_string(),
|
||||
icon_special_class: mime_to_icon_special_class(mime).to_string(),
|
||||
category: mime_to_category(mime).to_string(),
|
||||
size_formatted: format_file_size(size),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,9 +70,8 @@ impl From<File> for FileDto {
|
||||
// To convert from FileDto to File for batch handlers
|
||||
impl From<FileDto> for File {
|
||||
fn from(dto: FileDto) -> Self {
|
||||
// Use constructor to create an entity from DTO
|
||||
// Note: this should be simplified if File has a proper constructor
|
||||
// If not, make the conversion as best as possible
|
||||
// Display fields (icon_class, icon_special_class, category, size_formatted)
|
||||
// are not part of the domain entity and are ignored.
|
||||
File::from_dto(
|
||||
dto.id,
|
||||
dto.name,
|
||||
@@ -75,6 +97,10 @@ impl FileDto {
|
||||
folder_id: None,
|
||||
created_at: 0,
|
||||
modified_at: 0,
|
||||
icon_class: "fas fa-file".to_string(),
|
||||
icon_special_class: String::new(),
|
||||
category: "Document".to_string(),
|
||||
size_formatted: "0 Bytes".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user