Add mime_type column to files

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent d62f5b38bf
commit fd092e8f3e

@ -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(),
}
}

@ -0,0 +1,3 @@
-- Add migration script here
ALTER TABLE files
ADD COLUMN mime_type VARCHAR(128);

@ -9,6 +9,7 @@ pub struct Model {
pub file_type: u32,
pub name: Option<String>,
pub comment: Option<String>,
pub mime_type: Option<String>,
pub storage_id: i64,
pub hash_id: i64,
pub import_time: NaiveDateTime,

@ -77,6 +77,7 @@ impl File {
storage_id: i64,
hash: S,
file_type: FileType,
mime_type: Option<String>,
) -> RepoResult<Self> {
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<String> {
&self.model.mime_type
}
/// Returns the optional name of the file
pub fn name(&self) -> &Option<String> {
&self.model.name

@ -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<Mime> 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,
}
}
}

@ -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<File> {
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> {

@ -138,9 +138,10 @@ impl Storage {
&self,
reader: R,
file_type: FileType,
mime_type: Option<String>,
) -> RepoResult<File> {
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

@ -9,6 +9,7 @@ pub struct FileResponse {
pub comment: Option<String>,
pub hash: String,
pub file_type: FileType,
pub mime_type: Option<String>,
pub creation_time: NaiveDateTime,
pub change_time: NaiveDateTime,
pub import_time: NaiveDateTime,
@ -19,6 +20,7 @@ impl From<File> 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(),

Loading…
Cancel
Save