Add the logic which will call disk image conversion from disk image route
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::constants;
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::utils::file_disks_utils::DiskFileInfo;
|
||||
use crate::utils::file_disks_utils::{DiskFileFormat, DiskFileInfo};
|
||||
use crate::utils::files_utils;
|
||||
use actix_files::NamedFile;
|
||||
use actix_multipart::form::MultipartForm;
|
||||
@ -91,6 +91,59 @@ pub async fn download(p: web::Path<DiskFilePath>, req: HttpRequest) -> HttpResul
|
||||
Ok(NamedFile::open(file_path)?.into_response(&req))
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct ConvertDiskImageRequest {
|
||||
dest_file_name: String,
|
||||
#[serde(flatten)]
|
||||
format: DiskFileFormat,
|
||||
}
|
||||
|
||||
/// Convert disk image into a new format
|
||||
pub async fn convert(
|
||||
p: web::Path<DiskFilePath>,
|
||||
req: web::Json<ConvertDiskImageRequest>,
|
||||
) -> HttpResult {
|
||||
if !files_utils::check_file_name(&p.filename) {
|
||||
return Ok(HttpResponse::BadRequest().json("Invalid src file name!"));
|
||||
}
|
||||
|
||||
if !files_utils::check_file_name(&req.dest_file_name) {
|
||||
return Ok(HttpResponse::BadRequest().json("Invalid destination file name!"));
|
||||
}
|
||||
if !req
|
||||
.format
|
||||
.ext()
|
||||
.iter()
|
||||
.any(|e| req.dest_file_name.ends_with(e))
|
||||
{
|
||||
return Ok(HttpResponse::BadRequest().json("Invalid destination file extension!"));
|
||||
}
|
||||
|
||||
let src_file_path = AppConfig::get()
|
||||
.disk_images_storage_path()
|
||||
.join(&p.filename);
|
||||
|
||||
let src = DiskFileInfo::load_file(&src_file_path)?;
|
||||
|
||||
let dst_file_path = AppConfig::get()
|
||||
.disk_images_storage_path()
|
||||
.join(&req.dest_file_name);
|
||||
|
||||
if dst_file_path.exists() {
|
||||
return Ok(HttpResponse::Conflict().json("Specified destination file already exists!"));
|
||||
}
|
||||
|
||||
// Perform conversion
|
||||
if let Err(e) = src.convert(&dst_file_path, req.format) {
|
||||
log::error!("Disk file conversion error: {e}");
|
||||
return Ok(
|
||||
HttpResponse::InternalServerError().json(format!("Disk file conversion error: {e}"))
|
||||
);
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(src))
|
||||
}
|
||||
|
||||
/// Delete a disk image
|
||||
pub async fn delete(p: web::Path<DiskFilePath>) -> HttpResult {
|
||||
if !files_utils::check_file_name(&p.filename) {
|
||||
|
Reference in New Issue
Block a user