Improved URL lookup handling

This commit is contained in:
G2-Games 2024-10-27 06:53:04 -05:00
parent aaaea89502
commit 769bd72542
3 changed files with 43 additions and 7 deletions

View file

@ -59,3 +59,36 @@ pub async fn lookup(
file
))
}
#[get("/f/<mmid>/<filename>")]
pub async fn lookup_filename(
db: &State<Arc<RwLock<Database>>>,
settings: &State<Settings>,
mmid: &str,
filename: &str,
) -> Option<(ContentType, NamedFile)> {
let mmid: Mmid = match mmid.try_into() {
Ok(v) => v,
Err(_) => return None,
};
let entry = if let Some(e) = db.read().unwrap().get(&mmid).cloned() {
e
} else {
return None
};
dbg!(entry.name());
dbg!(filename);
if entry.name() != filename {
return None
}
let file = NamedFile::open(settings.file_dir.join(entry.hash().to_string())).await.ok()?;
Some((
ContentType::from_extension(entry.extension()).unwrap_or(ContentType::Binary),
file
))
}

View file

@ -11,7 +11,7 @@ use std::{
use chrono::{DateTime, TimeDelta, Utc};
use database::{clean_loop, Database, Mmid, MochiFile};
use endpoints::{lookup, server_info};
use endpoints::{lookup, lookup_filename, server_info};
use log::info;
use maud::{html, Markup, PreEscaped, DOCTYPE};
use rocket::{
@ -264,7 +264,8 @@ async fn main() {
stylesheet,
server_info,
favicon,
lookup
lookup,
lookup_filename,
],
)
.mount(

View file

@ -88,22 +88,24 @@ function makeErrored(progressBar, progressText, linkRow, errorMessage) {
linkRow.style.background = "#ffb2ae";
}
function makeFinished(progressBar, progressText, linkRow, MMID, _hash) {
function makeFinished(progressBar, progressText, linkRow, response) {
progressText.textContent = "";
const name = encodeURIComponent(response.name);
const link = progressText.appendChild(document.createElement("a"));
link.textContent = MMID;
link.href = "/f/" + MMID;
link.textContent = response.mmid;
link.href = "/f/" + response.mmid;
link.target = "_blank";
let button = linkRow.appendChild(document.createElement("button"));
button.textContent = "📝";
let buttonTimeout = null;
button.addEventListener('click', function(_e) {
const mmid = response.mmid;
if (buttonTimeout) {
clearTimeout(buttonTimeout)
}
navigator.clipboard.writeText(
encodeURI(window.location.protocol + "//" + window.location.host + "/f/" + MMID)
window.location.protocol + "//" + window.location.host + "/f/" + mmid
)
button.textContent = "✅";
buttonTimeout = setTimeout(function() {
@ -141,7 +143,7 @@ function uploadComplete(response, progressBar, progressText, linkRow) {
if (response.status) {
console.log("Successfully uploaded file", response);
makeFinished(progressBar, progressText, linkRow, response.mmid, response.hash);
makeFinished(progressBar, progressText, linkRow, response);
} else {
console.error("Error uploading", response);
makeErrored(progressBar, progressText, linkRow, response.response);