From: Olaf Wintermann Date: Fri, 14 Aug 2026 19:55:01 +0000 (+0200) Subject: add file extension 2 mime type mapping function X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=2502640ff6c6fea65072ff8115874663e38d5dea;p=note.git add file extension 2 mime type mapping function --- diff --git a/application/backend/src/backend.rs b/application/backend/src/backend.rs index 172371b..4ccf080 100644 --- a/application/backend/src/backend.rs +++ b/application/backend/src/backend.rs @@ -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), diff --git a/application/backend/src/note.rs b/application/backend/src/note.rs index f83b71d..f1db72b 100644 --- a/application/backend/src/note.rs +++ b/application/backend/src/note.rs @@ -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