From fd092e8f3e32271a12759850bd3b85dd00e0f868 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 11 Oct 2021 20:20:52 +0200 Subject: [PATCH] Add mime_type column to files Signed-off-by: trivernis --- .../mediarepo-core/src/settings.rs | 2 +- .../20211011181015_add-file-mime-column.sql | 3 +++ .../mediarepo-database/src/entities/file.rs | 1 + mediarepo-daemon/mediarepo-model/src/file.rs | 7 +++++++ .../mediarepo-model/src/file_type.rs | 21 +++++++------------ mediarepo-daemon/mediarepo-model/src/repo.rs | 10 +++++++-- .../mediarepo-model/src/storage.rs | 3 ++- .../mediarepo-socket/src/types/responses.rs | 2 ++ 8 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-database/migrations/20211011181015_add-file-mime-column.sql diff --git a/mediarepo-daemon/mediarepo-core/src/settings.rs b/mediarepo-daemon/mediarepo-core/src/settings.rs index fff82aa..45467fc 100644 --- a/mediarepo-daemon/mediarepo-core/src/settings.rs +++ b/mediarepo-daemon/mediarepo-core/src/settings.rs @@ -12,7 +12,7 @@ impl Default for Settings { fn default() -> Self { Self { listen_address: "127.0.0.1:3425".to_string(), - database_path: "./db/database.db".to_string(), + database_path: "./db/repo.db".to_string(), default_file_store: "./files".to_string(), } } diff --git a/mediarepo-daemon/mediarepo-database/migrations/20211011181015_add-file-mime-column.sql b/mediarepo-daemon/mediarepo-database/migrations/20211011181015_add-file-mime-column.sql new file mode 100644 index 0000000..94c6a6e --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/migrations/20211011181015_add-file-mime-column.sql @@ -0,0 +1,3 @@ +-- Add migration script here +ALTER TABLE files + ADD COLUMN mime_type VARCHAR(128); \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/file.rs b/mediarepo-daemon/mediarepo-database/src/entities/file.rs index ff35d0c..3dc36d7 100644 --- a/mediarepo-daemon/mediarepo-database/src/entities/file.rs +++ b/mediarepo-daemon/mediarepo-database/src/entities/file.rs @@ -9,6 +9,7 @@ pub struct Model { pub file_type: u32, pub name: Option, pub comment: Option, + pub mime_type: Option, pub storage_id: i64, pub hash_id: i64, pub import_time: NaiveDateTime, diff --git a/mediarepo-daemon/mediarepo-model/src/file.rs b/mediarepo-daemon/mediarepo-model/src/file.rs index dfb30d5..367c6fa 100644 --- a/mediarepo-daemon/mediarepo-model/src/file.rs +++ b/mediarepo-daemon/mediarepo-model/src/file.rs @@ -77,6 +77,7 @@ impl File { storage_id: i64, hash: S, file_type: FileType, + mime_type: Option, ) -> RepoResult { let hash = hash::ActiveModel { value: Set(hash.to_string()), @@ -88,6 +89,7 @@ impl File { let file = file::ActiveModel { hash_id: Set(id), file_type: Set(file_type as u32), + mime_type: Set(mime_type), storage_id: Set(storage_id), import_time: Set(now.clone()), creation_time: Set(now.clone()), @@ -122,6 +124,11 @@ impl File { } } + /// Returns the optional mime type of the file + pub fn mime_type(&self) -> &Option { + &self.model.mime_type + } + /// Returns the optional name of the file pub fn name(&self) -> &Option { &self.model.name diff --git a/mediarepo-daemon/mediarepo-model/src/file_type.rs b/mediarepo-daemon/mediarepo-model/src/file_type.rs index 50cec01..a5d17b6 100644 --- a/mediarepo-daemon/mediarepo-model/src/file_type.rs +++ b/mediarepo-daemon/mediarepo-model/src/file_type.rs @@ -1,5 +1,5 @@ +use mime::Mime; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; #[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq)] pub enum FileType { @@ -10,18 +10,13 @@ pub enum FileType { Audio = 3, } -impl From<&PathBuf> for FileType { - fn from(path: &PathBuf) -> Self { - let mime = mime_guess::from_path(path).first(); - if let Some(mime) = mime { - match mime.type_() { - mime::IMAGE => Self::Image, - mime::VIDEO => Self::Video, - mime::AUDIO => Self::Audio, - _ => Self::Other, - } - } else { - Self::Unknown +impl From for FileType { + fn from(mime_type: Mime) -> Self { + match mime_type.type_() { + mime::IMAGE => Self::Image, + mime::VIDEO => Self::Video, + mime::AUDIO => Self::Audio, + _ => Self::Other, } } } diff --git a/mediarepo-daemon/mediarepo-model/src/repo.rs b/mediarepo-daemon/mediarepo-model/src/repo.rs index 232f080..a3b957e 100644 --- a/mediarepo-daemon/mediarepo-model/src/repo.rs +++ b/mediarepo-daemon/mediarepo-model/src/repo.rs @@ -69,12 +69,18 @@ impl Repo { /// Adds a file to the database by its readable path in the file system pub async fn add_file_by_path(&self, path: PathBuf) -> RepoResult { - let file_type = FileType::from(&path); + let mime_match = mime_guess::from_path(&path).first(); + + let (mime_type, file_type) = if let Some(mime) = mime_match { + (Some(mime.clone().to_string()), FileType::from(mime)) + } else { + (None, FileType::Unknown) + }; let os_file = OpenOptions::new().read(true).open(&path).await?; let reader = BufReader::new(os_file); let storage = self.get_main_storage()?; - storage.add_file(reader, file_type).await + storage.add_file(reader, file_type, mime_type).await } fn get_main_storage(&self) -> RepoResult<&Storage> { diff --git a/mediarepo-daemon/mediarepo-model/src/storage.rs b/mediarepo-daemon/mediarepo-model/src/storage.rs index 4c3a3bd..e08e5e8 100644 --- a/mediarepo-daemon/mediarepo-model/src/storage.rs +++ b/mediarepo-daemon/mediarepo-model/src/storage.rs @@ -138,9 +138,10 @@ impl Storage { &self, reader: R, file_type: FileType, + mime_type: Option, ) -> RepoResult { let hash = self.store.add_file(reader, None).await?; - File::add(self.db.clone(), self.id(), hash, file_type).await + File::add(self.db.clone(), self.id(), hash, file_type, mime_type).await } /// Returns the buf reader to the given hash diff --git a/mediarepo-daemon/mediarepo-socket/src/types/responses.rs b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs index d0a66f6..f84fa13 100644 --- a/mediarepo-daemon/mediarepo-socket/src/types/responses.rs +++ b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs @@ -9,6 +9,7 @@ pub struct FileResponse { pub comment: Option, pub hash: String, pub file_type: FileType, + pub mime_type: Option, pub creation_time: NaiveDateTime, pub change_time: NaiveDateTime, pub import_time: NaiveDateTime, @@ -19,6 +20,7 @@ impl From for FileResponse { FileResponse { hash: file.hash().to_owned(), file_type: file.file_type(), + mime_type: file.mime_type().clone(), name: file.name().to_owned(), creation_time: file.creation_time().to_owned(), change_time: file.change_time().to_owned(),