mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-04-19 15:22:57 -05:00
Added download
option to mmid lookup
This commit is contained in:
parent
4f3250f655
commit
8cacaa53cc
4 changed files with 46 additions and 11 deletions
|
@ -16,6 +16,8 @@ rocket = { version = "0.5", features = ["json"] }
|
||||||
serde = { version = "1.0.213", features = ["derive"] }
|
serde = { version = "1.0.213", features = ["derive"] }
|
||||||
serde_with = { version = "3.11.0", features = ["chrono_0_4"] }
|
serde_with = { version = "3.11.0", features = ["chrono_0_4"] }
|
||||||
toml = "0.8.19"
|
toml = "0.8.19"
|
||||||
|
unidecode = "0.3.0"
|
||||||
|
urlencoding = "2.1.3"
|
||||||
uuid = { version = "1.11.0", features = ["serde", "v4"] }
|
uuid = { version = "1.11.0", features = ["serde", "v4"] }
|
||||||
|
|
||||||
[profile.production]
|
[profile.production]
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use rocket::{
|
use rocket::{
|
||||||
get, http::ContentType, response::{self, Redirect, Responder, Response}, serde::{self, json::Json}, tokio::fs::File, uri, Request, State
|
get, http::{uri::Uri, ContentType, Header}, response::{self, Redirect, Responder, Response}, serde::{self, json::Json}, tokio::{self, fs::File}, uri, Request, State
|
||||||
};
|
};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
|
@ -60,12 +60,13 @@ pub async fn lookup_mmid(db: &State<Arc<RwLock<Mochibase>>>, mmid: &str) -> Opti
|
||||||
))))
|
))))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/f/<mmid>?noredir")]
|
#[get("/f/<mmid>?noredir&<download>")]
|
||||||
pub async fn lookup_mmid_noredir(
|
pub async fn lookup_mmid_noredir(
|
||||||
db: &State<Arc<RwLock<Mochibase>>>,
|
db: &State<Arc<RwLock<Mochibase>>>,
|
||||||
settings: &State<Settings>,
|
settings: &State<Settings>,
|
||||||
mmid: &str,
|
mmid: &str,
|
||||||
) -> Option<(ContentType, File)> {
|
download: bool,
|
||||||
|
) -> Option<FileDownloader> {
|
||||||
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()?;
|
||||||
|
|
||||||
|
@ -73,12 +74,43 @@ pub async fn lookup_mmid_noredir(
|
||||||
.await
|
.await
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
Some((
|
Some(FileDownloader {
|
||||||
ContentType::from_str(entry.mime_type()).unwrap_or(ContentType::Binary),
|
inner: file,
|
||||||
file,
|
filename: entry.name().clone(),
|
||||||
))
|
content_type: ContentType::from_str(entry.mime_type()).unwrap_or(ContentType::Binary),
|
||||||
|
disposition: download
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct FileDownloader {
|
||||||
|
inner: tokio::fs::File,
|
||||||
|
filename: String,
|
||||||
|
content_type: ContentType,
|
||||||
|
disposition: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'r> Responder<'r, 'r> for FileDownloader {
|
||||||
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
|
||||||
|
let mut resp = Response::build();
|
||||||
|
resp.streamed_body(self.inner)
|
||||||
|
.header(self.content_type);
|
||||||
|
|
||||||
|
if self.disposition {
|
||||||
|
resp.raw_header(
|
||||||
|
"Content-Disposition",
|
||||||
|
format!(
|
||||||
|
"attachment; filename=\"{}\"; filename*=UTF-8''{}",
|
||||||
|
unidecode::unidecode(&self.filename),
|
||||||
|
urlencoding::encode(&self.filename)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[get("/f/<mmid>/<name>")]
|
#[get("/f/<mmid>/<name>")]
|
||||||
pub async fn lookup_mmid_name(
|
pub async fn lookup_mmid_name(
|
||||||
db: &State<Arc<RwLock<Mochibase>>>,
|
db: &State<Arc<RwLock<Mochibase>>>,
|
||||||
|
|
|
@ -20,10 +20,9 @@ use chrono::{TimeDelta, Utc};
|
||||||
use database::{Chunkbase, ChunkedInfo, Mmid, MochiFile, Mochibase};
|
use database::{Chunkbase, ChunkedInfo, Mmid, MochiFile, Mochibase};
|
||||||
use maud::{html, Markup, PreEscaped};
|
use maud::{html, Markup, PreEscaped};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
data::ToByteUnit, get, post, serde::{json::Json, Serialize}, tokio::{
|
data::ToByteUnit, get, http::ContentType, post, serde::{json::Json, Serialize}, tokio::{
|
||||||
fs,
|
self, fs, io::{AsyncSeekExt, AsyncWriteExt}
|
||||||
io::{AsyncSeekExt, AsyncWriteExt},
|
}, Data, Responder, State
|
||||||
}, Data, State
|
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
|
|
@ -131,6 +131,8 @@ pub fn api_info(settings: &State<Settings>) -> Markup {
|
||||||
behavior can be modified by appending " code{"?noredir"} " to
|
behavior can be modified by appending " code{"?noredir"} " to
|
||||||
the end of this request, like " code{"/f/<mmid>?noredir"} ",
|
the end of this request, like " code{"/f/<mmid>?noredir"} ",
|
||||||
in which case it behaves just like " code{"/f/<mmid>/<filename>"}
|
in which case it behaves just like " code{"/f/<mmid>/<filename>"}
|
||||||
|
". Appending " code{"download"} " forces the browser to download
|
||||||
|
the file regardless of MIME type."
|
||||||
}
|
}
|
||||||
p {"Example default response:"}
|
p {"Example default response:"}
|
||||||
pre {"303: /f/xNLF6ogx/1600-1200.jpg"}
|
pre {"303: /f/xNLF6ogx/1600-1200.jpg"}
|
||||||
|
|
Loading…
Reference in a new issue