mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-04-19 23:32:58 -05:00
Improved URL lookup handling
This commit is contained in:
parent
aaaea89502
commit
769bd72542
3 changed files with 43 additions and 7 deletions
|
@ -59,3 +59,36 @@ pub async fn lookup(
|
||||||
file
|
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
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ use std::{
|
||||||
|
|
||||||
use chrono::{DateTime, TimeDelta, Utc};
|
use chrono::{DateTime, TimeDelta, Utc};
|
||||||
use database::{clean_loop, Database, Mmid, MochiFile};
|
use database::{clean_loop, Database, Mmid, MochiFile};
|
||||||
use endpoints::{lookup, server_info};
|
use endpoints::{lookup, lookup_filename, server_info};
|
||||||
use log::info;
|
use log::info;
|
||||||
use maud::{html, Markup, PreEscaped, DOCTYPE};
|
use maud::{html, Markup, PreEscaped, DOCTYPE};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
|
@ -264,7 +264,8 @@ async fn main() {
|
||||||
stylesheet,
|
stylesheet,
|
||||||
server_info,
|
server_info,
|
||||||
favicon,
|
favicon,
|
||||||
lookup
|
lookup,
|
||||||
|
lookup_filename,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount(
|
.mount(
|
||||||
|
|
|
@ -88,22 +88,24 @@ function makeErrored(progressBar, progressText, linkRow, errorMessage) {
|
||||||
linkRow.style.background = "#ffb2ae";
|
linkRow.style.background = "#ffb2ae";
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeFinished(progressBar, progressText, linkRow, MMID, _hash) {
|
function makeFinished(progressBar, progressText, linkRow, response) {
|
||||||
progressText.textContent = "";
|
progressText.textContent = "";
|
||||||
|
const name = encodeURIComponent(response.name);
|
||||||
const link = progressText.appendChild(document.createElement("a"));
|
const link = progressText.appendChild(document.createElement("a"));
|
||||||
link.textContent = MMID;
|
link.textContent = response.mmid;
|
||||||
link.href = "/f/" + MMID;
|
link.href = "/f/" + response.mmid;
|
||||||
link.target = "_blank";
|
link.target = "_blank";
|
||||||
|
|
||||||
let button = linkRow.appendChild(document.createElement("button"));
|
let button = linkRow.appendChild(document.createElement("button"));
|
||||||
button.textContent = "📝";
|
button.textContent = "📝";
|
||||||
let buttonTimeout = null;
|
let buttonTimeout = null;
|
||||||
button.addEventListener('click', function(_e) {
|
button.addEventListener('click', function(_e) {
|
||||||
|
const mmid = response.mmid;
|
||||||
if (buttonTimeout) {
|
if (buttonTimeout) {
|
||||||
clearTimeout(buttonTimeout)
|
clearTimeout(buttonTimeout)
|
||||||
}
|
}
|
||||||
navigator.clipboard.writeText(
|
navigator.clipboard.writeText(
|
||||||
encodeURI(window.location.protocol + "//" + window.location.host + "/f/" + MMID)
|
window.location.protocol + "//" + window.location.host + "/f/" + mmid
|
||||||
)
|
)
|
||||||
button.textContent = "✅";
|
button.textContent = "✅";
|
||||||
buttonTimeout = setTimeout(function() {
|
buttonTimeout = setTimeout(function() {
|
||||||
|
@ -141,7 +143,7 @@ function uploadComplete(response, progressBar, progressText, linkRow) {
|
||||||
|
|
||||||
if (response.status) {
|
if (response.status) {
|
||||||
console.log("Successfully uploaded file", response);
|
console.log("Successfully uploaded file", response);
|
||||||
makeFinished(progressBar, progressText, linkRow, response.mmid, response.hash);
|
makeFinished(progressBar, progressText, linkRow, response);
|
||||||
} else {
|
} else {
|
||||||
console.error("Error uploading", response);
|
console.error("Error uploading", response);
|
||||||
makeErrored(progressBar, progressText, linkRow, response.response);
|
makeErrored(progressBar, progressText, linkRow, response.response);
|
||||||
|
|
Loading…
Reference in a new issue