Single target upload is functional but need to revisit multi-target upload
Multi-target upload is broken now. Needs to be revisited
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
pub const WAV_FILE_EXTENSION: &str = ".wav";
|
||||
pub const MP3_FILE_EXTENSION: &str = ".mp3";
|
||||
pub const JPG_FILE_EXTENSION: &str = ".jpg";
|
||||
@@ -0,0 +1 @@
|
||||
pub mod file_extensions;
|
||||
@@ -1,3 +1,4 @@
|
||||
mod constants;
|
||||
mod managers;
|
||||
mod models;
|
||||
mod parsers;
|
||||
|
||||
+8
-3
@@ -3,6 +3,10 @@ use std::io::Read;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::constants;
|
||||
|
||||
// use crate::constants;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Song {
|
||||
pub id: Option<i32>,
|
||||
@@ -95,7 +99,7 @@ impl Song {
|
||||
let path = self.song_path();
|
||||
println!("Converting song to data");
|
||||
println!("Path: {:?}", path);
|
||||
// let content = std::fs::read_to_string(path);
|
||||
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
@@ -105,6 +109,7 @@ impl Song {
|
||||
|
||||
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();
|
||||
@@ -115,9 +120,9 @@ impl Song {
|
||||
filename += &self.track.unwrap().to_string();
|
||||
|
||||
if i_type == 0 {
|
||||
filename += ".mp3";
|
||||
filename += constants::file_extensions::MP3_FILE_EXTENSION;
|
||||
} else {
|
||||
filename += ".wav";
|
||||
filename += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||
}
|
||||
|
||||
self.filepath = Some(filename);
|
||||
|
||||
+46
-36
@@ -1,17 +1,13 @@
|
||||
use std::default::Default;
|
||||
|
||||
use http::header;
|
||||
use http::HeaderMap;
|
||||
use http::HeaderValue;
|
||||
use reqwest;
|
||||
use reqwest::blocking::multipart;
|
||||
use reqwest::multipart::Form;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use reqwest::blocking::
|
||||
use reqwest::multipart::{Form, Part};
|
||||
use reqwest::Response;
|
||||
use reqwest::{Body, Client};
|
||||
use tokio::fs::File;
|
||||
|
||||
use crate::constants;
|
||||
use crate::models;
|
||||
|
||||
pub struct Upload {
|
||||
@@ -48,6 +44,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();
|
||||
let client = reqwest::Client::new();
|
||||
@@ -68,6 +65,7 @@ impl Upload {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO: Implement
|
||||
pub async fn upload_song_with_metadata(
|
||||
@@ -91,29 +89,6 @@ impl Upload {
|
||||
let song_raw_data = song_raw_data_as.await;
|
||||
let cover_raw_data: Vec<u8> = cover_raw_data_as.await;
|
||||
|
||||
/*
|
||||
match song_data {
|
||||
Ok(sd) => {
|
||||
println!("song converted to data. Length {:?}", sd.len());
|
||||
song_raw_data = sd;
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error: {:?}", er);
|
||||
std::process::exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
match cover_data {
|
||||
Ok(cv) => {
|
||||
cover_raw_data = cv;
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error: {:?}", er);
|
||||
std::process::exit(-1);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if url.is_empty() {
|
||||
println!("Url is empty");
|
||||
}
|
||||
@@ -122,6 +97,7 @@ impl Upload {
|
||||
println!("Length: {:?}", song_raw_data.len());
|
||||
println!("Token: {}", access_token);
|
||||
|
||||
/*
|
||||
let form_as = async {
|
||||
self.initialize_form(
|
||||
song_raw_data.clone(),
|
||||
@@ -129,18 +105,47 @@ impl Upload {
|
||||
song_detail.clone(),
|
||||
)
|
||||
};
|
||||
*/
|
||||
|
||||
// let form = self.initialize_form(song_raw_data, cover_raw_data, song_detail.clone());
|
||||
let form = form_as.await;
|
||||
// let form = form_as.await;
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
HeaderValue::from_str(&access_token.clone()).unwrap(),
|
||||
);
|
||||
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 mut client = reqwest::Client::new();
|
||||
let response = reqwest::Client::new()
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let response = client
|
||||
.post(url)
|
||||
.headers(headers)
|
||||
.multipart(form)
|
||||
.header(reqwest::header::AUTHORIZATION, access_token)
|
||||
.header(reqwest::header::CONTENT_TYPE, "multipart/form-data")
|
||||
.send()
|
||||
.await;
|
||||
let response_text = response.unwrap();
|
||||
@@ -172,10 +177,15 @@ impl Upload {
|
||||
|
||||
let mut 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.jpeg"))
|
||||
.part("cover", cover.file_name(cover_filename))
|
||||
.text("metadata", song_detail)
|
||||
.part("file", file.file_name("audio.wav"));
|
||||
.part("file", file.file_name(song_filename));
|
||||
}
|
||||
|
||||
pub fn set_api(&mut self, host: &String) {
|
||||
|
||||
Reference in New Issue
Block a user