tsk-83: Updated function to generate coverart filename
This commit is contained in:
@@ -1,11 +1,10 @@
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
const FILENAME_LENGTH: i32 = 16;
|
const FILENAME_LENGTH: i32 = 16;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct CoverArt {
|
pub struct CoverArt {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
@@ -18,11 +17,9 @@ pub struct CoverArt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod init {
|
pub mod init {
|
||||||
use super::CoverArt;
|
|
||||||
|
|
||||||
/// Initializes the CoverArt with just the directory and filename
|
/// Initializes the CoverArt with just the directory and filename
|
||||||
pub fn init_coverart_dir_and_filename(directory: &str, filename: &str) -> CoverArt {
|
pub fn init_coverart_dir_and_filename(directory: &str, filename: &str) -> super::CoverArt {
|
||||||
CoverArt {
|
super::CoverArt {
|
||||||
directory: String::from(directory),
|
directory: String::from(directory),
|
||||||
filename: String::from(filename),
|
filename: String::from(filename),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -88,7 +85,7 @@ impl CoverArt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Generates filename for a CoverArt
|
/// Generates filename for a CoverArt
|
||||||
pub fn generate_filename(typ: crate::types::CoverArtTypes, randomize: bool) -> String {
|
pub fn generate_filename(typ: crate::types::CoverArtTypes, randomize: bool) -> Result<String, std::io::Error> {
|
||||||
let file_extension = match typ {
|
let file_extension = match typ {
|
||||||
crate::types::CoverArtTypes::PngExtension => {
|
crate::types::CoverArtTypes::PngExtension => {
|
||||||
String::from(crate::constants::file_extensions::image::PNGEXTENSION)
|
String::from(crate::constants::file_extensions::image::PNGEXTENSION)
|
||||||
@@ -99,9 +96,12 @@ pub fn generate_filename(typ: crate::types::CoverArtTypes, randomize: bool) -> S
|
|||||||
crate::types::CoverArtTypes::JpgExtension => {
|
crate::types::CoverArtTypes::JpgExtension => {
|
||||||
String::from(crate::constants::file_extensions::image::JPGEXTENSION)
|
String::from(crate::constants::file_extensions::image::JPGEXTENSION)
|
||||||
}
|
}
|
||||||
|
crate::types::CoverArtTypes::None => {
|
||||||
|
return Err(std::io::Error::other("Unsupported CoverArtTypes"));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if randomize {
|
let filename: String = if randomize {
|
||||||
let mut filename: String = String::from("coverart-");
|
let mut filename: String = String::from("coverart-");
|
||||||
let some_chars: String = String::from("abcdefghij0123456789");
|
let some_chars: String = String::from("abcdefghij0123456789");
|
||||||
let some_chars_length = some_chars.len();
|
let some_chars_length = some_chars.len();
|
||||||
@@ -115,10 +115,12 @@ pub fn generate_filename(typ: crate::types::CoverArtTypes, randomize: bool) -> S
|
|||||||
filename.push(c);
|
filename.push(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filename + &file_extension
|
format!("{filename}{file_extension}")
|
||||||
} else {
|
} else {
|
||||||
"coverart-output".to_string() + &file_extension
|
format!("coverart-output{file_extension}")
|
||||||
}
|
};
|
||||||
|
|
||||||
|
Ok(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod io {
|
pub mod io {
|
||||||
|
|||||||
Reference in New Issue
Block a user