|
1 | 1 | use crate::{Context, PlayerScope};
|
2 | 2 | use fuse_rust::Fuse;
|
3 |
| -use lofty::{Accessor, AudioFile, MimeType, Probe, TaggedFileExt}; |
| 3 | +use lofty::{Accessor, AudioFile, MimeType, Probe, TagExt, TaggedFileExt}; |
4 | 4 | use nanoid::nanoid;
|
5 | 5 | use rand::prelude::*;
|
6 | 6 | use rspc::{Router, RouterBuilder, Type};
|
7 |
| -use serde::Serialize; |
| 7 | +use serde::{Deserialize, Serialize}; |
8 | 8 | use std::{
|
9 | 9 | collections::HashMap,
|
10 | 10 | fs::{self, create_dir_all, File},
|
11 | 11 | hash::Hash,
|
12 | 12 | io::Write,
|
13 | 13 | path::PathBuf,
|
| 14 | + process::Command, |
14 | 15 | };
|
15 | 16 | use walkdir::WalkDir;
|
16 | 17 |
|
@@ -187,6 +188,14 @@ struct SearchResults {
|
187 | 188 | songs: Vec<String>,
|
188 | 189 | }
|
189 | 190 |
|
| 191 | +#[derive(Deserialize, Type)] |
| 192 | +struct EditSongInput { |
| 193 | + id: String, |
| 194 | + title: String, |
| 195 | + album: String, |
| 196 | + artist: String, |
| 197 | +} |
| 198 | + |
190 | 199 | pub fn get_router() -> RouterBuilder<Context> {
|
191 | 200 | Router::<Context>::new()
|
192 | 201 | .query("get", |t| {
|
@@ -243,4 +252,55 @@ pub fn get_router() -> RouterBuilder<Context> {
|
243 | 252 | }
|
244 | 253 | })
|
245 | 254 | })
|
| 255 | + .mutation("editSong", |t| { |
| 256 | + t(|ctx, input: EditSongInput| { |
| 257 | + let mut library = ctx.library.lock().unwrap(); |
| 258 | + match library.songs.get(&input.id) { |
| 259 | + Some(song) => { |
| 260 | + match Probe::open(&song.path) |
| 261 | + .ok() |
| 262 | + .map(|f| f.read().ok()) |
| 263 | + .flatten() |
| 264 | + { |
| 265 | + Some(mut tagged_file) => { |
| 266 | + let tags_option = match tagged_file.primary_tag_mut() { |
| 267 | + Some(primary_tag) => Some(primary_tag), |
| 268 | + None => tagged_file.first_tag_mut(), |
| 269 | + }; |
| 270 | + match tags_option { |
| 271 | + Some(tags) => { |
| 272 | + tags.set_title(input.title); |
| 273 | + tags.set_album(input.album); |
| 274 | + tags.set_artist(input.artist); |
| 275 | + for i in 0..tags.picture_count() { |
| 276 | + tags.remove_picture(i as usize); |
| 277 | + } |
| 278 | + if tags.save_to_path(&song.path).is_err() { |
| 279 | + return "Failed to edit song"; |
| 280 | + } |
| 281 | + let sacad = Command::new("sacad_r") |
| 282 | + .args(["-f", ".", "600", "+"]) |
| 283 | + .current_dir(&song.path.parent().unwrap()) |
| 284 | + .status(); |
| 285 | + if !sacad.map(|s| s.success()).unwrap_or(false) { |
| 286 | + return "Failed to download cover art with sacad_r"; |
| 287 | + } |
| 288 | + *library = read_from_dirs(&ctx.config.lock().unwrap().music_folders); |
| 289 | + "Successfully edited" |
| 290 | + } |
| 291 | + None => "Could not edit song", |
| 292 | + } |
| 293 | + } |
| 294 | + None => "Could not find song to edit", |
| 295 | + } |
| 296 | + } |
| 297 | + None => "Could not find song to edit", |
| 298 | + } |
| 299 | + }) |
| 300 | + }) |
| 301 | + .mutation("refresh", |t| { |
| 302 | + t(|ctx, _: ()| { |
| 303 | + *ctx.library.lock().unwrap() = read_from_dirs(&ctx.config.lock().unwrap().music_folders); |
| 304 | + }) |
| 305 | + }) |
246 | 306 | }
|
0 commit comments