mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-04-19 15:22:57 -05:00
Get file type from MIME type rather than extension
This commit is contained in:
parent
46397a1f03
commit
96a6b5cf9d
4 changed files with 26 additions and 18 deletions
|
@ -151,7 +151,6 @@ impl Database {
|
||||||
|
|
||||||
/// An entry in the database storing metadata about a file
|
/// An entry in the database storing metadata about a file
|
||||||
#[derive(Debug, Clone, Decode, Encode, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Decode, Encode, Deserialize, Serialize)]
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
pub struct MochiFile {
|
pub struct MochiFile {
|
||||||
/// A unique identifier describing this file
|
/// A unique identifier describing this file
|
||||||
mmid: Mmid,
|
mmid: Mmid,
|
||||||
|
@ -159,8 +158,8 @@ pub struct MochiFile {
|
||||||
/// The original name of the file
|
/// The original name of the file
|
||||||
name: String,
|
name: String,
|
||||||
|
|
||||||
/// The format the file is, for serving
|
/// The MIME type of the file
|
||||||
extension: String,
|
mime_type: String,
|
||||||
|
|
||||||
/// The Blake3 hash of the file
|
/// The Blake3 hash of the file
|
||||||
#[bincode(with_serde)]
|
#[bincode(with_serde)]
|
||||||
|
@ -180,7 +179,7 @@ impl MochiFile {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
mmid: Mmid,
|
mmid: Mmid,
|
||||||
name: String,
|
name: String,
|
||||||
extension: &str,
|
mime_type: String,
|
||||||
hash: Hash,
|
hash: Hash,
|
||||||
upload: DateTime<Utc>,
|
upload: DateTime<Utc>,
|
||||||
expiry: DateTime<Utc>,
|
expiry: DateTime<Utc>,
|
||||||
|
@ -188,7 +187,7 @@ impl MochiFile {
|
||||||
Self {
|
Self {
|
||||||
mmid,
|
mmid,
|
||||||
name,
|
name,
|
||||||
extension: extension.to_string(),
|
mime_type,
|
||||||
hash,
|
hash,
|
||||||
upload_datetime: upload,
|
upload_datetime: upload,
|
||||||
expiry_datetime: expiry,
|
expiry_datetime: expiry,
|
||||||
|
@ -216,8 +215,8 @@ impl MochiFile {
|
||||||
&self.mmid
|
&self.mmid
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extension(&self) -> &String {
|
pub fn mime_type(&self) -> &String {
|
||||||
&self.extension
|
&self.mime_type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::sync::{Arc, RwLock};
|
use std::{str::FromStr, sync::{Arc, RwLock}};
|
||||||
|
|
||||||
use rocket::{
|
use rocket::{
|
||||||
get,
|
get,
|
||||||
|
@ -11,7 +11,7 @@ use rocket::{
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
database::{Database, Mmid},
|
database::{Database, Mmid, MochiFile},
|
||||||
settings::Settings,
|
settings::Settings,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -32,6 +32,18 @@ pub fn server_info(settings: &State<Settings>) -> Json<ServerInfo> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get information about a file
|
||||||
|
#[get("/info/<mmid>")]
|
||||||
|
pub async fn file_information(
|
||||||
|
db: &State<Arc<RwLock<Database>>>,
|
||||||
|
mmid: &str,
|
||||||
|
) -> Option<Json<MochiFile>> {
|
||||||
|
let mmid: Mmid = mmid.try_into().ok()?;
|
||||||
|
let entry = db.read().unwrap().get(&mmid).cloned()?;
|
||||||
|
|
||||||
|
Some(Json(entry))
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
#[serde(crate = "rocket::serde")]
|
#[serde(crate = "rocket::serde")]
|
||||||
pub struct ServerInfo {
|
pub struct ServerInfo {
|
||||||
|
@ -60,17 +72,14 @@ pub async fn lookup_mmid_noredir(
|
||||||
mmid: &str,
|
mmid: &str,
|
||||||
) -> Option<(ContentType, File)> {
|
) -> Option<(ContentType, File)> {
|
||||||
let mmid: Mmid = mmid.try_into().ok()?;
|
let mmid: Mmid = mmid.try_into().ok()?;
|
||||||
|
|
||||||
let entry = db.read().unwrap().get(&mmid).cloned()?;
|
let entry = db.read().unwrap().get(&mmid).cloned()?;
|
||||||
|
|
||||||
let file = File::open(settings.file_dir.join(entry.hash().to_string()))
|
let file = File::open(settings.file_dir.join(entry.hash().to_string()))
|
||||||
.await
|
.await
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
dbg!(entry.extension());
|
|
||||||
|
|
||||||
Some((
|
Some((
|
||||||
ContentType::from_extension(entry.extension()).unwrap_or(ContentType::Binary),
|
ContentType::from_str(entry.mime_type()).unwrap_or(ContentType::Binary),
|
||||||
file,
|
file,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -83,7 +92,6 @@ pub async fn lookup_mmid_name(
|
||||||
name: &str,
|
name: &str,
|
||||||
) -> Option<(ContentType, File)> {
|
) -> Option<(ContentType, File)> {
|
||||||
let mmid: Mmid = mmid.try_into().ok()?;
|
let mmid: Mmid = mmid.try_into().ok()?;
|
||||||
|
|
||||||
let entry = db.read().unwrap().get(&mmid).cloned()?;
|
let entry = db.read().unwrap().get(&mmid).cloned()?;
|
||||||
|
|
||||||
// If the name does not match, then this is invalid
|
// If the name does not match, then this is invalid
|
||||||
|
@ -96,7 +104,7 @@ pub async fn lookup_mmid_name(
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
Some((
|
Some((
|
||||||
ContentType::from_extension(entry.extension()).unwrap_or(ContentType::Binary),
|
ContentType::from_str(entry.mime_type()).unwrap_or(ContentType::Binary),
|
||||||
file,
|
file,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,13 +140,14 @@ async fn handle_upload(
|
||||||
let constructed_file = MochiFile::new(
|
let constructed_file = MochiFile::new(
|
||||||
file_mmid.clone(),
|
file_mmid.clone(),
|
||||||
raw_name,
|
raw_name,
|
||||||
file_type.extension(),
|
file_type.media_type().to_string(),
|
||||||
file_hash,
|
file_hash,
|
||||||
current,
|
current,
|
||||||
expiry
|
expiry
|
||||||
);
|
);
|
||||||
|
|
||||||
// If the hash does not exist in the database, move the file to the backend, else, delete it
|
// If the hash does not exist in the database,
|
||||||
|
// move the file to the backend, else, delete it
|
||||||
if db.read().unwrap().get_hash(&file_hash).is_none() {
|
if db.read().unwrap().get_hash(&file_hash).is_none() {
|
||||||
std::fs::rename(temp_filename, settings.file_dir.join(file_hash.to_string()))?;
|
std::fs::rename(temp_filename, settings.file_dir.join(file_hash.to_string()))?;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub fn footer() -> Markup {
|
||||||
html! {
|
html! {
|
||||||
footer {
|
footer {
|
||||||
p {a href="/" {"Home"}}
|
p {a href="/" {"Home"}}
|
||||||
p {a href="https://github.com/G2-Games/confetti-box" {"Source"}}
|
p {a href="https://github.com/Dangoware/confetti-box" {"Source"}}
|
||||||
p {a href="https://g2games.dev/" {"My Website"}}
|
p {a href="https://g2games.dev/" {"My Website"}}
|
||||||
p {a href="api" {"API"}}
|
p {a href="api" {"API"}}
|
||||||
p {a href="https://ko-fi.com/g2_games" {"Donate"}}
|
p {a href="https://ko-fi.com/g2_games" {"Donate"}}
|
||||||
|
|
Loading…
Reference in a new issue