Can upload ISO files

This commit is contained in:
2023-09-05 13:19:25 +02:00
parent 83ccd3a4b9
commit 036595fb24
24 changed files with 671 additions and 36 deletions

View File

@ -0,0 +1,60 @@
use crate::app_config::AppConfig;
use crate::constants;
use crate::controllers::HttpResult;
use crate::utils::files_utils;
use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::MultipartForm;
use actix_web::HttpResponse;
#[derive(Debug, MultipartForm)]
pub struct UploadIsoForm {
#[multipart(rename = "file")]
files: Vec<TempFile>,
}
/// Upload iso file
pub async fn upload_file(MultipartForm(mut form): MultipartForm<UploadIsoForm>) -> HttpResult {
if form.files.is_empty() {
log::error!("Missing uploaded ISO file!");
return Ok(HttpResponse::BadRequest().json("Missing file!"));
}
let file = form.files.remove(0);
if file.size > constants::ISO_MAX_SIZE {
log::error!("Uploaded ISO file is too large!");
return Ok(HttpResponse::BadRequest().json("File is too large!"));
}
if let Some(m) = &file.content_type {
if !constants::ALLOWED_ISO_MIME_TYPES.contains(&m.to_string().as_str()) {
log::error!("Uploaded ISO file has an invalid mimetype!");
return Ok(HttpResponse::BadRequest().json("Invalid mimetype!"));
}
}
let file_name = match &file.file_name {
None => {
log::error!("Uploaded ISO file does not have a name!");
return Ok(HttpResponse::BadRequest().json("Missing file name!"));
}
Some(f) => f,
};
if !files_utils::check_file_name(file_name) {
log::error!("Bad file name for uploaded iso!");
return Ok(HttpResponse::BadRequest().json("Bad file name!"));
}
let dest_file = AppConfig::get().iso_storage_path().join(file_name);
log::info!("Will save ISO file {:?}", dest_file);
if dest_file.exists() {
log::error!("Conflict with uploaded iso file name!");
return Ok(HttpResponse::Conflict().json("The file already exists!"));
}
file.file.persist(dest_file)?;
Ok(HttpResponse::Accepted().finish())
}

View File

@ -5,6 +5,7 @@ use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
pub mod auth_controller;
pub mod iso_controller;
pub mod server_controller;
/// Custom error to ease controller writing
@ -58,4 +59,10 @@ impl From<std::num::ParseIntError> for HttpErr {
}
}
impl From<tempfile::PersistError> for HttpErr {
fn from(value: tempfile::PersistError) -> Self {
HttpErr { err: value.into() }
}
}
pub type HttpResult = Result<HttpResponse, HttpErr>;

View File

@ -1,4 +1,5 @@
use crate::app_config::AppConfig;
use crate::constants;
use crate::extractors::local_auth_extractor::LocalAuthEnabled;
use actix_web::{HttpResponse, Responder};
@ -10,11 +11,15 @@ pub async fn root_index() -> impl Responder {
struct StaticConfig {
local_auth_enabled: bool,
oidc_auth_enabled: bool,
iso_mimetypes: &'static [&'static str],
iso_max_size: usize,
}
pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
HttpResponse::Ok().json(StaticConfig {
local_auth_enabled: *local_auth,
oidc_auth_enabled: !AppConfig::get().disable_oidc,
iso_mimetypes: &constants::ALLOWED_ISO_MIME_TYPES,
iso_max_size: constants::ISO_MAX_SIZE,
})
}