Fixed multi target upload
This commit is contained in:
@@ -167,10 +167,9 @@ impl CommitManager {
|
||||
println!("Deleting song");
|
||||
}
|
||||
|
||||
// TODO: Implement
|
||||
// NOTE: Might not need to implement. I will see how this goes
|
||||
fn upload_song(&self) {
|
||||
println!("Deleting song");
|
||||
panic!("Not supported");
|
||||
}
|
||||
|
||||
fn parse_token(&self, api: &models::api::API) -> models::token::Token {
|
||||
@@ -198,7 +197,6 @@ impl CommitManager {
|
||||
return token.unwrap();
|
||||
}
|
||||
|
||||
// TODO: Implement
|
||||
fn upload_song_with_metadata(&mut self) {
|
||||
println!("Uplodaring song with metadara");
|
||||
|
||||
@@ -234,7 +232,6 @@ impl CommitManager {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Implement
|
||||
fn sing_target_upload(
|
||||
&mut self,
|
||||
songpath: &String,
|
||||
@@ -264,32 +261,16 @@ impl CommitManager {
|
||||
let mut fp = String::new();
|
||||
let mut dir = String::new();
|
||||
|
||||
// song_file.file_name();
|
||||
|
||||
// for entry in read_dir(song_file)? {
|
||||
// for entry in read_file(song_file)? {
|
||||
let entry = &song_file;
|
||||
|
||||
// let file_type = entry.file_type();
|
||||
let file_name = std::ffi::OsString::from(entry.file_name().unwrap());
|
||||
|
||||
// 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::ImageFile => {}
|
||||
En::MetadataFile => {}
|
||||
En::SongFile => {
|
||||
// let mut song = models::song::Song::default();
|
||||
let fname = self.o_to_string(&file_name);
|
||||
|
||||
match fname {
|
||||
@@ -303,17 +284,13 @@ impl CommitManager {
|
||||
}
|
||||
Err(er) => println!("Error: {:?}", er),
|
||||
}
|
||||
|
||||
// songs.push(song)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// }
|
||||
|
||||
cover_art.path = Some(cover_path.clone());
|
||||
|
||||
let mut album = self.retrieve_metadata(&meta_path);
|
||||
// self.song_parsing(&mut album, &song.directory.unwrap(), &filenames);
|
||||
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);
|
||||
@@ -343,7 +320,7 @@ impl CommitManager {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// TODO: Implement
|
||||
|
||||
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
||||
let mut prsr = parsers::api_parser::APIParser {
|
||||
api: models::api::API::default(),
|
||||
@@ -356,8 +333,7 @@ impl CommitManager {
|
||||
let directory_path = std::path::Path::new(&sourcepath);
|
||||
|
||||
if !directory_path.exists() {
|
||||
println!("Directory does not exist");
|
||||
std::process::exit(-1);
|
||||
panic!("Directory does not exist");
|
||||
}
|
||||
|
||||
let mut cover_art = models::song::CoverArt::default();
|
||||
@@ -424,6 +400,15 @@ impl CommitManager {
|
||||
|
||||
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...");
|
||||
|
||||
+34
-49
@@ -27,6 +27,8 @@ struct Song {
|
||||
track_count: i32,
|
||||
disc: i32,
|
||||
disc_count: i32,
|
||||
#[serde(skip_serializing)]
|
||||
songpath: String,
|
||||
}
|
||||
|
||||
impl Song {
|
||||
@@ -67,7 +69,6 @@ impl Upload {
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO: Implement
|
||||
pub async fn upload_song_with_metadata(
|
||||
&mut self,
|
||||
token: &models::token::Token,
|
||||
@@ -77,39 +78,17 @@ impl Upload {
|
||||
) -> Result<reqwest::Response, std::io::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 mut new_song = self.initialize_song(&song, &album);
|
||||
new_song.songpath = song.song_path();
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
let song_data = song.data.clone().unwrap();
|
||||
let cover_data = cover.data.clone().unwrap();
|
||||
let song_detail = new_song.to_metadata_json().unwrap();
|
||||
|
||||
let song_raw_data_as = (async { song_data.clone() });
|
||||
let cover_raw_data_as = async { cover_data.clone() };
|
||||
let song_raw_data = song_raw_data_as.await;
|
||||
let cover_raw_data: Vec<u8> = cover_raw_data_as.await;
|
||||
|
||||
if url.is_empty() {
|
||||
println!("Url is empty");
|
||||
}
|
||||
|
||||
println!("Url: {}", url);
|
||||
println!("Length: {:?}", song_raw_data.len());
|
||||
println!("Token: {}", access_token);
|
||||
|
||||
/*
|
||||
let form_as = async {
|
||||
self.initialize_form(
|
||||
song_raw_data.clone(),
|
||||
cover_raw_data.clone(),
|
||||
song_detail.clone(),
|
||||
)
|
||||
};
|
||||
*/
|
||||
|
||||
// let form = self.initialize_form(song_raw_data, cover_raw_data, song_detail.clone());
|
||||
// let form = form_as.await;
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
@@ -117,30 +96,7 @@ impl Upload {
|
||||
);
|
||||
headers.insert(reqwest::header::ACCEPT, HeaderValue::from_static("*/*"));
|
||||
|
||||
let songpath = song.song_path();
|
||||
let coverpath = cover.path.clone().unwrap();
|
||||
|
||||
println!("\n{}\n", song_detail);
|
||||
|
||||
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;
|
||||
|
||||
let form = reqwest::multipart::Form::new()
|
||||
.part(
|
||||
"file",
|
||||
reqwest::multipart::Part::bytes(std::fs::read(songpath)?).file_name(song_filename),
|
||||
)
|
||||
.part(
|
||||
"cover",
|
||||
reqwest::multipart::Part::bytes(std::fs::read(coverpath)?)
|
||||
.file_name(cover_filename),
|
||||
)
|
||||
.text("metadata", song_detail);
|
||||
|
||||
println!("Form: {:?}", form);
|
||||
|
||||
let form = self.init_form(&new_song, &cover);
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let response = client
|
||||
.post(url)
|
||||
@@ -188,6 +144,34 @@ impl Upload {
|
||||
.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();
|
||||
|
||||
println!("\n{}\n", song_detail);
|
||||
|
||||
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;
|
||||
|
||||
let form = reqwest::multipart::Form::new()
|
||||
.part(
|
||||
"file",
|
||||
reqwest::multipart::Part::bytes(std::fs::read(songpath).unwrap())
|
||||
.file_name(song_filename),
|
||||
)
|
||||
.part(
|
||||
"cover",
|
||||
reqwest::multipart::Part::bytes(std::fs::read(coverpath).unwrap())
|
||||
.file_name(cover_filename),
|
||||
)
|
||||
.text("metadata", song_detail);
|
||||
|
||||
return form;
|
||||
}
|
||||
|
||||
pub fn set_api(&mut self, host: &String) {
|
||||
let mut api = models::api::API::default();
|
||||
api.url = host.clone();
|
||||
@@ -229,6 +213,7 @@ impl Upload {
|
||||
track_count: album.track_count.clone(),
|
||||
disc: song.disc.clone().unwrap(),
|
||||
disc_count: album.disc_count.clone(),
|
||||
songpath: String::new(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user