]> uap-core.de Git - note.git/commitdiff
add file extension 2 mime type mapping function
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 14 Aug 2026 19:55:01 +0000 (21:55 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 14 Aug 2026 19:55:01 +0000 (21:55 +0200)
application/backend/src/backend.rs
application/backend/src/note.rs

index 172371b04a0bf9b167de5c05e73a83fd28069a14..4ccf080ea96abaf8756ee0ee842483aefd504f9e 100644 (file)
@@ -49,7 +49,7 @@ use entity::note::{Column, Entity as Note, NoteType};
 use entity::notecontent::{Entity as NoteContent};
 use migration::prelude::Utc;
 use crate::lockmanager::LockManager;
-use crate::note::{create_nodename, randomize_nodename};
+use crate::note::{create_nodename, nodename_to_contenttype, randomize_nodename};
 use crate::notify::{fs_notify, FSWatch};
 use crate::storage::{*};
 
@@ -821,7 +821,7 @@ impl BackendHandle {
                 // prepare db insert
                 let nodename = generate_nodename_in_collection(&bhandle.backend.db, collection_id, name.as_str()).await?;
                 let title = nodename.clone(); // TODO
-                let contenttype = "image/png".to_string(); // TODO
+                let contenttype = nodename_to_contenttype(nodename.as_str()).to_string();
                 let insert = note::ActiveModel {
                     collection_id: Set(collection_id),
                     nodename: Set(nodename),
index f83b71def68a4a3547d70d29cacd92bd98c79659..f1db72bf3267ca22551149a7559ab6f9a172022b 100644 (file)
@@ -60,3 +60,87 @@ pub fn randomize_nodename(nodename: &str) -> String {
         None => format!("{stem}_{suffix}"),
     }
 }
+
+pub fn nodename_to_contenttype(name: &str) -> &'static str {
+    let ext = Path::new(name)
+        .extension()
+        .and_then(|ext| ext.to_str())
+        .unwrap_or("")
+        .to_ascii_lowercase();
+
+    match ext.as_str() {
+        // Images
+        "avif" => "image/avif",
+        "bmp" => "image/bmp",
+        "gif" => "image/gif",
+        "ico" => "image/x-icon",
+        "jpg" | "jpeg" => "image/jpeg",
+        "png" => "image/png",
+        "svg" => "image/svg+xml",
+        "tif" | "tiff" => "image/tiff",
+        "webp" => "image/webp",
+
+        // Audio
+        "aac" => "audio/aac",
+        "flac" => "audio/flac",
+        "m4a" => "audio/mp4",
+        "mp3" => "audio/mpeg",
+        "oga" | "ogg" => "audio/ogg",
+        "opus" => "audio/opus",
+        "wav" => "audio/wav",
+        "weba" => "audio/webm",
+
+        // Video
+        "avi" => "video/x-msvideo",
+        "m4v" => "video/x-m4v",
+        "mkv" => "video/x-matroska",
+        "mov" => "video/quicktime",
+        "mp4" => "video/mp4",
+        "mpeg" | "mpg" => "video/mpeg",
+        "ogv" => "video/ogg",
+        "webm" => "video/webm",
+
+        // Text / web
+        "css" => "text/css",
+        "csv" => "text/csv",
+        "html" | "htm" => "text/html",
+        "md" | "markdown" => "text/markdown",
+        "txt" => "text/plain",
+        "xml" => "application/xml",
+
+        // Data
+        "json" => "application/json",
+        "jsonld" => "application/ld+json",
+        "yaml" | "yml" => "application/yaml",
+        "toml" => "application/toml",
+
+        // Documents
+        "pdf" => "application/pdf",
+        "rtf" => "application/rtf",
+
+        "doc" => "application/msword",
+        "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+
+        "xls" => "application/vnd.ms-excel",
+        "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+
+        "ppt" => "application/vnd.ms-powerpoint",
+        "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
+
+        "odt" => "application/vnd.oasis.opendocument.text",
+        "ods" => "application/vnd.oasis.opendocument.spreadsheet",
+        "odp" => "application/vnd.oasis.opendocument.presentation",
+
+        // Archives
+        "7z" => "application/x-7z-compressed",
+        "bz" | "bz2" => "application/x-bzip2",
+        "gz" => "application/gzip",
+        "rar" => "application/vnd.rar",
+        "tar" => "application/x-tar",
+        "xz" => "application/x-xz",
+        "zip" => "application/zip",
+
+        // Unknown / unsupported
+        _ => "application/octet-stream",
+    }
+}
\ No newline at end of file