From 9609cfb33af25a8ec6d484bbe21e678976a44144 Mon Sep 17 00:00:00 2001
From: Pierre HUBERT <pierre.git@communiquons.org>
Date: Sat, 7 Jun 2025 11:28:39 +0200
Subject: [PATCH] Check if file can be loaded during disk image upload

---
 virtweb_backend/src/constants.rs                       |  3 ++-
 .../src/controllers/disk_images_controller.rs          | 10 +++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/virtweb_backend/src/constants.rs b/virtweb_backend/src/constants.rs
index e1079ba..214ad6b 100644
--- a/virtweb_backend/src/constants.rs
+++ b/virtweb_backend/src/constants.rs
@@ -30,8 +30,9 @@ pub const ALLOWED_ISO_MIME_TYPES: [&str; 4] = [
 pub const ISO_MAX_SIZE: FileSize = FileSize::from_gb(10);
 
 /// Allowed uploaded disk images formats
-pub const ALLOWED_DISK_IMAGES_MIME_TYPES: [&str; 3] = [
+pub const ALLOWED_DISK_IMAGES_MIME_TYPES: [&str; 4] = [
     "application/x-qemu-disk",
+    "application/x-raw-disk-image",
     "application/gzip",
     "application/octet-stream",
 ];
diff --git a/virtweb_backend/src/controllers/disk_images_controller.rs b/virtweb_backend/src/controllers/disk_images_controller.rs
index 82038f5..f19c77d 100644
--- a/virtweb_backend/src/controllers/disk_images_controller.rs
+++ b/virtweb_backend/src/controllers/disk_images_controller.rs
@@ -55,7 +55,15 @@ pub async fn upload(MultipartForm(mut form): MultipartForm<UploadDiskImageForm>)
     }
 
     // Copy the file to the destination
-    file.file.persist(dest_path)?;
+    file.file.persist(&dest_path)?;
+
+    // Check if file information can be loaded
+    if let Err(e) = DiskFileInfo::load_file(&dest_path) {
+        log::error!("Failed to get information about uploaded disk file! {e}");
+        std::fs::remove_file(&dest_path)?;
+        return Ok(HttpResponse::InternalServerError()
+            .json(format!("Unable to process uploaded file! {e}")));
+    }
 
     Ok(HttpResponse::Ok().json("Successfully uploaded disk image!"))
 }