Code refactoring (#40)

* Code refactoring

* Cleanup
This commit was merged in pull request #40.
This commit is contained in:
KD
2025-03-25 22:14:26 -04:00
committed by GitHub
parent 19d6f2dc33
commit aa946a0a05
3 changed files with 126 additions and 189 deletions
-1
View File
@@ -40,7 +40,6 @@ impl Delete {
println!("Success!");
let s = response.json::<icarus_models::song::Song>().await;
match s {
//
Ok(parsed) => {
sng = parsed;
}
+11 -89
View File
@@ -1,9 +1,7 @@
use std::default::Default;
use http::HeaderMap;
use http::HeaderValue;
use reqwest;
use serde::{Deserialize, Serialize};
use crate::models;
@@ -11,29 +9,6 @@ pub struct Upload {
pub api: models::api::API,
}
#[derive(Debug, Deserialize, Serialize)]
struct Song {
title: String,
album: String,
artist: String,
album_artist: String,
year: i32,
genre: String,
duration: i32,
track: i32,
track_count: i32,
disc: i32,
disc_count: i32,
#[serde(skip_serializing)]
songpath: String,
}
impl Song {
pub fn to_metadata_json(&self) -> Result<String, serde_json::Error> {
return serde_json::to_string_pretty(&self);
}
}
impl Default for Upload {
fn default() -> Self {
Upload {
@@ -48,11 +23,9 @@ impl Upload {
token: &icarus_models::token::AccessToken,
song: &icarus_models::song::Song,
cover: &icarus_models::coverart::CoverArt,
album: &icarus_models::album::collection::Album,
) -> Result<reqwest::Response, reqwest::Error> {
self.api.endpoint = String::from("song/data/upload/with/data");
let url = self.retrieve_url();
let new_song = self.initialize_song(&song, &album);
let access_token = token.bearer_token();
if url.is_empty() {
@@ -61,7 +34,7 @@ impl Upload {
println!("Url: {}", url);
println!("Token: {}", access_token);
println!("Path: {:?}", new_song.songpath);
println!("Path: {:?}", song.song_path());
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
@@ -70,7 +43,7 @@ impl Upload {
);
headers.insert(reqwest::header::ACCEPT, HeaderValue::from_static("*/*"));
let form = self.init_form(&new_song, &cover);
let form = self.init_form(&song, &cover);
let client = reqwest::Client::builder().build().unwrap();
match client
.post(url)
@@ -88,47 +61,21 @@ impl Upload {
}
}
fn _initialize_form(
&self,
song_raw_data: Vec<u8>,
cover_raw_data: Vec<u8>,
song_detail: String,
) -> reqwest::multipart::Form {
let mut headers = HeaderMap::new();
headers.insert(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("application/octet-stream"),
);
let file = reqwest::multipart::Part::bytes(song_raw_data).headers(headers);
let mut headers_i = HeaderMap::new();
headers_i.insert(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("image/jpeg"),
);
let cover = reqwest::multipart::Part::bytes(cover_raw_data).headers(headers_i);
let mut song_filename = String::from("audio");
song_filename += icarus_models::constants::DEFAULTMUSICEXTENSION;
let mut cover_filename = String::from("cover");
cover_filename += icarus_models::constants::JPGEXTENSION;
return reqwest::multipart::Form::new()
.part("cover", cover.file_name(cover_filename))
.text("metadata", song_detail)
.part("file", file.file_name(song_filename));
}
fn init_form(
&self,
song: &Song,
song: &icarus_models::song::Song,
cover: &icarus_models::coverart::CoverArt,
) -> reqwest::multipart::Form {
let songpath = song.songpath.clone();
let songpath = match song.song_path() {
Ok(s) => s,
Err(_) => String::new(),
};
let coverpath = cover.path.clone();
println!("Cover path: {:?}", coverpath);
let song_detail = song.to_metadata_json().unwrap();
let song_detail = match song.to_metadata_json(true) {
Ok(s) => s,
Err(_) => String::new(),
};
println!("\n{}\n", song_detail);
@@ -177,29 +124,4 @@ impl Upload {
return url;
}
fn initialize_song(
&self,
song: &icarus_models::song::Song,
album: &icarus_models::album::collection::Album,
) -> Song {
let dur = song.duration.clone();
println!("Duration: {}", dur);
return Song {
title: String::from(&song.title.clone()),
album: album.title.clone(),
artist: String::from(&song.artist.clone().clone()),
album_artist: album.artist.clone(),
year: album.year.clone(),
genre: album.genre.clone(),
// duration: f64::round(dur) as i32,
duration: dur,
track: (song.track.clone()),
track_count: album.track_count.clone(),
disc: song.disc.clone(),
disc_count: album.disc_count.clone(),
songpath: song.directory.clone() + "/" + &song.filename.clone(),
};
}
}