mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-06-23 07:02:58 -05:00
Added opengraph endpoint
This commit is contained in:
parent
83237c5d5a
commit
4f39f6b081
4 changed files with 95 additions and 15 deletions
|
@ -3,14 +3,15 @@ use std::{
|
||||||
sync::{Arc, RwLock},
|
sync::{Arc, RwLock},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use chrono::Utc;
|
||||||
|
use maud::{html, Markup, DOCTYPE};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
get, http::ContentType, response::{self, Redirect, Responder, Response}, serde::{self, json::Json}, tokio::{self, fs::File}, uri, Request, State
|
get, http::ContentType, response::{self, Redirect, Responder, Response}, serde::{self, json::Json}, tokio::{self, fs::File}, uri, Request, State
|
||||||
};
|
};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
database::{Mmid, MochiFile, Mochibase},
|
database::{Mmid, MochiFile, Mochibase}, settings::Settings, strings::{to_pretty_size, to_pretty_time, BreakStyle, TimeGranularity}
|
||||||
settings::Settings,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An endpoint to obtain information about the server's capabilities
|
/// An endpoint to obtain information about the server's capabilities
|
||||||
|
@ -39,6 +40,37 @@ pub async fn file_info(db: &State<Arc<RwLock<Mochibase>>>, mmid: &str) -> Option
|
||||||
Some(Json(entry))
|
Some(Json(entry))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/info/<mmid>?opengraph")]
|
||||||
|
pub async fn file_info_opengraph(
|
||||||
|
db: &State<Arc<RwLock<Mochibase>>>,
|
||||||
|
settings: &State<Settings>,
|
||||||
|
mmid: &str,
|
||||||
|
) -> Option<Markup> {
|
||||||
|
let mmid: Mmid = mmid.try_into().ok()?;
|
||||||
|
let entry = db.read().unwrap().get(&mmid).cloned()?;
|
||||||
|
|
||||||
|
let file = File::open(settings.file_dir.join(entry.hash().to_string()))
|
||||||
|
.await
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let size = to_pretty_size(file.metadata().await.ok()?.len());
|
||||||
|
|
||||||
|
let seconds_till_expiry = entry.expiry().signed_duration_since(Utc::now()).num_seconds();
|
||||||
|
let expiry = to_pretty_time(seconds_till_expiry as u32, BreakStyle::Space, TimeGranularity::Minutes);
|
||||||
|
|
||||||
|
let title = entry.name().clone() + " - " + &size + " - " + &expiry;
|
||||||
|
|
||||||
|
Some(html! {
|
||||||
|
(DOCTYPE)
|
||||||
|
meta charset="UTF-8";
|
||||||
|
title { (title) }
|
||||||
|
link rel="icon" type="image/svg+xml" href="/favicon.svg";
|
||||||
|
meta property="og:title" content=(title);
|
||||||
|
meta property="twitter:title" content=(title);
|
||||||
|
meta property="og:description" content={"Size: " (size) ", expires in " (expiry)};
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
#[serde(crate = "rocket::serde")]
|
#[serde(crate = "rocket::serde")]
|
||||||
pub struct ServerInfo {
|
pub struct ServerInfo {
|
||||||
|
|
|
@ -24,6 +24,7 @@ use rocket::{
|
||||||
fs, io::{AsyncSeekExt, AsyncWriteExt}
|
fs, io::{AsyncSeekExt, AsyncWriteExt}
|
||||||
}, Data, State
|
}, Data, State
|
||||||
};
|
};
|
||||||
|
use strings::{BreakStyle, TimeGranularity};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -47,7 +48,7 @@ pub fn home(settings: &State<Settings>) -> Markup {
|
||||||
button.button.{@if settings.duration.default == *d { "selected" }}
|
button.button.{@if settings.duration.default == *d { "selected" }}
|
||||||
data-duration-seconds=(d.num_seconds())
|
data-duration-seconds=(d.num_seconds())
|
||||||
{
|
{
|
||||||
(PreEscaped(to_pretty_time(d.num_seconds() as u32)))
|
(PreEscaped(to_pretty_time(d.num_seconds() as u32, BreakStyle::Break, TimeGranularity::Seconds)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,6 +84,7 @@ async fn main() {
|
||||||
confetti_box::websocket_upload,
|
confetti_box::websocket_upload,
|
||||||
endpoints::server_info,
|
endpoints::server_info,
|
||||||
endpoints::file_info,
|
endpoints::file_info,
|
||||||
|
endpoints::file_info_opengraph,
|
||||||
endpoints::lookup_mmid,
|
endpoints::lookup_mmid,
|
||||||
endpoints::lookup_mmid_noredir,
|
endpoints::lookup_mmid_noredir,
|
||||||
endpoints::lookup_mmid_name,
|
endpoints::lookup_mmid_name,
|
||||||
|
|
|
@ -35,7 +35,21 @@ pub fn parse_time_string(string: &str) -> Result<TimeDelta, Box<dyn Error>> {
|
||||||
Ok(final_time)
|
Ok(final_time)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pretty_time(seconds: u32) -> String {
|
pub enum BreakStyle {
|
||||||
|
Break,
|
||||||
|
Newline,
|
||||||
|
Space,
|
||||||
|
Nothing,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum TimeGranularity {
|
||||||
|
Days,
|
||||||
|
Hours,
|
||||||
|
Minutes,
|
||||||
|
Seconds,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_pretty_time(seconds: u32, breaks: BreakStyle, granularity: TimeGranularity) -> String {
|
||||||
let days = (seconds as f32 / 86400.0).floor();
|
let days = (seconds as f32 / 86400.0).floor();
|
||||||
let hour = ((seconds as f32 - (days * 86400.0)) / 3600.0).floor();
|
let hour = ((seconds as f32 - (days * 86400.0)) / 3600.0).floor();
|
||||||
let mins = ((seconds as f32 - (hour * 3600.0) - (days * 86400.0)) / 60.0).floor();
|
let mins = ((seconds as f32 - (hour * 3600.0) - (days * 86400.0)) / 60.0).floor();
|
||||||
|
@ -44,36 +58,68 @@ pub fn to_pretty_time(seconds: u32) -> String {
|
||||||
let days = if days == 0.0 {
|
let days = if days == 0.0 {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
} else if days == 1.0 {
|
} else if days == 1.0 {
|
||||||
days.to_string() + "<br>day"
|
days.to_string() + "\nday"
|
||||||
} else {
|
} else {
|
||||||
days.to_string() + "<br>days"
|
days.to_string() + "\ndays"
|
||||||
};
|
};
|
||||||
|
|
||||||
let hour = if hour == 0.0 {
|
let hour = if hour == 0.0 {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
} else if hour == 1.0 {
|
} else if hour == 1.0 {
|
||||||
hour.to_string() + "<br>hour"
|
hour.to_string() + "\nhour"
|
||||||
} else {
|
} else {
|
||||||
hour.to_string() + "<br>hours"
|
hour.to_string() + "\nhours"
|
||||||
};
|
};
|
||||||
|
|
||||||
let mins = if mins == 0.0 {
|
let mins = if mins == 0.0 {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
} else if mins == 1.0 {
|
} else if mins == 1.0 {
|
||||||
mins.to_string() + "<br>minute"
|
mins.to_string() + "\nminute"
|
||||||
} else {
|
} else {
|
||||||
mins.to_string() + "<br>minutes"
|
mins.to_string() + "\nminutes"
|
||||||
};
|
};
|
||||||
|
|
||||||
let secs = if secs == 0.0 {
|
let secs = if secs == 0.0 {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
} else if secs == 1.0 {
|
} else if secs == 1.0 {
|
||||||
secs.to_string() + "<br>second"
|
secs.to_string() + "\nsecond"
|
||||||
} else {
|
} else {
|
||||||
secs.to_string() + "<br>seconds"
|
secs.to_string() + "\nseconds"
|
||||||
};
|
};
|
||||||
|
|
||||||
(days + " " + &hour + " " + &mins + " " + &secs)
|
let mut out_string = match granularity {
|
||||||
.trim()
|
TimeGranularity::Days => days,
|
||||||
.to_string()
|
TimeGranularity::Hours => days + " " + &hour,
|
||||||
|
TimeGranularity::Minutes => days + " " + &hour + " " + &mins,
|
||||||
|
TimeGranularity::Seconds => days + " " + &hour + " " + &mins + " " + &secs,
|
||||||
|
}.trim().to_string();
|
||||||
|
|
||||||
|
match breaks {
|
||||||
|
BreakStyle::Break => out_string = out_string.replace("\n", "<br>"),
|
||||||
|
BreakStyle::Newline => (),
|
||||||
|
BreakStyle::Space => out_string = out_string.replace("\n", " "),
|
||||||
|
BreakStyle::Nothing => out_string = out_string.replace("\n", ""),
|
||||||
|
}
|
||||||
|
|
||||||
|
out_string
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_pretty_size(size: u64) -> String {
|
||||||
|
if size < 1000 {
|
||||||
|
size.to_string() + " B"
|
||||||
|
} else if size < 1000u64.pow(2) {
|
||||||
|
(size / 1000).to_string() + " kB"
|
||||||
|
} else if size < 1000u64.pow(3) {
|
||||||
|
(size / 1000u64.pow(2)).to_string() + " MB"
|
||||||
|
} else if size < 1000u64.pow(4) {
|
||||||
|
(size / 1000u64.pow(3)).to_string() + " GB"
|
||||||
|
} else if size < 1000u64.pow(5) {
|
||||||
|
(size as u128 / 1000u128.pow(4)).to_string() + " TB"
|
||||||
|
} else if size < 1000u64.pow(6) {
|
||||||
|
(size as u128 / 1000u128.pow(5)).to_string() + " PB"
|
||||||
|
} else if size < 1000u64.pow(7) {
|
||||||
|
(size as u128 / 1000u128.pow(6)).to_string() + " EB"
|
||||||
|
} else {
|
||||||
|
size.to_string() + " B"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue