Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 58a88b1abd | |||
| 9b181733a6 | |||
| 219b95bb2f |
+2
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "icarus-dm"
|
||||
version = "0.5.0"
|
||||
version = "0.6.0"
|
||||
edition = "2024"
|
||||
|
||||
|
||||
@@ -16,4 +16,4 @@ serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.140"
|
||||
tokio = { version = "1.44.1", features = ["full"] }
|
||||
tokio-util = { version = "0.7.14", features = ["codec"] }
|
||||
icarus-models = { git = "ssh://git@git.kundeng.us/phoenix/icarus-models.git", tag = "v0.1.14" }
|
||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
||||
|
||||
+2
-2
@@ -52,11 +52,11 @@ mod tests {
|
||||
let filename: String = if track < 10 {
|
||||
String::from("track0")
|
||||
+ &track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
+ icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION
|
||||
} else {
|
||||
String::from("track")
|
||||
+ &track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
+ icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION
|
||||
};
|
||||
let disc = 1;
|
||||
match managers::commit_manager::retrieve_song(
|
||||
|
||||
@@ -52,7 +52,9 @@ pub fn retrieve_song(
|
||||
song.album = album.title.clone();
|
||||
song.album_artist = album.artist.clone();
|
||||
song.artist = track.artist.clone();
|
||||
song.audio_type = String::from(icarus_models::constants::DEFAULTMUSICEXTENSION);
|
||||
song.audio_type = String::from(
|
||||
icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION,
|
||||
);
|
||||
song.disc = track.disc.clone();
|
||||
song.disc_count = album.disc_count.clone();
|
||||
song.duration = track.duration as i32;
|
||||
@@ -192,8 +194,8 @@ impl CommitManager {
|
||||
match Runtime::new().unwrap().block_on(result_fut) {
|
||||
Ok(o) => {
|
||||
println!("Success");
|
||||
let filename =
|
||||
String::from("audio") + icarus_models::constants::DEFAULTMUSICEXTENSION;
|
||||
let filename = String::from("audio")
|
||||
+ icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION;
|
||||
let data = o.as_bytes();
|
||||
let mut file = std::fs::File::create(filename).expect("Failed to save");
|
||||
file.write_all(&data)
|
||||
@@ -420,11 +422,11 @@ impl CommitManager {
|
||||
let filename = if track.track < 10 {
|
||||
"track0".to_owned()
|
||||
+ &track.track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
+ icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION
|
||||
} else {
|
||||
"track".to_owned()
|
||||
+ &track.track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
+ icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION
|
||||
};
|
||||
|
||||
songs.push(icarus_models::song::Song {
|
||||
@@ -540,13 +542,15 @@ impl CommitManager {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if extension == icarus_models::constants::WAVEXTENSION[1..]
|
||||
|| extension == icarus_models::constants::FLACEXTENSION[1..]
|
||||
if extension == icarus_models::constants::file_extensions::audio::WAVEXTENSION[1..]
|
||||
|| extension
|
||||
== icarus_models::constants::file_extensions::audio::FLACEXTENSION[1..]
|
||||
{
|
||||
return En::SongFile;
|
||||
} else if extension == "json" {
|
||||
return En::MetadataFile;
|
||||
} else if extension == icarus_models::constants::JPGEXTENSION[1..]
|
||||
} else if extension
|
||||
== icarus_models::constants::file_extensions::image::JPGEXTENSION[1..]
|
||||
|| extension == "jpeg"
|
||||
|| extension == "png"
|
||||
{
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
use crate::models;
|
||||
|
||||
pub fn retrieve_url(api: &models::api::API, with_id: bool, id: i32) -> String {
|
||||
if with_id {
|
||||
retrieve_url_with_id(&api, id)
|
||||
} else {
|
||||
retrieve_url_reg(&api)
|
||||
}
|
||||
}
|
||||
|
||||
fn retrieve_url_reg(api: &models::api::API) -> String {
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
fn retrieve_url_with_id(api: &models::api::API, id: i32) -> String {
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &id.to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
+15
-30
@@ -3,6 +3,7 @@ use std::default::Default;
|
||||
use reqwest;
|
||||
|
||||
use crate::models;
|
||||
use crate::syncers;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Delete {
|
||||
@@ -24,7 +25,7 @@ impl Delete {
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<icarus_models::song::Song, std::io::Error> {
|
||||
self.api.endpoint = "song/data/delete".to_owned();
|
||||
let url = self.retrieve_url(&song);
|
||||
let url = syncers::common::retrieve_url(&self.api, true, song.id);
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let access_token = token.bearer_token();
|
||||
let response = client
|
||||
@@ -33,39 +34,23 @@ impl Delete {
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let mut sng = icarus_models::song::Song::default();
|
||||
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
println!("Success!");
|
||||
let s = response.json::<icarus_models::song::Song>().await;
|
||||
match s {
|
||||
Ok(parsed) => {
|
||||
sng = parsed;
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
}
|
||||
};
|
||||
}
|
||||
other => {
|
||||
panic!("Issue occurred: {:?}", other);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(sng);
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &icarus_models::song::Song) -> String {
|
||||
let api = &self.api;
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.to_string();
|
||||
|
||||
return url;
|
||||
match response.json::<icarus_models::song::Song>().await {
|
||||
Ok(sng) => Ok(sng),
|
||||
Err(er) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
er.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
other => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
other.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
-29
@@ -1,6 +1,7 @@
|
||||
use std::default::Default;
|
||||
|
||||
use crate::models;
|
||||
use crate::syncers;
|
||||
|
||||
pub struct Download {
|
||||
pub api: models::api::API,
|
||||
@@ -27,23 +28,17 @@ impl Download {
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<String, MyError> {
|
||||
self.api.endpoint = String::from("song/data/download");
|
||||
let url = self.retrieve_url(&song);
|
||||
let url = syncers::common::retrieve_url(&self.api, true, song.id);
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
http::header::HeaderValue::from_str(&access_token.clone()).unwrap(),
|
||||
);
|
||||
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let response = client
|
||||
|
||||
match client
|
||||
.get(&url)
|
||||
.header(reqwest::header::AUTHORIZATION, &access_token)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
match response {
|
||||
.await
|
||||
{
|
||||
Ok(rep) => match rep.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
let data = rep.text();
|
||||
@@ -52,35 +47,20 @@ impl Download {
|
||||
return Ok(e);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
return Err(MyError::Other(er.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
return Err(MyError::Other(String::from("Need to grab a new token")));
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
return Err(MyError::Other(other.to_string()));
|
||||
}
|
||||
},
|
||||
Err(er) => {
|
||||
return Err(MyError::Request(er));
|
||||
}
|
||||
}
|
||||
|
||||
return Err(MyError::Other(String::from("Error downloading")));
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &icarus_models::song::Song) -> String {
|
||||
let api = &self.api;
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod common;
|
||||
pub mod delete;
|
||||
pub mod download;
|
||||
pub mod retrieve_records;
|
||||
|
||||
@@ -2,6 +2,7 @@ use std::default::Default;
|
||||
use std::io::Error;
|
||||
|
||||
use crate::models;
|
||||
use crate::syncers;
|
||||
|
||||
pub struct RetrieveRecords {
|
||||
pub api: models::api::API,
|
||||
@@ -21,20 +22,9 @@ impl RetrieveRecords {
|
||||
token: &icarus_models::token::AccessToken,
|
||||
) -> Result<Vec<icarus_models::song::Song>, Error> {
|
||||
self.api.endpoint = String::from("song");
|
||||
let mut songs: Vec<icarus_models::song::Song> = Vec::new();
|
||||
let url = self.retrieve_url();
|
||||
let url = syncers::common::retrieve_url(&self.api, false, 0);
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
http::header::HeaderValue::from_str(&access_token.clone()).unwrap(),
|
||||
);
|
||||
headers.insert(
|
||||
reqwest::header::CONTENT_TYPE,
|
||||
http::header::HeaderValue::from_static("application/json"),
|
||||
);
|
||||
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let response = client
|
||||
.get(&url)
|
||||
@@ -46,35 +36,28 @@ impl RetrieveRecords {
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
// on success, parse our JSON to an APIResponse
|
||||
let s = response.json::<Vec<icarus_models::song::Song>>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
songs = parsed;
|
||||
match response.json::<Vec<icarus_models::song::Song>>().await {
|
||||
Ok(parsed) => Ok(parsed),
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
err.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(_) => println!("Hm, the response didn't match the shape we expected."),
|
||||
};
|
||||
}
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Need to grab a new token",
|
||||
));
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
other.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(songs);
|
||||
}
|
||||
|
||||
fn retrieve_url(&self) -> String {
|
||||
let api = &self.api;
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-21
@@ -4,6 +4,7 @@ use http::HeaderValue;
|
||||
use reqwest;
|
||||
|
||||
use crate::models;
|
||||
use crate::syncers;
|
||||
|
||||
pub struct Upload {
|
||||
pub api: models::api::API,
|
||||
@@ -25,7 +26,7 @@ impl Upload {
|
||||
cover: &icarus_models::coverart::CoverArt,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
self.api.endpoint = String::from("song/data/upload/with/data");
|
||||
let url = self.retrieve_url();
|
||||
let url = syncers::common::retrieve_url(&self.api, false, 0);
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
if url.is_empty() {
|
||||
@@ -80,9 +81,9 @@ impl Upload {
|
||||
println!("\n{}\n", song_detail);
|
||||
|
||||
let mut song_filename = String::from("audio");
|
||||
song_filename += icarus_models::constants::DEFAULTMUSICEXTENSION;
|
||||
song_filename += icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION;
|
||||
let mut cover_filename = String::from("cover");
|
||||
cover_filename += icarus_models::constants::JPGEXTENSION;
|
||||
cover_filename += icarus_models::constants::file_extensions::image::JPGEXTENSION;
|
||||
|
||||
let form = reqwest::multipart::Form::new()
|
||||
.part(
|
||||
@@ -106,22 +107,4 @@ impl Upload {
|
||||
api.version = String::from("v1");
|
||||
self.api = api;
|
||||
}
|
||||
|
||||
fn retrieve_url(&self) -> String {
|
||||
let api = &self.api;
|
||||
let mut buffer = api.url.clone();
|
||||
let count = buffer.len();
|
||||
|
||||
if buffer.chars().nth(count - 1) != Some('/') {
|
||||
buffer += "/";
|
||||
}
|
||||
|
||||
let mut url: String = String::from(&buffer);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user