Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 24e066d8d9 | |||
| ab08792ff4 | |||
| 9799edef33 | |||
| 92eb31a687 | |||
| aa946a0a05 | |||
| 19d6f2dc33 | |||
| 4824e6f33a | |||
| 64935ea772 | |||
| de90b0d0a1 | |||
| 19b943eed8 | |||
| 8c781398b4 | |||
| c575d2e523 | |||
| 03ed3deae6 | |||
| b69c111b66 | |||
| 8d97d4eb70 | |||
| 2f00ce6abf | |||
| e3cd1f4018 | |||
| a9f9ba8350 | |||
| df60bef381 | |||
| 038142eb8a | |||
| 69b81bc15c | |||
| b2e246d175 | |||
| c76e7b71b5 | |||
| b8dccca158 | |||
| b52607b4e3 | |||
| ac970a4302 | |||
| a372bfcc05 | |||
| d22850b280 | |||
| 4d170f6a85 | |||
| 7391b1db68 | |||
| 772641a8c4 | |||
| 4c99f88baf | |||
| c3718c5add | |||
| a4d026e41f | |||
| 6aa6f548a9 | |||
| 53b83a809c |
@@ -2,9 +2,9 @@ name: Rust
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "rust-lg" ]
|
||||
branches: [ "master" ]
|
||||
pull_request:
|
||||
branches: [ "rust-lg" ]
|
||||
branches: [ "master" ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
@@ -12,12 +12,11 @@ env:
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: macos-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
name: Rust
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust version
|
||||
run: rustup install 1.85.0 && rustup default 1.85.0
|
||||
|
||||
- name: Debug secret
|
||||
run: echo "${{ secrets.MYREPO_TOKEN }}" | head -c 10 ; echo "..."
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "$EXTREPO_KEY" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
ssh-keyscan git.kundeng.us ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLl/OZiKVDxwnyvMxa+rjKvDpKqTxH1GWuGuDPLmENGQMbTVulajZWr9x8Q1cotoJiHZkt7DA5vczcjB/4lwgWA= >> ~/.ssh/known_hosts
|
||||
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/gitlab_deploy_key
|
||||
cargo build --release
|
||||
env:
|
||||
EXTREPO_KEY: ${{ secrets.MYREPO_TOKEN }}
|
||||
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
+10
-9
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "icarus-dm"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
version = "0.5.0"
|
||||
edition = "2024"
|
||||
|
||||
|
||||
|
||||
@@ -9,10 +9,11 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
futures = { version = "0.3.30" }
|
||||
http = { version = "1.1.0" }
|
||||
reqwest = { version = "0.12.4", features = ["json", "blocking", "multipart", "stream"] }
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
serde_json = "1.0.115"
|
||||
tokio = { version = "1.37", features = ["full"] }
|
||||
tokio-util = { version = "0.7.11", features = ["codec"] }
|
||||
futures = { version = "0.3.31" }
|
||||
http = { version = "1.3.1" }
|
||||
reqwest = { version = "0.12.15", features = ["json", "blocking", "multipart", "stream"] }
|
||||
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" }
|
||||
|
||||
@@ -43,7 +43,7 @@ icarus-dm download -u spacecadet -p stellar40 -h https://icarus.com -b 15
|
||||
### Uploading Song with metadata
|
||||
|
||||
```BASH
|
||||
icarus-dm upload-meta -u spacecadet -p stellar40 -h https://icarus.com -s /path/of/song.mp3 -t 1 -m /path/to/metadata/config/collection.json -ca /path/to/cover/art/image.png
|
||||
icarus-dm upload-meta -u spacecadet -p stellar40 -h https://icarus.com -s /path/of/song.flac -t 1 -m /path/to/metadata/config/collection.json -ca /path/to/cover/art/image.png
|
||||
```
|
||||
|
||||
### Uploading Song with metadata from directory
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
pub const WAV_FILE_EXTENSION: &str = ".wav";
|
||||
pub const _MP3_FILE_EXTENSION: &str = ".mp3";
|
||||
pub const JPG_FILE_EXTENSION: &str = ".jpg";
|
||||
@@ -1 +0,0 @@
|
||||
pub mod file_extensions;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
pub fn print_help() {
|
||||
let msg: String = String::from(
|
||||
r#"icd [Action] [flag]
|
||||
|
||||
Actions
|
||||
download
|
||||
upload-meta
|
||||
retrieve
|
||||
delete
|
||||
|
||||
Flags
|
||||
Required for all actions
|
||||
-u username
|
||||
-p password
|
||||
-h host
|
||||
|
||||
Required for upload with metadata
|
||||
-s path of song
|
||||
-t track number
|
||||
-m metadata filepath
|
||||
-ca coverart filepath
|
||||
-scma directory where songs, metadata, and cover art exists and will be uploaded (Optional)
|
||||
|
||||
Required for download
|
||||
-b song id
|
||||
-d path to download song (Optional)
|
||||
|
||||
Required for retrieving records
|
||||
-rt retrieve type (songs is only accepted)
|
||||
|
||||
Required for deleting a song
|
||||
-D song id"#,
|
||||
);
|
||||
|
||||
println!("{}", msg);
|
||||
}
|
||||
+121
-56
@@ -1,4 +1,4 @@
|
||||
mod constants;
|
||||
mod help;
|
||||
mod managers;
|
||||
mod models;
|
||||
mod parsers;
|
||||
@@ -6,71 +6,23 @@ mod syncers;
|
||||
mod utilities;
|
||||
|
||||
use std::env;
|
||||
use std::process;
|
||||
|
||||
fn exit_program(code: i32) {
|
||||
process::exit(code);
|
||||
}
|
||||
|
||||
fn print_help() {
|
||||
let msg: String = String::from(
|
||||
r#"icd [Action] [flag]
|
||||
|
||||
Actions
|
||||
download
|
||||
upload-meta
|
||||
retrieve
|
||||
delete
|
||||
|
||||
Flags
|
||||
Required for all actions
|
||||
-u username
|
||||
-p password
|
||||
-h host
|
||||
|
||||
Required for upload with metadata
|
||||
-s path of song
|
||||
-t track number
|
||||
-m metadata filepath
|
||||
-ca coverart filepath
|
||||
-scma directory where songs, metadata, and cover art exists and will be uploaded (Optional)
|
||||
|
||||
Required for download
|
||||
-b song id
|
||||
-d path to download song (Optional)
|
||||
|
||||
Required for retrieving records
|
||||
-rt retrieve type (songs is only accepted)
|
||||
|
||||
Required for deleting a song
|
||||
-D song id"#,
|
||||
);
|
||||
|
||||
println!("{}", msg);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
if args.len() == 1 {
|
||||
print_help();
|
||||
exit_program(-1);
|
||||
}
|
||||
|
||||
let args_len = args.len() as i32;
|
||||
|
||||
if args_len == 1 {
|
||||
help::print_help();
|
||||
utilities::checks::exit_program(-1);
|
||||
}
|
||||
|
||||
println!("Argument count: {}", args_len);
|
||||
|
||||
let mut act_mgr = managers::action_managers::ActionManager {
|
||||
action: String::new(),
|
||||
flags: Vec::new(),
|
||||
params: args,
|
||||
param_count: args_len,
|
||||
};
|
||||
let mut act_mgr = managers::action_managers::ActionManager::default();
|
||||
act_mgr.set_params(&args);
|
||||
act_mgr.initialize();
|
||||
|
||||
let chosen_act = act_mgr.retrieve_icarus_action();
|
||||
|
||||
chosen_act.print_action_and_flags();
|
||||
|
||||
let mut cmt_mgr = managers::commit_manager::CommitManager {
|
||||
@@ -79,3 +31,116 @@ fn main() {
|
||||
|
||||
cmt_mgr.commit_action();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::managers;
|
||||
|
||||
#[test]
|
||||
fn test_song_prep_upload() {
|
||||
let meta_path = String::from("tests/sample2_tracks/album.json");
|
||||
|
||||
if !std::path::Path::new(&meta_path).exists() {
|
||||
assert!(false, "File does not exists: {:?}", meta_path);
|
||||
}
|
||||
|
||||
match icarus_models::album::collection::parse_album(&meta_path) {
|
||||
Ok(album) => {
|
||||
for track in 1..3 {
|
||||
let directory_path = std::path::Path::new(&meta_path);
|
||||
let directory: String = directory_path.parent().unwrap().display().to_string();
|
||||
let filename: String = if track < 10 {
|
||||
String::from("track0")
|
||||
+ &track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
} else {
|
||||
String::from("track")
|
||||
+ &track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
};
|
||||
let disc = 1;
|
||||
match managers::commit_manager::retrieve_song(
|
||||
&album, track, disc, &directory, &filename,
|
||||
) {
|
||||
Ok(song) => match song.to_data() {
|
||||
Ok(_) => {
|
||||
print!("Success");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err.to_string());
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_minimum_action_and_args() {
|
||||
let args: Vec<String> = vec![
|
||||
"icarus-dm".to_string(),
|
||||
"download".to_string(),
|
||||
"-u".to_string(),
|
||||
"jamborie".to_string(),
|
||||
"-p".to_string(),
|
||||
"somethingsecr3t!".to_string(),
|
||||
"-h".to_string(),
|
||||
"https://music-server.com".to_string(),
|
||||
];
|
||||
let arg_count = args.len() as i32;
|
||||
let minimum_arg_count = 8;
|
||||
assert!(arg_count >= minimum_arg_count);
|
||||
|
||||
let mut act_mgr = managers::action_managers::ActionManager {
|
||||
action: String::new(),
|
||||
flags: Vec::new(),
|
||||
params: args,
|
||||
param_count: arg_count,
|
||||
};
|
||||
act_mgr.initialize();
|
||||
let ica = act_mgr.retrieve_icarus_action();
|
||||
let action = &ica.action;
|
||||
let flags = &ica.flags;
|
||||
|
||||
assert!(action != "");
|
||||
assert!(flags.len() > 2);
|
||||
assert!(
|
||||
!(action != "download"
|
||||
&& action != "upload"
|
||||
&& action != "retrieve"
|
||||
&& action != "upload-meta")
|
||||
);
|
||||
|
||||
let mut found_count = 0;
|
||||
let mut flags_already_read = Vec::new();
|
||||
|
||||
for flag in flags {
|
||||
if flags_already_read.contains(&flag.flag) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if flag.flag == "-u" {
|
||||
found_count += 1;
|
||||
flags_already_read.push(flag.flag.clone());
|
||||
} else if flag.flag == "-p" {
|
||||
found_count += 1;
|
||||
flags_already_read.push(flag.flag.clone());
|
||||
} else if flag.flag == "-h" {
|
||||
found_count += 1;
|
||||
flags_already_read.push(flag.flag.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let all_flags_found = found_count == 3;
|
||||
|
||||
assert_eq!(found_count, 3, "Three flags are required: -u, -p, -h");
|
||||
assert!(all_flags_found, "All flags have not been found");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models;
|
||||
use crate::{models, utilities};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ActionManager {
|
||||
@@ -10,6 +10,17 @@ pub struct ActionManager {
|
||||
pub param_count: i32,
|
||||
}
|
||||
|
||||
impl Default for ActionManager {
|
||||
fn default() -> Self {
|
||||
ActionManager {
|
||||
action: String::new(),
|
||||
flags: Vec::new(),
|
||||
params: Vec::new(),
|
||||
param_count: -1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ActionManager {
|
||||
pub fn retrieve_icarus_action(&self) -> models::icarus_action::IcarusAction {
|
||||
return models::icarus_action::IcarusAction {
|
||||
@@ -45,6 +56,11 @@ impl ActionManager {
|
||||
self.action = self.action.to_lowercase();
|
||||
}
|
||||
|
||||
pub fn set_params(&mut self, args: &Vec<String>) {
|
||||
self.params = args.clone();
|
||||
self.param_count = self.params.len() as i32;
|
||||
}
|
||||
|
||||
fn validate_flags(&mut self) {
|
||||
println!("Validating flags");
|
||||
|
||||
@@ -70,7 +86,7 @@ impl ActionManager {
|
||||
flg.flag = String::from(flag);
|
||||
} else {
|
||||
println!("Flag {} is not valid", flag);
|
||||
std::process::exit(-1);
|
||||
utilities::checks::exit_program(-1);
|
||||
}
|
||||
|
||||
self.flags.push(flg);
|
||||
@@ -102,7 +118,6 @@ impl ActionManager {
|
||||
|
||||
fn does_flag_have_value(&self, flag: &String) -> bool {
|
||||
let flags_tmp = self.parsed_flags();
|
||||
|
||||
let mut i_found: i32 = -1;
|
||||
|
||||
for i in 0..flags_tmp.len() {
|
||||
@@ -151,71 +166,3 @@ impl ActionManager {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::managers::action_managers::ActionManager;
|
||||
|
||||
#[test]
|
||||
fn minimum_action_and_args() {
|
||||
let args: Vec<String> = vec![
|
||||
"icarus-dm".to_string(),
|
||||
"download".to_string(),
|
||||
"-u".to_string(),
|
||||
"jamborie".to_string(),
|
||||
"-p".to_string(),
|
||||
"somethingsecr3t!".to_string(),
|
||||
"-h".to_string(),
|
||||
"https://music-server.com".to_string(),
|
||||
];
|
||||
let arg_count = args.len() as i32;
|
||||
let minimum_arg_count = 8;
|
||||
assert!(arg_count >= minimum_arg_count);
|
||||
|
||||
let mut act_mgr = ActionManager {
|
||||
action: String::new(),
|
||||
flags: Vec::new(),
|
||||
params: args,
|
||||
param_count: arg_count,
|
||||
};
|
||||
act_mgr.initialize();
|
||||
let ica = act_mgr.retrieve_icarus_action();
|
||||
let action = &ica.action;
|
||||
let flags = &ica.flags;
|
||||
|
||||
assert!(action != "");
|
||||
assert!(flags.len() > 2);
|
||||
assert!(
|
||||
!(action != "download"
|
||||
&& action != "upload"
|
||||
&& action != "retrieve"
|
||||
&& action != "upload-meta")
|
||||
);
|
||||
|
||||
let mut all_flags_found = false;
|
||||
let mut found_count = 0;
|
||||
let mut flags_already_read = Vec::new();
|
||||
|
||||
for flag in flags {
|
||||
if flags_already_read.contains(&flag.flag) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if flag.flag == "-u" {
|
||||
found_count += 1;
|
||||
flags_already_read.push(flag.flag.clone());
|
||||
} else if flag.flag == "-p" {
|
||||
found_count += 1;
|
||||
flags_already_read.push(flag.flag.clone());
|
||||
} else if flag.flag == "-h" {
|
||||
found_count += 1;
|
||||
flags_already_read.push(flag.flag.clone());
|
||||
}
|
||||
}
|
||||
|
||||
all_flags_found = found_count == 3;
|
||||
|
||||
assert_eq!(found_count, 3, "Three flags are required: -u, -p, -h");
|
||||
assert!(all_flags_found, "All flags have not been found");
|
||||
}
|
||||
}
|
||||
|
||||
+265
-391
@@ -8,11 +8,10 @@ use serde::{Deserialize, Serialize};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
use crate::managers;
|
||||
use crate::models::song::Album;
|
||||
use crate::models::{self};
|
||||
use crate::parsers;
|
||||
use crate::syncers;
|
||||
use crate::utilities;
|
||||
use crate::{constants, parsers};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct CommitManager {
|
||||
@@ -29,10 +28,6 @@ enum ActionValues {
|
||||
None,
|
||||
}
|
||||
|
||||
enum _RetrieveTypes {
|
||||
Songs,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum En {
|
||||
ImageFile,
|
||||
@@ -41,36 +36,47 @@ enum En {
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Album {
|
||||
pub fn _print_info(&self) {
|
||||
println!("Album: {}", self.title);
|
||||
println!("Album Artist: {}", self.album_artist);
|
||||
println!("Genre: {}", self.genre);
|
||||
println!("Year: {}", self.year);
|
||||
println!("Track Count: {}", self.track_count);
|
||||
println!("Disc Count: {}\n", self.disc_count);
|
||||
pub fn retrieve_song(
|
||||
album: &icarus_models::album::collection::Album,
|
||||
track: i32,
|
||||
disc: i32,
|
||||
directory: &String,
|
||||
filename: &String,
|
||||
) -> Result<icarus_models::song::Song> {
|
||||
let mut found = false;
|
||||
let mut song = icarus_models::song::Song::default();
|
||||
|
||||
for song_i in &album.tracks {
|
||||
if song_i.track == track && song_i.disc == disc {
|
||||
let track = song_i.clone();
|
||||
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.disc = track.disc.clone();
|
||||
song.disc_count = album.disc_count.clone();
|
||||
song.duration = track.duration as i32;
|
||||
song.genre = album.genre.clone();
|
||||
song.title = track.title.clone();
|
||||
song.year = album.year.clone();
|
||||
song.track = track.track.clone();
|
||||
song.track_count = album.track_count.clone();
|
||||
song.directory = directory.clone();
|
||||
song.filename = filename.clone();
|
||||
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn retrieve_song(&self, track: i32, disc: i32) -> Result<models::song::Song> {
|
||||
let mut found = false;
|
||||
let mut song = models::song::Song::default();
|
||||
|
||||
for song_i in &self.songs {
|
||||
if song_i.track.unwrap() == track && song_i.disc.unwrap() == disc {
|
||||
song = song_i.clone();
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
return Ok(song);
|
||||
}
|
||||
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"Song not found",
|
||||
));
|
||||
if found {
|
||||
return Ok(song);
|
||||
}
|
||||
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"Song not found",
|
||||
));
|
||||
}
|
||||
|
||||
impl CommitManager {
|
||||
@@ -110,15 +116,17 @@ impl CommitManager {
|
||||
}
|
||||
|
||||
fn map_actions(&self) -> HashMap<String, ActionValues> {
|
||||
let mut actions: HashMap<String, ActionValues> = HashMap::new();
|
||||
actions.insert("download".to_string(), ActionValues::DownloadAct);
|
||||
actions.insert("upload".to_string(), ActionValues::UploadAct);
|
||||
actions.insert(
|
||||
"upload-meta".to_string(),
|
||||
ActionValues::UploadSongWithMetadata,
|
||||
);
|
||||
actions.insert("retrieve".to_string(), ActionValues::RetrieveAct);
|
||||
actions.insert("delete".to_string(), ActionValues::DeleteAct);
|
||||
let actions: HashMap<String, ActionValues> = HashMap::from([
|
||||
("download".to_string(), ActionValues::DownloadAct),
|
||||
("download".to_string(), ActionValues::DownloadAct),
|
||||
("upload".to_string(), ActionValues::UploadAct),
|
||||
(
|
||||
"upload-meta".to_string(),
|
||||
ActionValues::UploadSongWithMetadata,
|
||||
),
|
||||
("retrieve".to_string(), ActionValues::RetrieveAct),
|
||||
("delete".to_string(), ActionValues::DeleteAct),
|
||||
]);
|
||||
|
||||
return actions;
|
||||
}
|
||||
@@ -135,14 +143,14 @@ impl CommitManager {
|
||||
|
||||
println!("Deleting song");
|
||||
|
||||
let mut song = models::song::Song::default();
|
||||
let mut song = icarus_models::song::Song::default();
|
||||
|
||||
for arg in &self.ica_action.flags {
|
||||
let flag = &arg.flag;
|
||||
let value = &arg.value;
|
||||
|
||||
if flag == "-D" {
|
||||
song.id = Some(value.parse::<i32>().unwrap());
|
||||
song.id = value.parse::<i32>().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +173,7 @@ impl CommitManager {
|
||||
fn download_song(&self) {
|
||||
println!("Deleting song");
|
||||
let dwn = self.ica_action.retrieve_flag_value(&String::from("-b"));
|
||||
let num: i32 = dwn.parse::<i32>().unwrap();
|
||||
let id: i32 = dwn.parse::<i32>().unwrap();
|
||||
|
||||
let mut prsr = parsers::api_parser::APIParser {
|
||||
api: models::api::API::default(),
|
||||
@@ -175,29 +183,38 @@ impl CommitManager {
|
||||
|
||||
let api = prsr.retrieve_api();
|
||||
let token = self.parse_token(&api);
|
||||
println!("Message: {}", token.message);
|
||||
|
||||
let mut dwn_loader = syncers::download::Download { api: api.clone() };
|
||||
let mut song = models::song::Song::default();
|
||||
song.id = Some(num);
|
||||
let mut song = icarus_models::song::Song::default();
|
||||
song.id = id;
|
||||
let result_fut = dwn_loader.download_song(&token, &song);
|
||||
let result = Runtime::new().unwrap().block_on(result_fut);
|
||||
match result {
|
||||
match Runtime::new().unwrap().block_on(result_fut) {
|
||||
Ok(o) => {
|
||||
println!("Success");
|
||||
let mut filename = String::from("audio");
|
||||
filename += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||
let filename =
|
||||
String::from("audio") + icarus_models::constants::DEFAULTMUSICEXTENSION;
|
||||
let data = o.as_bytes();
|
||||
let mut file = std::fs::File::create(filename).expect("Failed to save");
|
||||
file.write_all(&data).expect("ff");
|
||||
file.write_all(&data)
|
||||
.expect("Failed to save downloaded song");
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
match er {
|
||||
syncers::download::MyError::Request(error) => {
|
||||
println!("Error: {:?}", error);
|
||||
}
|
||||
syncers::download::MyError::Other(ss) => {
|
||||
println!("Error: {:?}", ss);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn retrieve_object(&self) {
|
||||
println!("Deleting song");
|
||||
println!("Retrieving song");
|
||||
let rt = self.ica_action.retrieve_flag_value(&String::from("-rt"));
|
||||
|
||||
if rt != "songs" {
|
||||
@@ -215,11 +232,13 @@ impl CommitManager {
|
||||
let mut repo = syncers::retrieve_records::RetrieveRecords { api: api.clone() };
|
||||
let result_fut = repo.get_all_songs(&token);
|
||||
|
||||
let result = Runtime::new().unwrap().block_on(result_fut);
|
||||
match result {
|
||||
match Runtime::new().unwrap().block_on(result_fut) {
|
||||
Ok(o) => {
|
||||
for son in o {
|
||||
son.print_info();
|
||||
for song in o {
|
||||
println!("Title: {:?}", song.title);
|
||||
println!("Artist: {:?}", song.artist);
|
||||
println!("Album: {:?}", song.album);
|
||||
println!("Year: {:?}", song.year);
|
||||
}
|
||||
}
|
||||
Err(er) => {
|
||||
@@ -233,15 +252,12 @@ impl CommitManager {
|
||||
panic!("Not supported");
|
||||
}
|
||||
|
||||
fn parse_token(&self, api: &models::api::API) -> models::token::Token {
|
||||
fn parse_token(&self, api: &models::api::API) -> icarus_models::token::AccessToken {
|
||||
println!("Fetching token");
|
||||
|
||||
let mut usr_mgr: managers::user_manager::UserManager =
|
||||
managers::user_manager::UserManager {
|
||||
user: models::user::User {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
},
|
||||
user: icarus_models::user::User::default(),
|
||||
ica_action: self.ica_action.clone(),
|
||||
};
|
||||
usr_mgr.parse_user_from_actions();
|
||||
@@ -309,6 +325,8 @@ impl CommitManager {
|
||||
let api = prsr.retrieve_api();
|
||||
let token = self.parse_token(&api);
|
||||
|
||||
println!("Token: {:?}", token.token);
|
||||
|
||||
let song_file = std::path::Path::new(&songpath);
|
||||
|
||||
if !song_file.exists() {
|
||||
@@ -316,70 +334,124 @@ impl CommitManager {
|
||||
panic!("Error");
|
||||
}
|
||||
|
||||
let mut cover_art = models::song::CoverArt::default();
|
||||
let mut song = models::song::Song::default();
|
||||
let mut filenames = Vec::new();
|
||||
let mut fp = String::new();
|
||||
let mut dir = String::new();
|
||||
|
||||
let entry = &song_file;
|
||||
|
||||
let file_name = std::ffi::OsString::from(entry.file_name().unwrap());
|
||||
|
||||
println!("file name: {:?}", file_name);
|
||||
let mut cover_art = icarus_models::coverart::CoverArt {
|
||||
id: 0,
|
||||
title: String::new(),
|
||||
path: cover_path.clone(),
|
||||
data: Vec::new(),
|
||||
};
|
||||
let file_name = std::ffi::OsString::from(&song_file.file_name().unwrap());
|
||||
|
||||
match self.find_file_extension(&file_name) {
|
||||
En::ImageFile => {}
|
||||
En::MetadataFile => {}
|
||||
En::SongFile => {
|
||||
let fname = self.o_to_string(&file_name);
|
||||
En::SongFile => match utilities::string::o_to_string(&file_name) {
|
||||
Ok(s) => {
|
||||
println!("file name: {:?}", file_name);
|
||||
|
||||
match fname {
|
||||
Ok(s) => {
|
||||
filenames.push(s.clone());
|
||||
fp = s.clone();
|
||||
dir = song_file.parent().unwrap().display().to_string();
|
||||
song.filepath = Some(s.clone());
|
||||
song.directory = Some(dir.clone());
|
||||
self.initialize_disc_and_track(&mut song);
|
||||
match icarus_models::album::collection::parse_album(&meta_path) {
|
||||
Ok(album) => {
|
||||
let filename = s.clone();
|
||||
let directory = song_file.parent().unwrap().display().to_string();
|
||||
let trck = i32::from_str(track_id).unwrap();
|
||||
let mut s =
|
||||
retrieve_song(&album, trck, 1, &directory, &filename).unwrap();
|
||||
println!("Directory: {:?}", s.directory);
|
||||
println!("Filename: {:?}", s.filename);
|
||||
println!("Path: {:?}", s.song_path());
|
||||
s.data = s.to_data().unwrap();
|
||||
|
||||
cover_art.data = cover_art.to_data().unwrap();
|
||||
|
||||
let mut up = syncers::upload::Upload::default();
|
||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||
up.set_api(&host);
|
||||
|
||||
let res = up.upload_song_with_metadata(&token, &s, &cover_art);
|
||||
|
||||
match Runtime::new().unwrap().block_on(res) {
|
||||
Ok(o) => {
|
||||
println!("Successfully sent {:?}", o);
|
||||
Ok(())
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Some error {:?}", er);
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
er.to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
err.to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
Err(er) => println!("Error: {:?}", er),
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
cover_art.path = Some(cover_path.clone());
|
||||
|
||||
let album = self.retrieve_metadata(&meta_path);
|
||||
let trck = i32::from_str(track_id).unwrap();
|
||||
let mut s = album.retrieve_song(trck, 1).unwrap();
|
||||
s.filepath = Some(fp);
|
||||
s.directory = Some(dir);
|
||||
s.genre = Some(album.genre.clone());
|
||||
s.year = Some(album.year.clone());
|
||||
s.album = Some(album.title.clone());
|
||||
s.data = Some(s.to_data().unwrap());
|
||||
|
||||
cover_art.data = Some(cover_art.to_data().unwrap());
|
||||
|
||||
let mut up = syncers::upload::Upload::default();
|
||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||
up.set_api(&host);
|
||||
|
||||
let res = up.upload_song_with_metadata(&token, &s, &cover_art, &album);
|
||||
let tken = Runtime::new().unwrap().block_on(res);
|
||||
|
||||
match &tken {
|
||||
Ok(o) => {
|
||||
println!("Successfully sent {:?}", o);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Some error {:?}", er);
|
||||
Err(er) => {
|
||||
println!("Error: {:?}", er);
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
er.to_string(),
|
||||
));
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"No sutitable file found".to_owned(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
fn get_songs(
|
||||
&self,
|
||||
metadata_path: &String,
|
||||
source_directory: &String,
|
||||
) -> Result<Vec<icarus_models::song::Song>> {
|
||||
match icarus_models::album::collection::parse_album(metadata_path) {
|
||||
Ok(albums) => {
|
||||
let mut songs: Vec<icarus_models::song::Song> = Vec::new();
|
||||
|
||||
for track in &albums.tracks {
|
||||
let filename = if track.track < 10 {
|
||||
"track0".to_owned()
|
||||
+ &track.track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
} else {
|
||||
"track".to_owned()
|
||||
+ &track.track.to_string()
|
||||
+ icarus_models::constants::DEFAULTMUSICEXTENSION
|
||||
};
|
||||
|
||||
songs.push(icarus_models::song::Song {
|
||||
id: -1,
|
||||
title: track.title.clone(),
|
||||
artist: track.artist.clone(),
|
||||
disc: track.disc.clone(),
|
||||
track: track.track.clone(),
|
||||
duration: track.duration.clone() as i32,
|
||||
year: albums.year.clone(),
|
||||
album_artist: albums.artist.clone(),
|
||||
genre: albums.genre.clone(),
|
||||
disc_count: albums.disc_count.clone(),
|
||||
track_count: albums.track_count.clone(),
|
||||
album: albums.title.clone(),
|
||||
audio_type: String::from("FLAC"),
|
||||
directory: source_directory.clone(),
|
||||
filename: filename,
|
||||
user_id: -1,
|
||||
data: Vec::new(),
|
||||
date_created: String::new(),
|
||||
});
|
||||
}
|
||||
Ok(songs)
|
||||
}
|
||||
Err(_) => Ok(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
||||
@@ -397,150 +469,52 @@ impl CommitManager {
|
||||
panic!("Directory does not exist");
|
||||
}
|
||||
|
||||
let mut cover_art = models::song::CoverArt::default();
|
||||
let mut songs: Vec<models::song::Song> = Vec::new();
|
||||
let mut filenames: Vec<String> = Vec::new();
|
||||
let mut metadatapath: String = String::new();
|
||||
|
||||
// iterate files in metadatapath
|
||||
let path = std::path::Path::new(directory_path);
|
||||
|
||||
for entry in read_dir(path)? {
|
||||
let entry = entry?;
|
||||
|
||||
let file_type = entry.file_type();
|
||||
let file_name = entry.file_name();
|
||||
|
||||
println!("file type: {:?}", file_type);
|
||||
println!("file name: {:?}", file_name);
|
||||
|
||||
match self.find_file_extension(&file_name) {
|
||||
En::ImageFile => {
|
||||
let directory_part = sourcepath.clone();
|
||||
let fname = self.o_to_string(&file_name);
|
||||
let fullpath = directory_part + "/" + &fname.unwrap();
|
||||
cover_art.path = Some(fullpath);
|
||||
}
|
||||
En::MetadataFile => {
|
||||
let directory_part = sourcepath.clone();
|
||||
let fname = self.o_to_string(&file_name);
|
||||
metadatapath = directory_part + "/" + &fname.unwrap();
|
||||
}
|
||||
En::SongFile => {
|
||||
let mut song = models::song::Song::default();
|
||||
let fname = self.o_to_string(&file_name);
|
||||
|
||||
match fname {
|
||||
Ok(s) => {
|
||||
filenames.push(s.clone());
|
||||
song.filepath = Some(s.clone());
|
||||
song.directory = Some(sourcepath.clone());
|
||||
song.data = Some(song.to_data().unwrap());
|
||||
self.initialize_disc_and_track(&mut song);
|
||||
}
|
||||
Err(er) => println!("Error: {:?}", er),
|
||||
}
|
||||
|
||||
songs.push(song)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
filenames.sort();
|
||||
|
||||
let mut album = self.retrieve_metadata(&metadatapath);
|
||||
|
||||
self.song_parsing(&mut album, &sourcepath, &filenames);
|
||||
let mut cover_art = icarus_models::coverart::CoverArt {
|
||||
id: 0,
|
||||
title: String::new(),
|
||||
path: match self.get_cover_art_path(&sourcepath) {
|
||||
Ok(o) => o,
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
data: Vec::new(),
|
||||
};
|
||||
let metadatapath = match self.get_metadata_path(&sourcepath) {
|
||||
Ok(o) => o,
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
let mut up = syncers::upload::Upload::default();
|
||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||
up.set_api(&host);
|
||||
|
||||
cover_art.data = Some(cover_art.to_data().unwrap());
|
||||
cover_art.data = cover_art.to_data().unwrap();
|
||||
|
||||
println!("");
|
||||
|
||||
for sng in &mut album.songs {
|
||||
match sng.data {
|
||||
Some(_) => {}
|
||||
None => {
|
||||
sng.data = Some(sng.to_data().unwrap());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
for song in &album.songs {
|
||||
// Upload each song
|
||||
println!("Sending song...");
|
||||
let res = up.upload_song_with_metadata(&token, &song, &cover_art, &album);
|
||||
let tken = Runtime::new().unwrap().block_on(res);
|
||||
|
||||
match &tken {
|
||||
Ok(o) => {
|
||||
println!("Successfully sent {:?}", o);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Some error {:?}", er);
|
||||
match self.get_songs(&metadatapath, &sourcepath) {
|
||||
Ok(sngs) => {
|
||||
for song in sngs {
|
||||
match Runtime::new()
|
||||
.unwrap()
|
||||
.block_on(up.upload_song_with_metadata(&token, &song, &cover_art))
|
||||
{
|
||||
Ok(o) => {
|
||||
println!("Response: {:?}", o);
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
println!("");
|
||||
Err(error) => {
|
||||
println!("Error: {:?}", error);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Makes sure the elements in album.songs is populated
|
||||
fn song_parsing(
|
||||
&self,
|
||||
album: &mut models::song::Album,
|
||||
directory: &String,
|
||||
filenames: &Vec<String>,
|
||||
) {
|
||||
// Apply directory
|
||||
for song in &mut album.songs {
|
||||
let dir = &song.directory;
|
||||
match dir {
|
||||
Some(s) => println!("{}", s),
|
||||
None => {
|
||||
song.directory = Some(directory.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply filename
|
||||
let mut index = 0;
|
||||
for song in &mut album.songs {
|
||||
let filename = filenames[index].clone();
|
||||
song.filepath = Some(filename);
|
||||
index += 1;
|
||||
}
|
||||
|
||||
for song in &mut album.songs {
|
||||
match &mut song.album {
|
||||
Some(_) => {}
|
||||
None => {
|
||||
song.album = Some(album.title.clone());
|
||||
}
|
||||
}
|
||||
|
||||
match &mut song.genre {
|
||||
Some(_) => {}
|
||||
None => {
|
||||
song.genre = Some(album.genre.clone());
|
||||
}
|
||||
}
|
||||
|
||||
match &mut song.year {
|
||||
Some(_) => {}
|
||||
None => {
|
||||
song.year = Some(album.year.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_file_extension(&self, file_name: &std::ffi::OsString) -> En {
|
||||
let file_name_str = Some(file_name.clone().into_string());
|
||||
|
||||
@@ -566,11 +540,16 @@ impl CommitManager {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if extension == "wav" || extension == "flac" {
|
||||
if extension == icarus_models::constants::WAVEXTENSION[1..]
|
||||
|| extension == icarus_models::constants::FLACEXTENSION[1..]
|
||||
{
|
||||
return En::SongFile;
|
||||
} else if extension == "json" {
|
||||
return En::MetadataFile;
|
||||
} else if extension == "jpg" || extension == "jpeg" || extension == "png" {
|
||||
} else if extension == icarus_models::constants::JPGEXTENSION[1..]
|
||||
|| extension == "jpeg"
|
||||
|| extension == "png"
|
||||
{
|
||||
return En::ImageFile;
|
||||
}
|
||||
}
|
||||
@@ -582,139 +561,51 @@ impl CommitManager {
|
||||
return En::Other;
|
||||
}
|
||||
|
||||
fn o_to_string(&self, val: &std::ffi::OsString) -> Result<std::string::String> {
|
||||
let res = val.clone().into_string();
|
||||
return match res {
|
||||
Ok(sss) => Ok(sss),
|
||||
Err(_) => Ok(String::from("Error")),
|
||||
};
|
||||
fn get_cover_art_path(&self, directory_path: &String) -> Result<String> {
|
||||
for entry in read_dir(std::path::Path::new(directory_path))? {
|
||||
let entry = entry?;
|
||||
|
||||
let file_type = entry.file_type();
|
||||
let file_name = entry.file_name();
|
||||
|
||||
println!("file type: {:?}", file_type);
|
||||
println!("file name: {:?}", file_name);
|
||||
|
||||
match self.find_file_extension(&file_name) {
|
||||
En::ImageFile => {
|
||||
let directory_part = directory_path.clone();
|
||||
let fname = utilities::string::o_to_string(&file_name);
|
||||
let fullpath = directory_part + "/" + &fname.unwrap();
|
||||
return Ok(fullpath);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
// Standards
|
||||
// * track01.cdda.wav - Disc 1, Track 1
|
||||
// * track02d02.cdda.wav - Disc 2, Track 2
|
||||
fn initialize_disc_and_track(&mut self, song: &mut models::song::Song) {
|
||||
let mut disc = 1;
|
||||
let mut track = 1;
|
||||
let mut mode = 0;
|
||||
let filename =
|
||||
&<std::option::Option<std::string::String> as Clone>::clone(&song.filepath).unwrap();
|
||||
fn get_metadata_path(&self, directory_path: &String) -> Result<String> {
|
||||
for entry in read_dir(std::path::Path::new(directory_path))? {
|
||||
let entry = entry?;
|
||||
|
||||
let trd = filename.contains("trackd");
|
||||
let tr = filename.contains("track");
|
||||
let file_type = entry.file_type();
|
||||
let file_name = entry.file_name();
|
||||
|
||||
if tr {
|
||||
mode = 1;
|
||||
}
|
||||
println!("file type: {:?}", file_type);
|
||||
println!("file name: {:?}", file_name);
|
||||
|
||||
if trd {
|
||||
mode = 2;
|
||||
}
|
||||
|
||||
let dl = |a: &char, b: &char| -> bool {
|
||||
return a == b;
|
||||
};
|
||||
let d = utilities::checks::Checks::index_of_item_in_container(&filename, &'d', dl);
|
||||
let k = utilities::checks::Checks::index_of_item_in_container(&filename, &'k', dl);
|
||||
let dot = utilities::checks::Checks::index_of_item_in_container(&filename, &'.', dl);
|
||||
let end = filename.len() as i32;
|
||||
|
||||
match mode {
|
||||
1 => {
|
||||
if k != end && dot != end {
|
||||
let st = k + 1;
|
||||
let ed = dot - 1;
|
||||
let mut t: String = String::new();
|
||||
let mut index = 0;
|
||||
for a in filename.chars() {
|
||||
if index >= st && index <= ed {
|
||||
t.push(a);
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if utilities::checks::Checks::is_numeric(&t) {
|
||||
track = t.parse::<i32>().unwrap();
|
||||
}
|
||||
disc = 1
|
||||
match self.find_file_extension(&file_name) {
|
||||
En::MetadataFile => {
|
||||
let directory_part = directory_path.clone();
|
||||
let fname = utilities::string::o_to_string(&file_name);
|
||||
return Ok(directory_part + "/" + &fname.unwrap());
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
if k != end && dot != end && d != end {
|
||||
let st = k + 1;
|
||||
let ed = dot - 1;
|
||||
let mut t: String = String::new();
|
||||
let mut index = 0;
|
||||
for a in filename.chars() {
|
||||
if index <= ed {
|
||||
t.push(a);
|
||||
} else if index >= st {
|
||||
t.push(a);
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if utilities::checks::Checks::is_numeric(&t) {
|
||||
track = t.parse::<i32>().unwrap();
|
||||
}
|
||||
|
||||
let sst = d + 1;
|
||||
let eed = dot;
|
||||
let mut d_s = String::new();
|
||||
index = 0;
|
||||
for a in filename.chars() {
|
||||
if index <= eed {
|
||||
d_s.push(a);
|
||||
} else if index >= sst {
|
||||
d_s.push(a);
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if utilities::checks::Checks::is_numeric(&d_s) {
|
||||
track = d_s.parse::<i32>().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => println!(""),
|
||||
}
|
||||
|
||||
song.disc = Some(disc);
|
||||
song.track = Some(track);
|
||||
}
|
||||
|
||||
fn _parse_disc_and_track(&self, song: &mut models::song::Song, track_id: &String) {
|
||||
let sep = |_a: &char, _b: &char| -> bool {
|
||||
return false;
|
||||
};
|
||||
|
||||
let index = utilities::checks::Checks::index_of_item_in_container(track_id, &':', sep);
|
||||
|
||||
if index == -1 {
|
||||
let mut d_str: String = String::new();
|
||||
let t_str = String::new();
|
||||
|
||||
for c in track_id.chars().skip(0).take(index as usize) {
|
||||
d_str.push(c);
|
||||
}
|
||||
|
||||
let start = index + 1;
|
||||
let end = track_id.len() - 1;
|
||||
|
||||
for c in track_id.chars().skip(start as usize).take(end as usize) {
|
||||
d_str.push(c);
|
||||
}
|
||||
|
||||
song.disc = Some(d_str.parse::<i32>().unwrap());
|
||||
song.track = Some(t_str.parse::<i32>().unwrap());
|
||||
} else {
|
||||
if utilities::checks::Checks::is_numeric(track_id) {
|
||||
song.track = Some(track_id.parse::<i32>().unwrap());
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
fn _check_for_no_confirm(&self) -> bool {
|
||||
@@ -725,21 +616,4 @@ impl CommitManager {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn retrieve_metadata(&self, path: &String) -> Album {
|
||||
let content = self.retrieve_file_content(&path);
|
||||
let val = content.unwrap();
|
||||
|
||||
let converted = serde_json::from_str(&val);
|
||||
|
||||
match &converted {
|
||||
Ok(_) => println!("Good!"),
|
||||
Err(er) => println!("Error {:?}", er),
|
||||
}
|
||||
return converted.unwrap();
|
||||
}
|
||||
|
||||
fn retrieve_file_content(&self, path: &String) -> Result<String> {
|
||||
return std::fs::read_to_string(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
use std::default::Default;
|
||||
|
||||
|
||||
pub struct FileManager {
|
||||
pub filepath: String,
|
||||
pub filebuffer: String,
|
||||
pub file_read: bool,
|
||||
pub file_buffer_length: i32,
|
||||
}
|
||||
|
||||
impl Default for FileManager {
|
||||
fn default() -> Self {
|
||||
FileManager {
|
||||
filepath: String::new(),
|
||||
filebuffer: String::new(),
|
||||
file_read: false,
|
||||
file_buffer_length: -1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl FileManager {
|
||||
// TODO: Implement
|
||||
pub fn init(&mut self) {
|
||||
self.read_file()
|
||||
}
|
||||
|
||||
pub fn save_file(&mut self, filepath: &String) {
|
||||
if !self.file_read {
|
||||
self.read_file();
|
||||
}
|
||||
|
||||
let mut file = File::open(filepath)?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
self.file_buffer_length = buffer.len();
|
||||
self.filebuffer = String::from_utf8(buffer); // Assuming UTF-8 encoding
|
||||
}
|
||||
|
||||
pub fn modify_file_path(&mut self, file: &String) {
|
||||
self.filepath = file;
|
||||
}
|
||||
|
||||
pub fn retrieve_file_buffer(&self) -> String {
|
||||
self.filebuffer;
|
||||
}
|
||||
|
||||
pub fn retrieve_file_length(&self) -> i32 {
|
||||
self.file_buffer_length;
|
||||
}
|
||||
|
||||
fn read_file(&mut self) -> Vec<u8> {
|
||||
let mut file = File::open(self.filepath)?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
self.file_buffer_length = buffer.len();
|
||||
self.filebuffer = String::from_utf8(buffer); // Assuming UTF-8 encoding
|
||||
self.file_read = true;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ use std::default::Default;
|
||||
use crate::models;
|
||||
|
||||
pub struct TokenManager {
|
||||
pub user: models::user::User,
|
||||
pub user: icarus_models::user::User,
|
||||
pub api: models::api::API,
|
||||
}
|
||||
|
||||
impl Default for TokenManager {
|
||||
fn default() -> Self {
|
||||
let mut token = TokenManager {
|
||||
user: models::user::User::default(),
|
||||
user: icarus_models::user::User::default(),
|
||||
api: models::api::API::default(),
|
||||
};
|
||||
|
||||
@@ -21,14 +21,21 @@ impl Default for TokenManager {
|
||||
}
|
||||
|
||||
impl TokenManager {
|
||||
pub async fn request_token(&self) -> Result<models::token::Token, std::io::Error> {
|
||||
pub async fn request_token(&self) -> Result<icarus_models::token::AccessToken, std::io::Error> {
|
||||
println!("Sending request for a token");
|
||||
|
||||
let url = self.retrieve_url();
|
||||
|
||||
println!("URL: {}", url);
|
||||
|
||||
let mut token = models::token::Token::default();
|
||||
let mut token = icarus_models::token::AccessToken {
|
||||
user_id: -1,
|
||||
username: String::new(),
|
||||
token: String::new(),
|
||||
token_type: String::new(),
|
||||
expiration: -1,
|
||||
message: String::new(),
|
||||
};
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client.post(&url).json(&self.user).send().await.unwrap();
|
||||
@@ -36,7 +43,7 @@ impl TokenManager {
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
// on success, parse our JSON to an APIResponse
|
||||
let s = response.json::<models::token::Token>().await;
|
||||
let s = response.json::<icarus_models::token::AccessToken>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
@@ -66,7 +73,6 @@ impl TokenManager {
|
||||
let api = &self.api;
|
||||
let mut url = String::from(&api.url);
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -6,36 +6,42 @@ use crate::models::{self};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct UserManager {
|
||||
pub user: models::user::User,
|
||||
pub user: icarus_models::user::User,
|
||||
pub ica_action: models::icarus_action::IcarusAction,
|
||||
}
|
||||
|
||||
impl Default for UserManager {
|
||||
fn default() -> Self {
|
||||
UserManager {
|
||||
user: models::user::User::default(),
|
||||
user: icarus_models::user::User::default(),
|
||||
ica_action: models::icarus_action::IcarusAction::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserManager {
|
||||
pub fn retrieve_user(&self) -> models::user::User {
|
||||
pub fn retrieve_user(&self) -> icarus_models::user::User {
|
||||
return self.user.clone();
|
||||
}
|
||||
|
||||
pub fn parse_user_from_actions(&mut self) {
|
||||
let args = &self.ica_action.flags;
|
||||
|
||||
// Quit the loop when two are found
|
||||
let mut amt: i32 = 0;
|
||||
for arg in args {
|
||||
let flag = &arg.flag;
|
||||
|
||||
if flag == "-u" {
|
||||
self.user.username = String::from(&arg.value);
|
||||
amt += 1;
|
||||
} else if flag == "-p" {
|
||||
self.user.password = String::from(&arg.value);
|
||||
amt += 1;
|
||||
}
|
||||
|
||||
if flag == "-p" {
|
||||
self.user.password = String::from(&arg.value);
|
||||
if amt == 2 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
pub mod api;
|
||||
pub mod flags;
|
||||
pub mod icarus_action;
|
||||
pub mod song;
|
||||
pub mod token;
|
||||
pub mod upload_form;
|
||||
pub mod user;
|
||||
|
||||
@@ -1,171 +0,0 @@
|
||||
use std::default::Default;
|
||||
use std::io::Read;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::constants;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Song {
|
||||
#[serde(alias = "song_id")]
|
||||
pub id: Option<i32>,
|
||||
pub title: Option<String>,
|
||||
pub artist: Option<String>,
|
||||
pub album: Option<String>,
|
||||
pub genre: Option<String>,
|
||||
pub year: Option<i32>,
|
||||
pub duration: Option<f64>,
|
||||
pub track: Option<i32>,
|
||||
pub disc: Option<i32>,
|
||||
pub data: Option<Vec<u8>>,
|
||||
pub filepath: Option<String>,
|
||||
pub directory: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Album {
|
||||
#[serde(alias = "album")]
|
||||
pub title: String,
|
||||
pub album_artist: String,
|
||||
pub genre: String,
|
||||
pub year: i32,
|
||||
pub track_count: i32,
|
||||
pub disc_count: i32,
|
||||
#[serde(alias = "tracks")]
|
||||
pub songs: Vec<Song>,
|
||||
}
|
||||
|
||||
impl Default for Album {
|
||||
fn default() -> Self {
|
||||
Album {
|
||||
title: String::new(),
|
||||
album_artist: String::new(),
|
||||
genre: String::new(),
|
||||
year: 0,
|
||||
track_count: 0,
|
||||
disc_count: 0,
|
||||
songs: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Song {
|
||||
fn default() -> Self {
|
||||
Song {
|
||||
id: None,
|
||||
title: None,
|
||||
artist: None,
|
||||
album: None,
|
||||
genre: None,
|
||||
year: None,
|
||||
duration: None,
|
||||
track: None,
|
||||
disc: None,
|
||||
data: None,
|
||||
filepath: None,
|
||||
directory: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Song {
|
||||
pub fn print_info(&self) {
|
||||
println!("Title: {:?}", self.title);
|
||||
println!("Artist: {:?}", self.artist);
|
||||
}
|
||||
|
||||
pub fn song_path(&self) -> String {
|
||||
let directory =
|
||||
&<std::option::Option<std::string::String> as Clone>::clone(&self.directory).unwrap();
|
||||
|
||||
let mut buffer: String = directory.to_string();
|
||||
let count = buffer.len();
|
||||
|
||||
if buffer.chars().nth(count - 1) != Some('/') {
|
||||
buffer += "/";
|
||||
}
|
||||
|
||||
let filename =
|
||||
&<std::option::Option<std::string::String> as Clone>::clone(&self.filepath).unwrap();
|
||||
buffer += filename;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> {
|
||||
let path = self.song_path();
|
||||
println!("Converting song to data");
|
||||
println!("Path: {:?}", path);
|
||||
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
if buffer.len() == 0 {
|
||||
println!("Why is it empty?");
|
||||
}
|
||||
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
// if 1 - wav, if 0 - mp3, anything else defaults to wav
|
||||
pub fn _generate_filename_from_track(&mut self, i_type: i32) -> i32 {
|
||||
let mut filename: String = String::new();
|
||||
if self.track.unwrap() < 10 {
|
||||
filename += "0";
|
||||
}
|
||||
|
||||
filename += &self.track.unwrap().to_string();
|
||||
|
||||
if i_type == 0 {
|
||||
filename += constants::file_extensions::_MP3_FILE_EXTENSION;
|
||||
} else {
|
||||
filename += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||
}
|
||||
|
||||
self.filepath = Some(filename);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
pub fn _to_metadata_json(&self) -> Result<String, serde_json::Error> {
|
||||
return serde_json::to_string_pretty(&self);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct CoverArt {
|
||||
pub id: Option<i32>,
|
||||
pub title: Option<String>,
|
||||
pub path: Option<String>,
|
||||
pub data: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl Default for CoverArt {
|
||||
fn default() -> Self {
|
||||
CoverArt {
|
||||
id: None,
|
||||
title: None,
|
||||
path: None,
|
||||
data: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CoverArt {
|
||||
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> {
|
||||
let mut path: String = String::new();
|
||||
match &self.path {
|
||||
Some(val) => {
|
||||
path = String::from(val);
|
||||
}
|
||||
None => {
|
||||
();
|
||||
}
|
||||
}
|
||||
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
use std::default::Default;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Token {
|
||||
#[serde(alias = "user_id")]
|
||||
pub user_id: i32,
|
||||
#[serde(alias = "username")]
|
||||
pub username: Option<String>,
|
||||
#[serde(alias = "token")]
|
||||
pub access_token: Option<String>,
|
||||
#[serde(alias = "token_type")]
|
||||
pub token_type: Option<String>,
|
||||
#[serde(alias = "expiration")]
|
||||
pub expiration: Option<i32>,
|
||||
#[serde(alias = "message")]
|
||||
pub message: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Token {
|
||||
fn default() -> Self {
|
||||
Token {
|
||||
user_id: -1,
|
||||
username: None,
|
||||
access_token: None,
|
||||
token_type: None,
|
||||
expiration: None,
|
||||
message: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn bearer_token(&self) -> String {
|
||||
let mut token: String = String::from("Bearer ");
|
||||
|
||||
match &self.access_token {
|
||||
Some(tok) => {
|
||||
token += tok;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
use std::default::Default;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct User {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl Default for User {
|
||||
fn default() -> Self {
|
||||
User {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn _to_json(&self) -> Result<String, serde_json::Error> {
|
||||
return serde_json::to_string_pretty(&self);
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,9 @@ impl Default for Delete {
|
||||
impl Delete {
|
||||
pub async fn delete_song(
|
||||
&mut self,
|
||||
token: &models::token::Token,
|
||||
song: &models::song::Song,
|
||||
) -> Result<models::song::Song, std::io::Error> {
|
||||
token: &icarus_models::token::AccessToken,
|
||||
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 client = reqwest::Client::builder().build().unwrap();
|
||||
@@ -33,14 +33,13 @@ impl Delete {
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let mut sng = models::song::Song::default();
|
||||
let mut sng = icarus_models::song::Song::default();
|
||||
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
println!("Success!");
|
||||
let s = response.json::<models::song::Song>().await;
|
||||
let s = response.json::<icarus_models::song::Song>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
sng = parsed;
|
||||
}
|
||||
@@ -57,7 +56,7 @@ impl Delete {
|
||||
return Ok(sng);
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||
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/");
|
||||
@@ -65,7 +64,7 @@ impl Delete {
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.unwrap().to_string();
|
||||
url += &song.id.to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
+26
-22
@@ -16,15 +16,15 @@ impl Default for Download {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MyError {
|
||||
_Request(reqwest::Error),
|
||||
Request(reqwest::Error),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl Download {
|
||||
pub async fn download_song(
|
||||
&mut self,
|
||||
token: &models::token::Token,
|
||||
song: &models::song::Song,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<String, MyError> {
|
||||
self.api.endpoint = String::from("song/data/download");
|
||||
let url = self.retrieve_url(&song);
|
||||
@@ -41,33 +41,37 @@ impl Download {
|
||||
.get(&url)
|
||||
.header(reqwest::header::AUTHORIZATION, &access_token)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
.await;
|
||||
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
let data = response.text();
|
||||
match data.await {
|
||||
Ok(e) => {
|
||||
return Ok(e);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
match response {
|
||||
Ok(rep) => match rep.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
let data = rep.text();
|
||||
match data.await {
|
||||
Ok(e) => {
|
||||
return Ok(e);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
}
|
||||
},
|
||||
Err(er) => {
|
||||
return Err(MyError::Request(er));
|
||||
}
|
||||
}
|
||||
|
||||
return Err(MyError::Other(String::from("Error downloading")));
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||
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/");
|
||||
@@ -75,7 +79,7 @@ impl Download {
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.unwrap().to_string();
|
||||
url += &song.id.to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ impl Default for RetrieveRecords {
|
||||
impl RetrieveRecords {
|
||||
pub async fn get_all_songs(
|
||||
&mut self,
|
||||
token: &models::token::Token,
|
||||
) -> Result<Vec<models::song::Song>, Error> {
|
||||
token: &icarus_models::token::AccessToken,
|
||||
) -> Result<Vec<icarus_models::song::Song>, Error> {
|
||||
self.api.endpoint = String::from("song");
|
||||
let mut songs: Vec<models::song::Song> = Vec::new();
|
||||
let mut songs: Vec<icarus_models::song::Song> = Vec::new();
|
||||
let url = self.retrieve_url();
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
@@ -46,7 +46,7 @@ impl RetrieveRecords {
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
// on success, parse our JSON to an APIResponse
|
||||
let s = response.json::<Vec<models::song::Song>>().await;
|
||||
let s = response.json::<Vec<icarus_models::song::Song>>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
|
||||
+32
-100
@@ -1,41 +1,14 @@
|
||||
use std::default::Default;
|
||||
|
||||
use http::HeaderMap;
|
||||
use http::HeaderValue;
|
||||
use reqwest;
|
||||
use reqwest::multipart::Form;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::constants;
|
||||
use crate::models;
|
||||
|
||||
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 {
|
||||
@@ -47,15 +20,12 @@ impl Default for Upload {
|
||||
impl Upload {
|
||||
pub async fn upload_song_with_metadata(
|
||||
&mut self,
|
||||
token: &models::token::Token,
|
||||
song: &models::song::Song,
|
||||
cover: &models::song::CoverArt,
|
||||
album: &models::song::Album,
|
||||
) -> Result<reqwest::Response, std::io::Error> {
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &icarus_models::song::Song,
|
||||
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 mut new_song = self.initialize_song(&song, &album);
|
||||
new_song.songpath = song.song_path();
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
if url.is_empty() {
|
||||
@@ -64,6 +34,7 @@ impl Upload {
|
||||
|
||||
println!("Url: {}", url);
|
||||
println!("Token: {}", access_token);
|
||||
println!("Path: {:?}", song.song_path());
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
@@ -72,65 +43,46 @@ 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();
|
||||
let response = client
|
||||
match client
|
||||
.post(url)
|
||||
.headers(headers)
|
||||
.multipart(form)
|
||||
.send()
|
||||
.await;
|
||||
let response_text = response.unwrap();
|
||||
|
||||
println!("Something was sent");
|
||||
println!("{:?}", response_text);
|
||||
|
||||
return Ok(response_text);
|
||||
.await
|
||||
{
|
||||
Ok(r) => {
|
||||
return Ok(r);
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn _initialize_form(
|
||||
fn init_form(
|
||||
&self,
|
||||
song_raw_data: Vec<u8>,
|
||||
cover_raw_data: Vec<u8>,
|
||||
song_detail: String,
|
||||
) -> 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 += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||
let mut cover_filename = String::from("cover");
|
||||
cover_filename += constants::file_extensions::JPG_FILE_EXTENSION;
|
||||
|
||||
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, cover: &models::song::CoverArt) -> reqwest::multipart::Form {
|
||||
let songpath = song.songpath.clone();
|
||||
let coverpath = cover.path.clone().unwrap();
|
||||
let song_detail = song.to_metadata_json().unwrap();
|
||||
song: &icarus_models::song::Song,
|
||||
cover: &icarus_models::coverart::CoverArt,
|
||||
) -> reqwest::multipart::Form {
|
||||
let songpath = match song.song_path() {
|
||||
Ok(s) => s,
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
let coverpath = cover.path.clone();
|
||||
println!("Cover path: {:?}", coverpath);
|
||||
let song_detail = match song.to_metadata_json(true) {
|
||||
Ok(s) => s,
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
println!("\n{}\n", song_detail);
|
||||
|
||||
let mut song_filename = String::from("audio");
|
||||
song_filename += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||
song_filename += icarus_models::constants::DEFAULTMUSICEXTENSION;
|
||||
let mut cover_filename = String::from("cover");
|
||||
cover_filename += constants::file_extensions::JPG_FILE_EXTENSION;
|
||||
cover_filename += icarus_models::constants::JPGEXTENSION;
|
||||
|
||||
let form = reqwest::multipart::Form::new()
|
||||
.part(
|
||||
@@ -172,24 +124,4 @@ impl Upload {
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
fn initialize_song(&self, song: &models::song::Song, album: &models::song::Album) -> Song {
|
||||
let dur = song.duration.clone().unwrap();
|
||||
println!("Duration: {}", dur);
|
||||
|
||||
return Song {
|
||||
title: String::from(&song.title.clone().unwrap()),
|
||||
album: album.title.clone(),
|
||||
artist: String::from(&song.artist.clone().unwrap().clone()),
|
||||
album_artist: album.album_artist.clone(),
|
||||
year: album.year.clone(),
|
||||
genre: album.genre.clone(),
|
||||
duration: f64::round(dur) as i32,
|
||||
track: (song.track.clone().unwrap()),
|
||||
track_count: album.track_count.clone(),
|
||||
disc: song.disc.clone().unwrap(),
|
||||
disc_count: album.disc_count.clone(),
|
||||
songpath: String::new(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ use serde::{Deserialize, Serialize};
|
||||
pub struct Checks {}
|
||||
|
||||
impl Checks {
|
||||
pub fn is_numeric(text: &String) -> bool {
|
||||
pub fn _is_numeric(text: &String) -> bool {
|
||||
text.parse::<f64>().is_ok()
|
||||
}
|
||||
|
||||
pub fn index_of_item_in_container<F>(container: &String, item: &char, func: F) -> i32
|
||||
pub fn _index_of_item_in_container<F>(container: &String, item: &char, func: F) -> i32
|
||||
where
|
||||
F: Fn(&char, &char) -> bool,
|
||||
{
|
||||
@@ -26,3 +26,7 @@ impl Checks {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exit_program(code: i32) {
|
||||
std::process::exit(code);
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod checks;
|
||||
pub mod string;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
pub fn o_to_string(val: &std::ffi::OsString) -> Result<std::string::String, std::io::Error> {
|
||||
match val.clone().into_string() {
|
||||
Ok(value) => Ok(value),
|
||||
Err(_) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
String::from("Error"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"album": "Sample Tracks 2",
|
||||
"album_artist": "KD",
|
||||
"genre": "Country",
|
||||
"year": 2025,
|
||||
"track_count": 3,
|
||||
"disc_count": 1,
|
||||
"tracks": [
|
||||
{
|
||||
"title": "Uh huh",
|
||||
"artist": "KD",
|
||||
"disc": 1,
|
||||
"track": 1,
|
||||
"duration": 32
|
||||
},
|
||||
{
|
||||
"title": "Bold and brash",
|
||||
"artist": "KD",
|
||||
"disc": 1,
|
||||
"track": 2,
|
||||
"duration": 37
|
||||
},
|
||||
{
|
||||
"title": "How does this work?",
|
||||
"artist": "KD",
|
||||
"disc": 1,
|
||||
"track": 3,
|
||||
"duration": 46
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 75 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user