Add VirtualFs tag-mode WebDAV + MyFiles UI + Admin WebDAV endpoint
Test / test (push) Has been cancelled
Test / build (push) Has been cancelled

- VirtualFs: SQLite-backed virtual folders (tag mode), 16 unit tests
- MyFiles module: API endpoints + Web UI for folder/tag management
- Admin WebDAV: /admin-webdav/*path with Basic Auth + URI prefix rewrite
- CLI: webdav-folder/tag/untag/list/start --virtual-mode commands
- Deployed and tested on M5Max48: PROPFIND, PUT, GET, DELETE all working
This commit is contained in:
Warren
2026-06-22 10:38:25 +08:00
parent 37d0fe1a3c
commit 60e4329eed
7 changed files with 1596 additions and 39 deletions
+533
View File
@@ -0,0 +1,533 @@
use axum::{
extract::{Path, Query, State},
http::StatusCode,
response::{Html, Json},
};
use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use crate::server::AppState;
const SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS virtual_folders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
folder TEXT NOT NULL UNIQUE,
description TEXT DEFAULT '',
created_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS file_tags (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
tag TEXT NOT NULL,
UNIQUE(filename, tag)
);
CREATE INDEX IF NOT EXISTS idx_file_tags_tag ON file_tags(tag);
CREATE INDEX IF NOT EXISTS idx_file_tags_filename ON file_tags(filename);
";
fn user_db_path(state: &AppState, username: &str) -> PathBuf {
let root = std::env::var("MB_WEBDAV_PARENT")
.unwrap_or_else(|_| "/Users/accusys/momentry/var/sftpgo/data".to_string());
PathBuf::from(root)
.join(username)
.join("webdav_virtual.sqlite")
}
fn user_root(username: &str) -> PathBuf {
let root = std::env::var("MB_WEBDAV_PARENT")
.unwrap_or_else(|_| "/Users/accusys/momentry/var/sftpgo/data".to_string());
PathBuf::from(root).join(username)
}
fn ensure_schema(db_path: &PathBuf) -> anyhow::Result<Connection> {
let conn = Connection::open(db_path)
.map_err(|e| anyhow::anyhow!("Failed to open DB: {}", e))?;
conn.execute_batch(SCHEMA)
.map_err(|e| anyhow::anyhow!("Failed to create schema: {}", e))?;
Ok(conn)
}
#[derive(Serialize)]
pub struct FolderInfo {
pub name: String,
pub description: String,
pub file_count: usize,
}
#[derive(Serialize)]
pub struct FileInfo {
pub name: String,
pub size: u64,
pub tags: Vec<String>,
}
#[derive(Deserialize)]
pub struct FolderRequest {
pub name: String,
pub description: Option<String>,
}
#[derive(Deserialize)]
pub struct TagRequest {
pub file: String,
pub tag: String,
}
pub async fn list_folders(
State(state): State<AppState>,
Path(username): Path<String>,
) -> Result<Json<Vec<FolderInfo>>, (StatusCode, String)> {
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let mut stmt = conn
.prepare("SELECT folder, description FROM virtual_folders ORDER BY folder")
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let folders: Vec<(String, String)> = stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.filter_map(|r| r.ok())
.collect();
let mut result = Vec::new();
for (folder, desc) in folders {
let tag = folder.trim_start_matches('/').to_string();
let count: usize = conn
.query_row(
"SELECT COUNT(*) FROM file_tags WHERE tag = ?1",
params![tag],
|row| row.get(0),
)
.unwrap_or(0);
result.push(FolderInfo {
name: folder,
description: desc,
file_count: count,
});
}
Ok(Json(result))
}
pub async fn create_folder(
State(state): State<AppState>,
Path(username): Path<String>,
Json(req): Json<FolderRequest>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let folder = if req.name.starts_with('/') {
req.name
} else {
format!("/{}", req.name)
};
let desc = req.description.unwrap_or_default();
conn.execute(
"INSERT OR IGNORE INTO virtual_folders (folder, description) VALUES (?1, ?2)",
params![folder, desc],
)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(serde_json::json!({
"status": "ok",
"folder": folder,
"description": desc
})))
}
pub async fn delete_folder(
State(state): State<AppState>,
Path((username, folder_name)): Path<(String, String)>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let folder = if folder_name.starts_with('/') {
folder_name
} else {
format!("/{}", folder_name)
};
let tag = folder.trim_start_matches('/').to_string();
conn.execute("DELETE FROM file_tags WHERE tag = ?1", params![tag])
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
conn.execute("DELETE FROM virtual_folders WHERE folder = ?1", params![folder])
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(serde_json::json!({"status": "ok", "deleted": folder})))
}
pub async fn list_files(
State(state): State<AppState>,
Path(username): Path<String>,
Query(q): Query<serde_json::Map<String, serde_json::Value>>,
) -> Result<Json<Vec<FileInfo>>, (StatusCode, String)> {
let root = user_root(&username);
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let folder_filter = q.get("folder").and_then(|v| v.as_str()).map(|s| s.to_string());
let filenames: Vec<String> = if let Some(folder) = &folder_filter {
let tag = folder.trim_start_matches('/');
let rows: Vec<String> = conn
.prepare("SELECT filename FROM file_tags WHERE tag = ?1 ORDER BY filename")
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.query_map(params![tag], |row| row.get::<_, String>(0))
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.filter_map(|r| r.ok())
.collect();
rows
} else {
let mut entries = Vec::new();
if let Ok(rd) = std::fs::read_dir(&root) {
for entry in rd.flatten() {
if entry.path().is_file() {
if let Some(name) = entry.file_name().to_str() {
entries.push(name.to_string());
}
}
}
}
entries.sort();
entries
};
let mut result = Vec::new();
for fname in filenames {
let size = std::fs::metadata(root.join(&fname))
.map(|m| m.len())
.unwrap_or(0);
let mut tags_stmt = conn
.prepare("SELECT tag FROM file_tags WHERE filename = ?1 ORDER BY tag")
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let tags: Vec<String> = tags_stmt
.query_map(params![fname], |row| row.get::<_, String>(0))
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.filter_map(|r| r.ok())
.collect();
result.push(FileInfo {
name: fname,
size,
tags,
});
}
Ok(Json(result))
}
pub async fn add_tag(
State(state): State<AppState>,
Path(username): Path<String>,
Json(req): Json<TagRequest>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let folder = if req.tag.starts_with('/') {
req.tag.clone()
} else {
format!("/{}", req.tag)
};
let tag = folder.trim_start_matches('/').to_string();
conn.execute(
"INSERT OR IGNORE INTO virtual_folders (folder, description) VALUES (?1, '')",
params![folder],
)
.ok();
conn.execute(
"INSERT OR IGNORE INTO file_tags (filename, tag) VALUES (?1, ?2)",
params![req.file, tag],
)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(serde_json::json!({
"status": "ok",
"file": req.file,
"tag": tag
})))
}
pub async fn remove_tag(
State(state): State<AppState>,
Path(username): Path<String>,
Json(req): Json<TagRequest>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let tag = req.tag.trim_start_matches('/');
conn.execute(
"DELETE FROM file_tags WHERE filename = ?1 AND tag = ?2",
params![req.file, tag],
)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(serde_json::json!({
"status": "ok",
"file": req.file,
"removed_tag": tag
})))
}
pub async fn file_tags(
State(state): State<AppState>,
Path((username, filename)): Path<(String, String)>,
) -> Result<Json<Vec<String>>, (StatusCode, String)> {
let db_path = user_db_path(&state, &username);
let conn = ensure_schema(&db_path).map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let mut stmt = conn
.prepare("SELECT tag FROM file_tags WHERE filename = ?1 ORDER BY tag")
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let tags: Vec<String> = stmt
.query_map(params![filename], |row| row.get::<_, String>(0))
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.filter_map(|r| r.ok())
.collect();
Ok(Json(tags))
}
pub async fn ui_page() -> Html<String> {
Html(MYFILES_HTML.to_string())
}
const MYFILES_HTML: &str = r#"<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MyFiles — MarkBase</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #f5f5f7; color: #1d1d1f; }
.header { background: #fff; border-bottom: 1px solid #d2d2d7; padding: 12px 20px; display: flex; align-items: center; justify-content: space-between; }
.header h1 { font-size: 20px; font-weight: 600; }
.header .user-info { font-size: 14px; color: #6e6e73; }
.container { display: flex; max-width: 1200px; margin: 0 auto; padding: 20px; gap: 20px; }
.sidebar { width: 220px; flex-shrink: 0; }
.sidebar h2 { font-size: 14px; color: #6e6e73; text-transform: uppercase; margin-bottom: 10px; }
.folder-list { list-style: none; }
.folder-list li { padding: 8px 12px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 8px; font-size: 14px; }
.folder-list li:hover { background: #e8e8ed; }
.folder-list li.active { background: #0071e3; color: #fff; }
.folder-list .count { margin-left: auto; font-size: 12px; opacity: 0.6; }
.main { flex: 1; min-width: 0; }
.toolbar { display: flex; gap: 8px; margin-bottom: 16px; align-items: center; }
.toolbar input[type="text"] { flex: 1; padding: 8px 12px; border: 1px solid #d2d2d7; border-radius: 8px; font-size: 14px; }
.btn { padding: 8px 16px; border: none; border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 500; }
.btn-primary { background: #0071e3; color: #fff; }
.btn-primary:hover { background: #0058b0; }
.btn-secondary { background: #e8e8ed; color: #1d1d1f; }
.btn-secondary:hover { background: #d2d2d7; }
.btn-danger { background: #ff3b30; color: #fff; }
.btn-danger:hover { background: #cc2918; }
.file-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.file-card { background: #fff; border-radius: 12px; padding: 12px; border: 1px solid #e8e8ed; }
.file-card .name { font-size: 14px; font-weight: 500; word-break: break-all; margin-bottom: 4px; }
.file-card .size { font-size: 12px; color: #6e6e73; margin-bottom: 8px; }
.file-card .tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: #e8e8ed; }
.tag.blue { background: #d1e8ff; color: #0058b0; }
.empty { text-align: center; padding: 60px; color: #6e6e73; }
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 100; align-items: center; justify-content: center; }
.modal-overlay.show { display: flex; }
.modal { background: #fff; border-radius: 16px; padding: 24px; min-width: 320px; max-width: 400px; }
.modal h3 { font-size: 18px; margin-bottom: 16px; }
.modal label { font-size: 14px; color: #6e6e73; display: block; margin-bottom: 4px; }
.modal input { width: 100%; padding: 8px 12px; border: 1px solid #d2d2d7; border-radius: 8px; font-size: 14px; margin-bottom: 12px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
</style>
</head>
<body>
<div class="header">
<h1>📁 MyFiles</h1>
<div class="user-info" id="user-info">Loading...</div>
</div>
<div class="container">
<div class="sidebar">
<h2>Folders</h2>
<ul class="folder-list" id="folder-list">
<li class="active" data-folder="">All Files</li>
</ul>
<div style="margin-top:16px">
<button class="btn btn-secondary" onclick="showNewFolderModal()" style="width:100%">+ New Folder</button>
</div>
</div>
<div class="main">
<div class="toolbar">
<input type="text" id="search" placeholder="Search files..." oninput="loadFiles()">
<button class="btn btn-primary" onclick="showUploadModal()">Upload</button>
</div>
<div class="file-grid" id="file-grid"></div>
<div class="empty" id="empty-state" style="display:none">No files found</div>
</div>
</div>
<div class="modal-overlay" id="folder-modal">
<div class="modal">
<h3>New Folder</h3>
<label>Folder Name</label>
<input type="text" id="folder-name" placeholder="e.g. photos">
<label>Description</label>
<input type="text" id="folder-desc" placeholder="Optional description">
<div class="actions">
<button class="btn btn-secondary" onclick="hideFolderModal()">Cancel</button>
<button class="btn btn-primary" onclick="createFolder()">Create</button>
</div>
</div>
</div>
<div class="modal-overlay" id="tag-modal">
<div class="modal">
<h3>Tag File</h3>
<p style="margin-bottom:12px;font-size:14px" id="tag-filename"></p>
<label>Tag / Folder</label>
<input type="text" id="tag-name" placeholder="e.g. photos">
<div class="actions">
<button class="btn btn-secondary" onclick="hideTagModal()">Cancel</button>
<button class="btn btn-primary" onclick="addTag()">Tag</button>
</div>
</div>
</div>
<script>
const API = '/api/v2/myfiles';
let username = 'demo';
let currentFolder = '';
let allFiles = [];
async function init() {
try {
const res = await fetch('/api/v2/auth/verify');
const data = await res.json();
if (data.user) username = data.user;
} catch(e) {}
document.getElementById('user-info').textContent = 'User: ' + username;
loadFolders();
loadFiles();
}
async function loadFolders() {
try {
const res = await fetch(`${API}/${username}/folders`);
const folders = await res.json();
const list = document.getElementById('folder-list');
list.innerHTML = '<li class="active" data-folder="" onclick="selectFolder(\'\')">All Files</li>';
for (const f of folders) {
const li = document.createElement('li');
li.dataset.folder = f.name;
li.onclick = () => selectFolder(f.name);
li.innerHTML = `📁 ${f.name} <span class="count">${f.file_count}</span>`;
list.appendChild(li);
}
} catch(e) { console.error(e); }
}
async function loadFiles() {
const search = document.getElementById('search').value;
let url = `${API}/${username}/files`;
if (currentFolder) url += `?folder=${encodeURIComponent(currentFolder)}`;
try {
const res = await fetch(url);
allFiles = await res.json();
const filtered = search ? allFiles.filter(f => f.name.toLowerCase().includes(search.toLowerCase())) : allFiles;
renderFiles(filtered);
} catch(e) { console.error(e); }
}
function renderFiles(files) {
const grid = document.getElementById('file-grid');
const empty = document.getElementById('empty-state');
grid.innerHTML = '';
if (files.length === 0) { empty.style.display = 'block'; return; }
empty.style.display = 'none';
for (const f of files) {
const card = document.createElement('div');
card.className = 'file-card';
let tagHtml = '';
for (const t of f.tags) {
tagHtml += `<span class="tag blue" onclick="event.stopPropagation();removeTag('${f.name}','${t}')" style="cursor:pointer">${t} ×</span>`;
}
tagHtml += `<span class="tag" onclick="showTagModal('${f.name}')" style="cursor:pointer">+ tag</span>`;
card.innerHTML = `
<div class="name">${f.name}</div>
<div class="size">${formatSize(f.size)}</div>
<div class="tags">${tagHtml}</div>
`;
grid.appendChild(card);
}
}
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1048576) return (bytes/1024).toFixed(1) + ' KB';
if (bytes < 1073741824) return (bytes/1048576).toFixed(1) + ' MB';
return (bytes/1073741824).toFixed(1) + ' GB';
}
function selectFolder(folder) {
currentFolder = folder;
document.querySelectorAll('.folder-list li').forEach(li => li.classList.remove('active'));
const target = document.querySelector(`[data-folder="${folder}"]`);
if (target) target.classList.add('active');
loadFiles();
}
function showNewFolderModal() { document.getElementById('folder-modal').classList.add('show'); }
function hideFolderModal() { document.getElementById('folder-modal').classList.remove('show'); }
async function createFolder() {
const name = document.getElementById('folder-name').value.trim();
const desc = document.getElementById('folder-desc').value.trim();
if (!name) return;
await fetch(`${API}/${username}/folders`, {
method: 'POST', headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name, description: desc})
});
document.getElementById('folder-name').value = '';
document.getElementById('folder-desc').value = '';
hideFolderModal();
loadFolders();
}
function showTagModal(filename) {
document.getElementById('tag-filename').textContent = filename;
document.getElementById('tag-name').value = '';
document.getElementById('tag-modal').classList.add('show');
}
function hideTagModal() { document.getElementById('tag-modal').classList.remove('show'); }
async function addTag() {
const filename = document.getElementById('tag-filename').textContent;
const tag = document.getElementById('tag-name').value.trim();
if (!tag) return;
await fetch(`${API}/${username}/tags`, {
method: 'POST', headers: {'Content-Type': 'application/json'},
body: JSON.stringify({file: filename, tag})
});
hideTagModal();
loadFolders();
loadFiles();
}
async function removeTag(file, tag) {
await fetch(`${API}/${username}/tags`, {
method: 'DELETE', headers: {'Content-Type': 'application/json'},
body: JSON.stringify({file, tag})
});
loadFolders();
loadFiles();
}
function showUploadModal() { alert('Upload via WebDAV at http://webdav.momentry.ddns.net (user: ' + username + ')'); }
init();
</script>
</body>
</html>"#;