|
| 1 | +use crate::{Result, media::MediaType}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::{fs, path::PathBuf, time::Duration, time::SystemTime}; |
| 4 | + |
| 5 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 6 | +pub struct PlaylistItem { |
| 7 | + pub file_path: PathBuf, |
| 8 | + pub name: String, |
| 9 | + pub media_type: MediaType, |
| 10 | + pub duration: Option<Duration>, |
| 11 | + pub file_size: u64, |
| 12 | +} |
| 13 | + |
| 14 | +impl PlaylistItem { |
| 15 | + pub fn new(file_path: PathBuf, name: String, media_type: MediaType) -> Self { |
| 16 | + // Convert to absolute path |
| 17 | + let absolute_path = file_path.canonicalize().unwrap_or_else(|_| { |
| 18 | + // If canonicalize fails (file doesn't exist), use absolute path |
| 19 | + std::path::absolute(&file_path).unwrap_or(file_path.clone()) |
| 20 | + }); |
| 21 | + |
| 22 | + let file_size = fs::metadata(&absolute_path).map(|m| m.len()).unwrap_or(0); |
| 23 | + |
| 24 | + Self { |
| 25 | + file_path: absolute_path, |
| 26 | + name, |
| 27 | + media_type, |
| 28 | + duration: None, |
| 29 | + file_size, |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + pub fn with_duration(mut self, duration: Duration) -> Self { |
| 34 | + self.duration = Some(duration); |
| 35 | + self |
| 36 | + } |
| 37 | + |
| 38 | + pub fn format_duration(&self) -> String { |
| 39 | + if let Some(duration) = self.duration { |
| 40 | + let secs = duration.as_secs(); |
| 41 | + let minutes = secs / 60; |
| 42 | + let seconds = secs % 60; |
| 43 | + format!("{:02}:{:02}", minutes, seconds) |
| 44 | + } else { |
| 45 | + "--:--".to_string() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn format_file_size(&self) -> String { |
| 50 | + cutil::str::pretty_size_string(self.file_size) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 55 | +pub struct Playlist { |
| 56 | + pub name: String, |
| 57 | + pub description: Option<String>, |
| 58 | + |
| 59 | + #[serde(default)] |
| 60 | + pub items: Vec<PlaylistItem>, |
| 61 | + |
| 62 | + #[serde(default = "SystemTime::now")] |
| 63 | + pub created_at: SystemTime, |
| 64 | + |
| 65 | + #[serde(default = "SystemTime::now")] |
| 66 | + pub modified_at: SystemTime, |
| 67 | + |
| 68 | + pub thumbnail_path: Option<PathBuf>, |
| 69 | +} |
| 70 | + |
| 71 | +impl Playlist { |
| 72 | + pub fn new(name: String) -> Self { |
| 73 | + let now = SystemTime::now(); |
| 74 | + Self { |
| 75 | + name, |
| 76 | + description: None, |
| 77 | + items: Vec::new(), |
| 78 | + created_at: now, |
| 79 | + modified_at: now, |
| 80 | + thumbnail_path: None, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + pub fn with_description(mut self, description: String) -> Self { |
| 85 | + self.description = Some(description); |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + pub fn add_item(&mut self, item: PlaylistItem) { |
| 90 | + self.items.push(item); |
| 91 | + self.modified_at = SystemTime::now(); |
| 92 | + } |
| 93 | + |
| 94 | + pub fn remove_item(&mut self, index: usize) -> Result<PlaylistItem> { |
| 95 | + if index >= self.items.len() { |
| 96 | + return Err(crate::Error::IndexOutOfBounds(index, self.items.len())); |
| 97 | + } |
| 98 | + |
| 99 | + self.modified_at = SystemTime::now(); |
| 100 | + let item = self.items.remove(index); |
| 101 | + Ok(item) |
| 102 | + } |
| 103 | + |
| 104 | + pub fn get_item(&self, index: usize) -> Option<&PlaylistItem> { |
| 105 | + self.items.get(index) |
| 106 | + } |
| 107 | + |
| 108 | + pub fn get_item_mut(&mut self, index: usize) -> Option<&mut PlaylistItem> { |
| 109 | + self.modified_at = SystemTime::now(); |
| 110 | + self.items.get_mut(index) |
| 111 | + } |
| 112 | + |
| 113 | + pub fn update_item(&mut self, index: usize, item: PlaylistItem) -> Result<()> { |
| 114 | + if index >= self.items.len() { |
| 115 | + return Err(crate::Error::IndexOutOfBounds(index, self.items.len())); |
| 116 | + } |
| 117 | + |
| 118 | + self.items[index] = item; |
| 119 | + self.modified_at = SystemTime::now(); |
| 120 | + |
| 121 | + Ok(()) |
| 122 | + } |
| 123 | + |
| 124 | + pub fn move_item(&mut self, from_index: usize, to_index: usize) -> Result<()> { |
| 125 | + if from_index >= self.items.len() || to_index >= self.items.len() { |
| 126 | + return Err(crate::Error::IndexOutOfBounds( |
| 127 | + from_index.max(to_index), |
| 128 | + self.items.len(), |
| 129 | + )); |
| 130 | + } |
| 131 | + |
| 132 | + if from_index == to_index { |
| 133 | + return Ok(()); |
| 134 | + } |
| 135 | + |
| 136 | + let item = self.items.remove(from_index); |
| 137 | + self.items.insert(to_index, item); |
| 138 | + self.modified_at = SystemTime::now(); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | + |
| 143 | + pub fn clear(&mut self) { |
| 144 | + self.items.clear(); |
| 145 | + self.modified_at = SystemTime::now(); |
| 146 | + } |
| 147 | + |
| 148 | + pub fn item_count(&self) -> usize { |
| 149 | + self.items.len() |
| 150 | + } |
| 151 | + |
| 152 | + pub fn is_empty(&self) -> bool { |
| 153 | + self.items.is_empty() |
| 154 | + } |
| 155 | + |
| 156 | + pub fn items_by_type(&self, media_type: MediaType) -> Vec<&PlaylistItem> { |
| 157 | + self.items |
| 158 | + .iter() |
| 159 | + .filter(|item| item.media_type == media_type) |
| 160 | + .collect() |
| 161 | + } |
| 162 | + |
| 163 | + pub fn search(&self, query: &str) -> Vec<&PlaylistItem> { |
| 164 | + let query_lower = query.to_lowercase(); |
| 165 | + self.items |
| 166 | + .iter() |
| 167 | + .filter(|item| { |
| 168 | + item.name.to_lowercase().contains(&query_lower) |
| 169 | + || item |
| 170 | + .file_path |
| 171 | + .to_string_lossy() |
| 172 | + .to_lowercase() |
| 173 | + .contains(&query_lower) |
| 174 | + }) |
| 175 | + .collect() |
| 176 | + } |
| 177 | + |
| 178 | + pub fn to_json(&self, pretty: bool) -> Result<String> { |
| 179 | + if pretty { |
| 180 | + serde_json::to_string_pretty(self).map_err(|e| crate::Error::Json(e)) |
| 181 | + } else { |
| 182 | + serde_json::to_string(self).map_err(|e| crate::Error::Json(e)) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + pub fn from_json(json: &str) -> Result<Self> { |
| 187 | + serde_json::from_str(json).map_err(|e| crate::Error::Json(e)) |
| 188 | + } |
| 189 | +} |
0 commit comments