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::{*};
// 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),
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