Added single target upload
This commit is contained in:
@@ -4,13 +4,14 @@ use std::fmt::Display;
|
|||||||
use std::fs::{read_dir, DirEntry};
|
use std::fs::{read_dir, DirEntry};
|
||||||
use std::io::{Error, Read, Result};
|
use std::io::{Error, Read, Result};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use futures::{FutureExt, TryFutureExt};
|
use futures::{FutureExt, TryFutureExt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
|
|
||||||
use crate::models;
|
|
||||||
use crate::models::song::Album;
|
use crate::models::song::Album;
|
||||||
|
use crate::models::{self, song};
|
||||||
use crate::parsers;
|
use crate::parsers;
|
||||||
use crate::syncers;
|
use crate::syncers;
|
||||||
use crate::utilities;
|
use crate::utilities;
|
||||||
@@ -52,6 +53,27 @@ impl Album {
|
|||||||
println!("Track Count: {}", self.track_count);
|
println!("Track Count: {}", self.track_count);
|
||||||
println!("Disc Count: {}\n", self.disc_count);
|
println!("Disc Count: {}\n", self.disc_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommitManager {
|
impl CommitManager {
|
||||||
@@ -195,13 +217,14 @@ impl CommitManager {
|
|||||||
|
|
||||||
if single_target && multitarget {
|
if single_target && multitarget {
|
||||||
println!("Cannot upload from source and directory");
|
println!("Cannot upload from source and directory");
|
||||||
|
panic!("What??");
|
||||||
}
|
}
|
||||||
|
|
||||||
if single_target {
|
if single_target {
|
||||||
println!("Song path: {}", songpath);
|
println!("Song path: {}", songpath);
|
||||||
println!("Track ID: {}", track_id);
|
println!("Track ID: {}", track_id);
|
||||||
println!("metadata path: {}", coverpath);
|
println!("metadata path: {}", metadata_path);
|
||||||
println!("cover art path: {}", track_id);
|
println!("cover art path: {}", coverpath);
|
||||||
|
|
||||||
self.sing_target_upload(&songpath, &track_id, &metadata_path, &coverpath);
|
self.sing_target_upload(&songpath, &track_id, &metadata_path, &coverpath);
|
||||||
} else if multitarget {
|
} else if multitarget {
|
||||||
@@ -213,12 +236,112 @@ impl CommitManager {
|
|||||||
|
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
fn sing_target_upload(
|
fn sing_target_upload(
|
||||||
&self,
|
&mut self,
|
||||||
songpath: &String,
|
songpath: &String,
|
||||||
track_id: &String,
|
track_id: &String,
|
||||||
meta_path: &String,
|
meta_path: &String,
|
||||||
cover_path: &String,
|
cover_path: &String,
|
||||||
) {
|
) -> Result<()> {
|
||||||
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
|
api: models::api::API::default(),
|
||||||
|
ica_act: self.ica_action.clone(),
|
||||||
|
};
|
||||||
|
prsr.parse_api();
|
||||||
|
|
||||||
|
let api = prsr.retrieve_api();
|
||||||
|
let token = self.parse_token(&api);
|
||||||
|
|
||||||
|
let song_file = std::path::Path::new(&songpath);
|
||||||
|
|
||||||
|
if !song_file.exists() {
|
||||||
|
println!("Song file does not exist");
|
||||||
|
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();
|
||||||
|
|
||||||
|
// 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::SongFile => {
|
||||||
|
// let mut song = models::song::Song::default();
|
||||||
|
let fname = self.o_to_string(&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);
|
||||||
|
}
|
||||||
|
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 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
||||||
@@ -254,7 +377,6 @@ impl CommitManager {
|
|||||||
println!("file type: {:?}", file_type);
|
println!("file type: {:?}", file_type);
|
||||||
println!("file name: {:?}", file_name);
|
println!("file name: {:?}", file_name);
|
||||||
|
|
||||||
|
|
||||||
match self.find_file_extension(&file_name) {
|
match self.find_file_extension(&file_name) {
|
||||||
En::ImageFile => {
|
En::ImageFile => {
|
||||||
let directory_part = sourcepath.clone();
|
let directory_part = sourcepath.clone();
|
||||||
@@ -276,6 +398,7 @@ impl CommitManager {
|
|||||||
filenames.push(s.clone());
|
filenames.push(s.clone());
|
||||||
song.filepath = Some(s.clone());
|
song.filepath = Some(s.clone());
|
||||||
song.directory = Some(sourcepath.clone());
|
song.directory = Some(sourcepath.clone());
|
||||||
|
song.data = Some(song.to_data().unwrap());
|
||||||
self.initialize_disc_and_track(&mut song);
|
self.initialize_disc_and_track(&mut song);
|
||||||
}
|
}
|
||||||
Err(er) => println!("Error: {:?}", er),
|
Err(er) => println!("Error: {:?}", er),
|
||||||
@@ -293,11 +416,12 @@ impl CommitManager {
|
|||||||
|
|
||||||
self.song_parsing(&mut album, &sourcepath, &filenames);
|
self.song_parsing(&mut album, &sourcepath, &filenames);
|
||||||
|
|
||||||
|
|
||||||
let mut up = syncers::upload::Upload::default();
|
let mut up = syncers::upload::Upload::default();
|
||||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||||
up.set_api(&host);
|
up.set_api(&host);
|
||||||
|
|
||||||
|
cover_art.data = Some(cover_art.to_data().unwrap());
|
||||||
|
|
||||||
println!("");
|
println!("");
|
||||||
|
|
||||||
for song in &album.songs {
|
for song in &album.songs {
|
||||||
@@ -309,12 +433,12 @@ impl CommitManager {
|
|||||||
match &tken {
|
match &tken {
|
||||||
Ok(o) => {
|
Ok(o) => {
|
||||||
println!("Successfully sent {:?}", o);
|
println!("Successfully sent {:?}", o);
|
||||||
},
|
}
|
||||||
Err(er) => {
|
Err(er) => {
|
||||||
println!("Some error {:?}", er);
|
println!("Some error {:?}", er);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("");
|
println!("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -14,7 +14,7 @@ pub struct Song {
|
|||||||
pub duration: Option<f64>,
|
pub duration: Option<f64>,
|
||||||
pub track: Option<i32>,
|
pub track: Option<i32>,
|
||||||
pub disc: Option<i32>,
|
pub disc: Option<i32>,
|
||||||
pub data: Option<String>,
|
pub data: Option<Vec<u8>>,
|
||||||
// use filepath instead
|
// use filepath instead
|
||||||
// pub song_path: String,
|
// pub song_path: String,
|
||||||
pub filepath: Option<String>,
|
pub filepath: Option<String>,
|
||||||
@@ -135,6 +135,7 @@ pub struct CoverArt {
|
|||||||
pub id: Option<i32>,
|
pub id: Option<i32>,
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
pub path: Option<String>,
|
pub path: Option<String>,
|
||||||
|
pub data: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CoverArt {
|
impl Default for CoverArt {
|
||||||
@@ -143,6 +144,7 @@ impl Default for CoverArt {
|
|||||||
id: None,
|
id: None,
|
||||||
title: None,
|
title: None,
|
||||||
path: None,
|
path: None,
|
||||||
|
data: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-14
@@ -79,17 +79,19 @@ impl Upload {
|
|||||||
) -> Result<reqwest::Response, std::io::Error> {
|
) -> Result<reqwest::Response, std::io::Error> {
|
||||||
self.api.endpoint = String::from("song/data/upload/with/data");
|
self.api.endpoint = String::from("song/data/upload/with/data");
|
||||||
let url = self.retrieve_url();
|
let url = self.retrieve_url();
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let new_song = self.initialize_song(&song, &album);
|
let new_song = self.initialize_song(&song, &album);
|
||||||
let access_token = token.bearer_token();
|
let access_token = token.bearer_token();
|
||||||
|
|
||||||
let song_data = song.to_data();
|
let song_data = song.data.clone().unwrap();
|
||||||
let cover_data = cover.to_data();
|
let cover_data = cover.data.clone().unwrap();
|
||||||
let song_detail = new_song.to_metadata_json().unwrap();
|
let song_detail = new_song.to_metadata_json().unwrap();
|
||||||
|
|
||||||
let mut song_raw_data: Vec<u8> = Vec::new();
|
let song_raw_data_as = (async { song_data.clone() });
|
||||||
let mut cover_raw_data: Vec<u8> = Vec::new();
|
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;
|
||||||
|
|
||||||
|
/*
|
||||||
match song_data {
|
match song_data {
|
||||||
Ok(sd) => {
|
Ok(sd) => {
|
||||||
println!("song converted to data. Length {:?}", sd.len());
|
println!("song converted to data. Length {:?}", sd.len());
|
||||||
@@ -110,6 +112,7 @@ impl Upload {
|
|||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if url.is_empty() {
|
if url.is_empty() {
|
||||||
println!("Url is empty");
|
println!("Url is empty");
|
||||||
@@ -119,14 +122,25 @@ impl Upload {
|
|||||||
println!("Length: {:?}", song_raw_data.len());
|
println!("Length: {:?}", song_raw_data.len());
|
||||||
println!("Token: {}", access_token);
|
println!("Token: {}", access_token);
|
||||||
|
|
||||||
let form = self.initialize_form(song_raw_data, cover_raw_data, song_detail.clone());
|
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;
|
||||||
|
|
||||||
println!("Form: {:?}", form);
|
println!("Form: {:?}", form);
|
||||||
|
|
||||||
let response = client
|
// let mut client = reqwest::Client::new();
|
||||||
|
let response = reqwest::Client::new()
|
||||||
.post(url)
|
.post(url)
|
||||||
.multipart(form)
|
.multipart(form)
|
||||||
.header(reqwest::header::AUTHORIZATION, access_token)
|
.header(reqwest::header::AUTHORIZATION, access_token)
|
||||||
|
.header(reqwest::header::CONTENT_TYPE, "multipart/form-data")
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
let response_text = response.unwrap();
|
let response_text = response.unwrap();
|
||||||
@@ -137,29 +151,31 @@ impl Upload {
|
|||||||
return Ok(response_text);
|
return Ok(response_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initialize_form(&self, song_raw_data: Vec<u8>, cover_raw_data: Vec<u8>, song_detail: String) -> Form {
|
fn initialize_form(
|
||||||
|
&self,
|
||||||
|
song_raw_data: Vec<u8>,
|
||||||
|
cover_raw_data: Vec<u8>,
|
||||||
|
song_detail: String,
|
||||||
|
) -> Form {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.insert(
|
headers.insert(
|
||||||
http::header::CONTENT_TYPE,
|
http::header::CONTENT_TYPE,
|
||||||
http::HeaderValue::from_static("application/octet-stream"),
|
http::HeaderValue::from_static("application/octet-stream"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let file = reqwest::multipart::Part::bytes(song_raw_data).headers(headers);
|
let mut file = reqwest::multipart::Part::bytes(song_raw_data).headers(headers);
|
||||||
let mut headers_i = HeaderMap::new();
|
let mut headers_i = HeaderMap::new();
|
||||||
headers_i.insert(
|
headers_i.insert(
|
||||||
http::header::CONTENT_TYPE,
|
http::header::CONTENT_TYPE,
|
||||||
http::HeaderValue::from_static("image/jpeg"),
|
http::HeaderValue::from_static("image/jpeg"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let cover = reqwest::multipart::Part::bytes(cover_raw_data).headers(headers_i);
|
let mut cover = reqwest::multipart::Part::bytes(cover_raw_data).headers(headers_i);
|
||||||
|
|
||||||
let mut form = Form::new();
|
return reqwest::multipart::Form::new()
|
||||||
form = form
|
|
||||||
.part("cover", cover.file_name("cover.jpeg"))
|
.part("cover", cover.file_name("cover.jpeg"))
|
||||||
.text("metadata", song_detail)
|
.text("metadata", song_detail)
|
||||||
.part("file", file.file_name("audio.wav"));
|
.part("file", file.file_name("audio.wav"));
|
||||||
|
|
||||||
return form;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_api(&mut self, host: &String) {
|
pub fn set_api(&mut self, host: &String) {
|
||||||
|
|||||||
Reference in New Issue
Block a user