Uploading multi target works, but is buggy
Not all of the songs are being uploaded
This commit is contained in:
@@ -171,7 +171,6 @@ impl CommitManager {
|
||||
};
|
||||
tok_mgr.init();
|
||||
|
||||
// let token = tok_mgr.request_token();
|
||||
let token = Runtime::new().unwrap().block_on(tok_mgr.request_token());
|
||||
|
||||
return token.unwrap();
|
||||
@@ -245,7 +244,6 @@ impl CommitManager {
|
||||
|
||||
// iterate files in metadatapath
|
||||
let path = std::path::Path::new(directory_path);
|
||||
// let entries = std::fs::read_dir(path);
|
||||
|
||||
for entry in read_dir(path)? {
|
||||
let entry = entry?;
|
||||
@@ -256,9 +254,6 @@ impl CommitManager {
|
||||
println!("file type: {:?}", file_type);
|
||||
println!("file name: {:?}", file_name);
|
||||
|
||||
// TODO: Write code to determine file extension by
|
||||
// the file path. The result will determine what
|
||||
// behavior will happen
|
||||
|
||||
match self.find_file_extension(&file_name) {
|
||||
En::ImageFile => {
|
||||
@@ -292,12 +287,12 @@ impl CommitManager {
|
||||
}
|
||||
}
|
||||
|
||||
filenames.sort();
|
||||
|
||||
let mut album = self.retrieve_metadata(&metadatapath);
|
||||
|
||||
self.song_parsing(&mut album, &sourcepath, &filenames);
|
||||
|
||||
// songs.clear();
|
||||
// songs = album.songs;
|
||||
|
||||
let mut up = syncers::upload::Upload::default();
|
||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||
@@ -307,13 +302,20 @@ impl CommitManager {
|
||||
|
||||
for song in &album.songs {
|
||||
// Upload each song
|
||||
// TODO: Add functions to Upload struct that uploads song
|
||||
// with metadata and img
|
||||
println!("Sending song...");
|
||||
let res = up.upload_song_with_metadata(&token, &song, &cover_art, &album);
|
||||
let tken = Runtime::new().unwrap().block_on(res);
|
||||
// let data = futures::executor::block_on(res);
|
||||
println!("{:?}", tken);
|
||||
|
||||
match &tken {
|
||||
Ok(o) => {
|
||||
println!("Successfully sent {:?}", o);
|
||||
},
|
||||
Err(er) => {
|
||||
println!("Some error {:?}", er);
|
||||
}
|
||||
}
|
||||
|
||||
println!("");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -328,8 +330,7 @@ impl CommitManager {
|
||||
) {
|
||||
// Apply directory
|
||||
for song in &mut album.songs {
|
||||
// let bor = song.as_mut();
|
||||
let mut dir = &song.directory;
|
||||
let dir = &song.directory;
|
||||
match dir {
|
||||
Some(s) => println!("{}", s),
|
||||
None => {
|
||||
@@ -376,7 +377,7 @@ impl CommitManager {
|
||||
match file_name_str {
|
||||
Some(string) => {
|
||||
let a = string.unwrap();
|
||||
let mut split = a.split(".");
|
||||
let split = a.split(".");
|
||||
let mut last_index = 0;
|
||||
|
||||
for _ in split.clone() {
|
||||
@@ -403,7 +404,6 @@ impl CommitManager {
|
||||
return En::ImageFile;
|
||||
}
|
||||
}
|
||||
// Err(err) => {
|
||||
_ => {
|
||||
return En::Other;
|
||||
}
|
||||
@@ -416,7 +416,7 @@ impl CommitManager {
|
||||
let res = val.clone().into_string();
|
||||
return match res {
|
||||
Ok(sss) => Ok(sss),
|
||||
Err(eee) => Ok(String::from("Error")),
|
||||
Err(_) => Ok(String::from("Error")),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -427,11 +427,9 @@ impl CommitManager {
|
||||
let mut disc = 1;
|
||||
let mut track = 1;
|
||||
let mut mode = 0;
|
||||
let songpath = song.song_path();
|
||||
let filename =
|
||||
&<std::option::Option<std::string::String> as Clone>::clone(&song.filepath).unwrap();
|
||||
|
||||
// let directory = &<std::option::Option<std::string::String> as Clone>::clone(&self.directory).unwrap();
|
||||
let trd = filename.contains("trackd");
|
||||
let tr = filename.contains("track");
|
||||
|
||||
@@ -559,27 +557,13 @@ impl CommitManager {
|
||||
}
|
||||
|
||||
fn retrieve_metadata(&self, path: &String) -> Album {
|
||||
/*
|
||||
let mut alb = Album {
|
||||
title: String::from(""),
|
||||
album_artist: String::from(""),
|
||||
genre: String::from(""),
|
||||
year: 0,
|
||||
track_count: 0,
|
||||
disc_count: 0,
|
||||
songs: Vec::new(),
|
||||
};
|
||||
*/
|
||||
|
||||
let content = self.retrieve_file_content(&path);
|
||||
// let alb = serde_json::from_str(&content.unwrap());
|
||||
let val = content.unwrap();
|
||||
|
||||
// return alb.unwrap();
|
||||
let converted = serde_json::from_str(&val);
|
||||
|
||||
match &converted {
|
||||
Ok(res) => println!("Good!"),
|
||||
Ok(_) => println!("Good!"),
|
||||
Err(er) => println!("Error {:?}", er),
|
||||
}
|
||||
return converted.unwrap();
|
||||
|
||||
@@ -70,6 +70,7 @@ impl Default for Song {
|
||||
impl Song {
|
||||
pub fn print_info(&self) {
|
||||
println!("Title: {:?}", self.title);
|
||||
println!("Artist: {:?}", self.artist);
|
||||
}
|
||||
|
||||
pub fn song_path(&self) -> String {
|
||||
|
||||
+32
-67
@@ -26,7 +26,7 @@ struct Song {
|
||||
album_artist: String,
|
||||
year: i32,
|
||||
genre: String,
|
||||
duration: f64,
|
||||
duration: i32,
|
||||
track: i32,
|
||||
track_count: i32,
|
||||
disc: i32,
|
||||
@@ -49,7 +49,7 @@ impl Default for Upload {
|
||||
|
||||
impl Upload {
|
||||
pub async fn upload_song(&self, token: &models::token::Token, song: &models::song::Song) {
|
||||
let url = self.retrieve_url(&song);
|
||||
let url = self.retrieve_url();
|
||||
let client = reqwest::Client::new();
|
||||
let access_token = token.bearer_token();
|
||||
let response = client
|
||||
@@ -78,7 +78,7 @@ impl Upload {
|
||||
album: &models::song::Album,
|
||||
) -> Result<reqwest::Response, std::io::Error> {
|
||||
self.api.endpoint = String::from("song/data/upload/with/data");
|
||||
let url = self.retrieve_url(&song);
|
||||
let url = self.retrieve_url();
|
||||
let client = reqwest::Client::new();
|
||||
let new_song = self.initialize_song(&song, &album);
|
||||
let access_token = token.bearer_token();
|
||||
@@ -116,53 +116,12 @@ impl Upload {
|
||||
}
|
||||
|
||||
println!("Url: {}", url);
|
||||
|
||||
println!("Length: {:?}", song_raw_data.len());
|
||||
println!("Token: {}", access_token);
|
||||
|
||||
// Adding text data
|
||||
// form = form.text("metadata", song_detail);
|
||||
let form = self.initialize_form(song_raw_data, cover_raw_data, song_detail.clone());
|
||||
|
||||
// Adding a file
|
||||
// let file_path = "path/to/your/file.txt";
|
||||
// let file = reqwest::blocking::multipart::Part::file(file_path);
|
||||
let mut headers = Vec::new();
|
||||
headers.push("application/octet-stream");
|
||||
headers.push("Content-type");
|
||||
|
||||
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.clone()).headers(headers);
|
||||
// headers.clear();
|
||||
let mut headers_i = HeaderMap::new();
|
||||
headers_i.insert(
|
||||
http::header::CONTENT_TYPE,
|
||||
http::HeaderValue::from_static("image/jpeg"),
|
||||
);
|
||||
let mut image_dis = String::new();
|
||||
image_dis = "form-data; name='cover'; filename='audio.wav'".to_owned();
|
||||
// headers_i.insert(http::header::CONTENT_DISPOSITION, HeaderValue::from_static("form-data; name=\"cover\"; filename=\"file.jpeg\""));
|
||||
|
||||
let cover = reqwest::multipart::Part::bytes(cover_raw_data.clone()).headers(headers_i);
|
||||
let song_filepath = "ss";
|
||||
let mut p = reqwest::multipart::Part::stream(song_raw_data.clone());
|
||||
// p.file_name("file");
|
||||
let mut q = reqwest::multipart::Part::stream(cover_raw_data.clone());
|
||||
// q.file_name("cover");
|
||||
// let ee = reqwest::multipart::Part::file_name(song_filepath);
|
||||
// let content_type = http::header::ContentType::from_mime_type("audio/mpeg").unwrap();
|
||||
|
||||
// let mut disposition = http::header::ContentDisposition::form_data_binary();
|
||||
// disposition.set_filename("my_file.mp3");
|
||||
|
||||
let mut form = Form::new();
|
||||
form = form
|
||||
.part("cover", cover.file_name("cover.jpeg"))
|
||||
.text("metadata", song_detail)
|
||||
.part("file", file.file_name("audio.wav"));
|
||||
println!("Form: {:?}", form);
|
||||
|
||||
let response = client
|
||||
.post(url)
|
||||
@@ -171,34 +130,36 @@ impl Upload {
|
||||
.send()
|
||||
.await;
|
||||
let response_text = response.unwrap();
|
||||
|
||||
println!("Something was sent");
|
||||
println!("{:?}", response_text);
|
||||
|
||||
return Ok(response_text);
|
||||
}
|
||||
|
||||
/*
|
||||
fn initialize_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 s_file = std::fs::File::open(song.song_path());
|
||||
let s_stream = tokio_util::codec::FramedRead::new(s_file, tokio_util::codec::BytesCodec::new());
|
||||
let s_file_body = Body::wrap_stream(s_stream);
|
||||
let s_some_file = multipart::Part::stream(s_file_body)
|
||||
.file_name("track.cdda.wav");
|
||||
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 s_data = Part::file(song.song_path());
|
||||
let c_data = multipart::Part::stream(&cover.path);
|
||||
let cover = reqwest::multipart::Part::bytes(cover_raw_data).headers(headers_i);
|
||||
|
||||
let mut form = Form::new();
|
||||
let meta = serde_json::to_string_pretty(&new_song);
|
||||
form = form.text("metadata", meta.unwrap());
|
||||
form = form
|
||||
.part("cover", cover.file_name("cover.jpeg"))
|
||||
.text("metadata", song_detail)
|
||||
.part("file", file.file_name("audio.wav"));
|
||||
|
||||
form = form.part("file", s_some_file)
|
||||
.part("cover", c_data);
|
||||
|
||||
let response = client.post(url).multipart(form).send();
|
||||
let result = response.text;
|
||||
|
||||
return Ok(result);
|
||||
*/
|
||||
return form;
|
||||
}
|
||||
|
||||
pub fn set_api(&mut self, host: &String) {
|
||||
@@ -207,7 +168,8 @@ impl Upload {
|
||||
api.version = String::from("v1");
|
||||
self.api = api;
|
||||
}
|
||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||
|
||||
fn retrieve_url(&self) -> String {
|
||||
let api = &self.api;
|
||||
let mut buffer = api.url.clone();
|
||||
let count = buffer.len();
|
||||
@@ -226,6 +188,9 @@ impl Upload {
|
||||
}
|
||||
|
||||
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(),
|
||||
@@ -233,7 +198,7 @@ impl Upload {
|
||||
album_artist: album.album_artist.clone(),
|
||||
year: album.year.clone(),
|
||||
genre: album.genre.clone(),
|
||||
duration: song.duration.clone().unwrap(),
|
||||
duration: f64::round(dur) as i32,
|
||||
track: (song.track.clone().unwrap()),
|
||||
track_count: album.track_count.clone(),
|
||||
disc: song.disc.clone().unwrap(),
|
||||
|
||||
Reference in New Issue
Block a user