- tauri 设计完成

This commit is contained in:
Pine
2026-05-12 23:19:37 +08:00
parent c8e8dd12d2
commit 4ad266f6c4
25 changed files with 3495 additions and 3039 deletions
+29
View File
@@ -0,0 +1,29 @@
use crate::models::ServerEvent;
use once_cell::sync::Lazy;
use tokio::sync::broadcast;
/// SSE 事件管理器
pub struct EventManager {
tx: broadcast::Sender<String>,
}
impl EventManager {
pub fn new() -> Self {
let (tx, _) = broadcast::channel(64);
Self { tx }
}
/// 广播事件给所有 SSE 客户端
pub fn broadcast(&self, event: &ServerEvent) {
if let Ok(json) = serde_json::to_string(event) {
let _ = self.tx.send(json);
}
}
/// 获取广播接收器
pub fn subscribe(&self) -> broadcast::Receiver<String> {
self.tx.subscribe()
}
}
pub static EVENTS: Lazy<EventManager> = Lazy::new(EventManager::new);
+12 -6
View File
@@ -1,14 +1,20 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
mod events;
mod models;
mod server;
mod storage;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
// 在后台线程启动 HTTP 服务器
std::thread::spawn(|| {
let rt = tokio::runtime::Runtime::new().expect("无法创建 tokio runtime");
rt.block_on(async {
server::start().await;
});
});
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
+161
View File
@@ -0,0 +1,161 @@
use serde::{Deserialize, Serialize};
// ============ Settings ============
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub username: String,
pub password: String,
pub volume: u32,
pub auto_play: bool,
pub play_mode: String,
pub image_duration: u32,
}
impl Default for Settings {
fn default() -> Self {
Self {
username: "admin".into(),
password: "123456".into(),
volume: 80,
auto_play: true,
play_mode: "sequential".into(),
image_duration: 5,
}
}
}
// ============ Playlist Item ============
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaylistItem {
pub path: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
// ============ URL Media Item ============
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UrlMediaItem {
pub url: String,
pub name: String,
#[serde(rename = "type")]
pub media_type: String,
pub added_at: String,
}
// ============ Media File (response) ============
#[derive(Debug, Clone, Serialize)]
pub struct MediaFile {
pub name: String,
pub relative_path: String,
#[serde(rename = "type")]
pub file_type: String,
pub url: String,
pub source: String,
}
// ============ Playback State ============
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaybackState {
pub status: String,
pub index: usize,
pub name: String,
#[serde(rename = "type")]
pub media_type: String,
}
impl Default for PlaybackState {
fn default() -> Self {
Self {
status: "idle".into(),
index: 0,
name: String::new(),
media_type: String::new(),
}
}
}
// ============ Events ============
#[derive(Debug, Clone, Serialize)]
pub struct ServerEvent {
pub action: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<PlaybackState>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub play_mode: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub image_duration: Option<u32>,
}
// ============ Playlist Response ============
#[derive(Debug, Clone, Serialize)]
pub struct PlaylistResponse {
pub files: Vec<MediaFile>,
pub volume: u32,
pub play_mode: String,
pub image_duration: u32,
}
// ============ API Request Bodies ============
#[derive(Debug, Deserialize)]
pub struct LoginForm {
pub username: String,
pub password: String,
}
#[derive(Debug, Serialize)]
pub struct LoginResponse {
pub success: bool,
}
#[derive(Debug, Deserialize)]
pub struct SettingsUpdate {
pub volume: Option<u32>,
pub play_mode: Option<String>,
pub image_duration: Option<u32>,
}
#[derive(Debug, Deserialize)]
pub struct PathBody {
pub path: String,
}
#[derive(Debug, Deserialize)]
pub struct UrlBody {
pub url: String,
}
#[derive(Debug, Deserialize)]
pub struct ActionBody {
pub action: String,
}
#[derive(Debug, Deserialize)]
pub struct StateBody {
pub state: PlaybackState,
}
#[derive(Debug, Serialize)]
pub struct OkResponse {
pub ok: bool,
}
#[derive(Debug, Serialize)]
pub struct OkDuplicateResponse {
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub duplicate: Option<bool>,
}
#[derive(Debug, Serialize)]
pub struct FilesResponse {
pub files: Vec<MediaFile>,
}
#[derive(Debug, Serialize)]
pub struct StateResponse {
pub state: PlaybackState,
}
+415
View File
@@ -0,0 +1,415 @@
use crate::events::EVENTS;
use crate::models::*;
use crate::storage::{get_media_type, is_allowed_extension, STORAGE};
use axum::{
extract::{Form, Multipart, State},
http::StatusCode,
response::sse::{Event, KeepAlive, Sse},
response::Json,
routing::{get, post},
Router,
};
use chrono::Local;
use futures::stream::Stream;
use std::convert::Infallible;
use std::path::PathBuf;
use std::sync::Arc;
use std::fs;
use tokio_stream::wrappers::BroadcastStream;
use tokio_stream::StreamExt;
use tower_http::cors::CorsLayer;
use tower_http::services::{ServeDir, ServeFile};
// ===================== 媒体目录 =====================
fn media_dir() -> PathBuf {
let mut path = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
path.push("Downloads");
path.push("Media");
fs::create_dir_all(&path).ok();
path
}
fn media_dir_str() -> String {
media_dir().to_string_lossy().to_string()
}
#[derive(Clone)]
pub struct AppState {
pub playback: Arc<tokio::sync::Mutex<PlaybackState>>,
}
impl AppState {
pub fn new() -> Self {
Self {
playback: Arc::new(tokio::sync::Mutex::new(PlaybackState::default())),
}
}
}
// ===================== 路由构建 =====================
pub fn create_router(state: AppState) -> Router {
Router::new()
.route("/api/login", post(login_handler))
.route("/api/settings", get(get_settings).post(update_settings))
.route("/media", get(list_media))
.route("/upload", post(upload_handler))
.route("/api/delete", post(delete_handler))
.route("/api/media/add-url", post(add_url_handler))
.route("/api/playlist", get(get_playlist))
.route("/api/playlist/add", post(add_playlist))
.route("/api/playlist/remove", post(remove_playlist))
.route("/api/control", post(control_handler))
.route("/api/state", get(get_state).post(update_state))
.route("/api/events", get(sse_handler))
.nest_service("/file", ServeDir::new(media_dir_str()))
.fallback_service(
ServeDir::new("../dist")
.append_index_html_on_directories(true)
.fallback(ServeFile::new("../dist/index.html")),
)
.layer(CorsLayer::permissive())
.with_state(state)
}
pub async fn start() {
let state = AppState::new();
let app = create_router(state);
println!("🚀 大屏媒体轮播系统启动于 http://0.0.0.0:8000");
println!(" - 管理后台: http://localhost:8000/admin");
println!(" - 大屏展示: http://localhost:8000/screen");
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000")
.await
.expect("无法绑定 0.0.0.0:8000,请检查端口是否被占用");
axum::serve(listener, app)
.await
.expect("服务器启动失败");
}
// ===================== Handler: 登录 =====================
async fn login_handler(
Form(form): Form<LoginForm>,
) -> Json<LoginResponse> {
let s = STORAGE.get_settings();
Json(LoginResponse {
success: form.username == s.username && form.password == s.password,
})
}
// ===================== Handler: 设置 =====================
async fn get_settings() -> Json<serde_json::Value> {
let s = STORAGE.get_settings();
Json(serde_json::json!({
"volume": s.volume,
"play_mode": s.play_mode,
"image_duration": s.image_duration,
}))
}
async fn update_settings(
Json(body): Json<SettingsUpdate>,
) -> Json<serde_json::Value> {
STORAGE.update_settings(SettingsUpdate {
volume: body.volume,
play_mode: body.play_mode.clone(),
image_duration: body.image_duration,
});
EVENTS.broadcast(&ServerEvent {
action: "settings_changed".into(),
state: None,
volume: body.volume,
play_mode: body.play_mode,
image_duration: body.image_duration,
});
Json(serde_json::json!({"ok": true}))
}
// ===================== Handler: 媒体列表 =====================
async fn list_media() -> Json<FilesResponse> {
let mut files = Vec::new();
let dir = media_dir();
// 本地文件
if let Ok(entries) = fs::read_dir(&dir) {
for entry in entries.flatten() {
let path = entry.path();
if !path.is_file() {
continue;
}
let name = path.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
.to_string();
if !is_allowed_extension(&name) {
continue;
}
let rel = name.clone();
let t = get_media_type(&name).to_string();
files.push(MediaFile {
name,
relative_path: rel.clone(),
file_type: t,
url: format!("/file/{}", rel),
source: "local".into(),
});
}
}
// URL 媒体
for item in STORAGE.get_url_media() {
files.push(MediaFile {
name: item.name,
relative_path: item.url.clone(),
file_type: item.media_type,
url: item.url,
source: "url".into(),
});
}
Json(FilesResponse { files })
}
// ===================== Handler: 上传 =====================
async fn upload_handler(
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
while let Ok(Some(field)) = multipart.next_field().await {
let file_name = field.file_name().unwrap_or("file").to_string();
let ext = std::path::Path::new(&file_name)
.extension()
.and_then(|e| e.to_str())
.map(|e| format!(".{}", e.to_lowercase()))
.unwrap_or_default();
if !is_allowed_extension(&ext) {
return Err((StatusCode::BAD_REQUEST, "不支持的文件类型".into()));
}
let ts = Local::now().format("%Y%m%d%H%M%S");
let stem = std::path::Path::new(&file_name)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("file");
let new_name = format!("{}_{}{}", stem, ts, ext);
let save_path = media_dir().join(&new_name);
let data = field.bytes().await.map_err(|e| {
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
})?;
fs::write(&save_path, &data).map_err(|e| {
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
})?;
}
Ok(Json(serde_json::json!({"ok": true})))
}
// ===================== Handler: 删除 =====================
async fn delete_handler(
Json(body): Json<PathBody>,
) -> Json<OkResponse> {
let path = body.path;
if !path.starts_with("http://") && !path.starts_with("https://") {
let file_path = media_dir().join(&path);
if file_path.exists() && file_path.is_file() {
fs::remove_file(&file_path).ok();
}
}
STORAGE.delete_media(&path);
EVENTS.broadcast(&ServerEvent {
action: "playlist_changed".into(),
state: None,
volume: None,
play_mode: None,
image_duration: None,
});
Json(OkResponse { ok: true })
}
// ===================== Handler: 添加 URL =====================
async fn add_url_handler(
Json(body): Json<UrlBody>,
) -> Json<OkDuplicateResponse> {
let url = body.url.trim().to_string();
if url.is_empty() {
return Json(OkDuplicateResponse { ok: false, duplicate: None });
}
let name = url.rsplit('/').next().unwrap_or(&url)
.split('?').next().unwrap_or(&url)
.to_string();
let t = get_media_type(&url).to_string();
let is_new = STORAGE.add_url_media(&url, name, t);
Json(OkDuplicateResponse { ok: true, duplicate: Some(!is_new) })
}
// ===================== Handler: 播放列表 =====================
async fn get_playlist() -> Json<PlaylistResponse> {
let items = STORAGE.get_playlist();
let settings = STORAGE.get_settings();
let dir = media_dir();
let mut files = Vec::new();
for item in &items {
if item.source.as_deref() == Some("url") {
let ext = std::path::Path::new(&item.path)
.extension()
.and_then(|e| e.to_str())
.unwrap_or("");
let t = if ["mp4", "mkv", "avi"].contains(&ext) { "video" } else { "image" };
files.push(MediaFile {
name: item.name.clone().unwrap_or_else(|| item.path.clone()),
relative_path: item.path.clone(),
file_type: t.to_string(),
url: item.path.clone(),
source: "url".into(),
});
} else {
let file_path = dir.join(&item.path);
if file_path.exists() {
let ext = file_path.extension()
.and_then(|e| e.to_str())
.unwrap_or("");
let t = if ["mp4", "mkv", "avi"].contains(&ext) { "video" } else { "image" };
files.push(MediaFile {
name: file_path.file_name()
.and_then(|n| n.to_str())
.unwrap_or(&item.path)
.to_string(),
relative_path: item.path.clone(),
file_type: t.to_string(),
url: format!("/file/{}", item.path),
source: "local".into(),
});
}
}
}
Json(PlaylistResponse {
files,
volume: settings.volume,
play_mode: settings.play_mode,
image_duration: settings.image_duration,
})
}
async fn add_playlist(
Json(body): Json<PathBody>,
) -> Json<OkResponse> {
STORAGE.add_to_playlist(&body.path);
EVENTS.broadcast(&ServerEvent {
action: "playlist_changed".into(),
state: None,
volume: None,
play_mode: None,
image_duration: None,
});
Json(OkResponse { ok: true })
}
async fn remove_playlist(
Json(body): Json<PathBody>,
) -> Json<OkResponse> {
STORAGE.remove_from_playlist(&body.path);
EVENTS.broadcast(&ServerEvent {
action: "playlist_changed".into(),
state: None,
volume: None,
play_mode: None,
image_duration: None,
});
Json(OkResponse { ok: true })
}
// ===================== Handler: 播放控制 =====================
async fn control_handler(
State(state): State<AppState>,
Json(body): Json<ActionBody>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
if !["play", "pause", "next", "prev"].contains(&body.action.as_str()) {
return Err((StatusCode::BAD_REQUEST, "action 必须是 play/pause/next/prev".into()));
}
EVENTS.broadcast(&ServerEvent {
action: body.action.clone(),
state: None,
volume: None,
play_mode: None,
image_duration: None,
});
let mut playback = state.playback.lock().await;
playback.status = match body.action.as_str() {
"play" => "playing".into(),
"pause" => "paused".into(),
_ => playback.status.clone(),
};
let state_snapshot = playback.clone();
drop(playback);
EVENTS.broadcast(&ServerEvent {
action: "state_update".into(),
state: Some(state_snapshot),
volume: None,
play_mode: None,
image_duration: None,
});
Ok(Json(serde_json::json!({"ok": true, "action": body.action})))
}
// ===================== Handler: 状态 =====================
async fn get_state(
State(state): State<AppState>,
) -> Json<StateResponse> {
let s = state.playback.lock().await;
Json(StateResponse { state: s.clone() })
}
async fn update_state(
State(state): State<AppState>,
Json(body): Json<StateBody>,
) -> Json<OkResponse> {
{
let mut s = state.playback.lock().await;
if !body.state.status.is_empty() {
s.status = body.state.status.clone();
}
s.index = body.state.index;
if !body.state.name.is_empty() {
s.name = body.state.name.clone();
}
if !body.state.media_type.is_empty() {
s.media_type = body.state.media_type.clone();
}
}
let s2 = state.playback.lock().await;
EVENTS.broadcast(&ServerEvent {
action: "state_update".into(),
state: Some(s2.clone()),
volume: None,
play_mode: None,
image_duration: None,
});
Json(OkResponse { ok: true })
}
// ===================== Handler: SSE =====================
async fn sse_handler() -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
let rx = EVENTS.subscribe();
let stream = BroadcastStream::new(rx).map(|msg| match msg {
Ok(data) => Ok(Event::default().data(data)),
Err(_) => Ok(Event::default().data("")),
});
Sse::new(stream).keep_alive(
KeepAlive::new()
.interval(std::time::Duration::from_secs(15)),
)
}
+197
View File
@@ -0,0 +1,197 @@
use crate::models::*;
use once_cell::sync::Lazy;
use std::path::PathBuf;
use std::sync::Mutex;
/// 数据文件路径:系统数据目录 /dpm/data.json
fn db_path() -> PathBuf {
let mut path = dirs::data_dir().unwrap_or_else(|| PathBuf::from("."));
path.push("dpm");
std::fs::create_dir_all(&path).ok();
path.push("data.json");
path
}
/// 持久化数据结构
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct PersistentData {
settings: std::collections::HashMap<String, Settings>,
playlist: Vec<PlaylistItem>,
url_media: Vec<UrlMediaItem>,
}
impl Default for PersistentData {
fn default() -> Self {
let mut s = std::collections::HashMap::new();
s.insert("admin".into(), Settings::default());
Self {
settings: s,
playlist: Vec::new(),
url_media: Vec::new(),
}
}
}
/// 数据存储
pub struct Storage {
inner: Mutex<PersistentData>,
path: PathBuf,
}
impl Storage {
pub fn new() -> Self {
let path = db_path();
let data = if path.exists() {
std::fs::read_to_string(&path)
.ok()
.and_then(|c| serde_json::from_str(&c).ok())
.unwrap_or_default()
} else {
PersistentData::default()
};
let storage = Self {
inner: Mutex::new(data),
path,
};
storage.save();
storage
}
fn save(&self) {
if let Ok(data) = self.inner.lock() {
if let Ok(json) = serde_json::to_string_pretty(&*data) {
std::fs::write(&self.path, json).ok();
}
}
}
// === Settings ===
pub fn get_settings(&self) -> Settings {
self.inner
.lock()
.ok()
.and_then(|d| d.settings.get("admin").cloned())
.unwrap_or_default()
}
pub fn update_settings(&self, update: SettingsUpdate) {
if let Ok(mut data) = self.inner.lock() {
if let Some(s) = data.settings.get_mut("admin") {
if let Some(v) = update.volume {
s.volume = v;
}
if let Some(m) = update.play_mode {
s.play_mode = m;
}
if let Some(d) = update.image_duration {
s.image_duration = d;
}
}
}
self.save();
}
// === Playlist ===
pub fn get_playlist(&self) -> Vec<PlaylistItem> {
self.inner
.lock()
.ok()
.map(|d| d.playlist.clone())
.unwrap_or_default()
}
pub fn add_to_playlist(&self, path: &str) {
if let Ok(mut data) = self.inner.lock() {
if !data.playlist.iter().any(|p| p.path == path) {
let mut item = PlaylistItem {
path: path.into(),
source: None,
name: None,
};
if path.starts_with("http://") || path.starts_with("https://") {
item.source = Some("url".into());
if let Some(um) = data.url_media.iter().find(|u| u.url == path) {
item.name = Some(um.name.clone());
}
}
data.playlist.push(item);
}
}
self.save();
}
pub fn remove_from_playlist(&self, path: &str) {
if let Ok(mut data) = self.inner.lock() {
data.playlist.retain(|p| p.path != path);
}
self.save();
}
// === URL Media ===
pub fn add_url_media(&self, url: &str, name: String, media_type: String) -> bool {
let is_new = if let Ok(mut data) = self.inner.lock() {
if !data.url_media.iter().any(|u| u.url == url) {
data.url_media.push(UrlMediaItem {
url: url.into(),
name,
media_type,
added_at: chrono::Local::now()
.format("%Y-%m-%dT%H:%M:%S%.3f")
.to_string(),
});
true
} else {
false
}
} else {
false
};
if is_new {
self.save();
}
is_new
}
pub fn get_url_media(&self) -> Vec<UrlMediaItem> {
self.inner
.lock()
.ok()
.map(|d| d.url_media.clone())
.unwrap_or_default()
}
pub fn remove_url_media(&self, url: &str) {
if let Ok(mut data) = self.inner.lock() {
data.url_media.retain(|u| u.url != url);
}
self.save();
}
// === Delete ===
pub fn delete_media(&self, path: &str) {
if path.starts_with("http://") || path.starts_with("https://") {
self.remove_url_media(path);
}
self.remove_from_playlist(path);
}
}
// 全局共享实例
pub static STORAGE: Lazy<Storage> = Lazy::new(Storage::new);
/// 判断文件扩展名是否允许
pub fn is_allowed_extension(path: &str) -> bool {
let allowed = [".mp4", ".mkv", ".avi", ".jpg", ".jpeg", ".png"];
let lower = path.to_lowercase();
allowed.iter().any(|ext| lower.ends_with(ext))
}
/// 获取媒体类型
pub fn get_media_type(path: &str) -> &'static str {
let lower = path.to_lowercase();
if lower.ends_with(".mp4") || lower.ends_with(".mkv") || lower.ends_with(".avi") {
"video"
} else {
"image"
}
}