From 60f6d973c86e7c4662a7dfc99de11ebccd9b2277 Mon Sep 17 00:00:00 2001 From: blek Date: Thu, 2 Nov 2023 23:44:46 +1000 Subject: [PATCH] web UI for curlapi/help --- filed/src/web/curlapi/help.rs | 30 ++++++--- filed/src/web/pages.rs | 8 +++ filed/static/assets/code.css | 13 ++++ filed/templates/curlapi_help.html | 101 ++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 filed/static/assets/code.css create mode 100644 filed/templates/curlapi_help.html diff --git a/filed/src/web/curlapi/help.rs b/filed/src/web/curlapi/help.rs index 0480b8b..3bc652b 100644 --- a/filed/src/web/curlapi/help.rs +++ b/filed/src/web/curlapi/help.rs @@ -1,8 +1,18 @@ -use warp::{Filter, reply::Reply, reject::Rejection}; +use askama::Template; +use warp::{Filter, reply::{Reply, html}, reject::Rejection}; -use crate::web::state::SharedState; +use crate::web::{state::SharedState, pages::CurlHelpPage, rejection::HttpReject}; -pub async fn help(state: SharedState) -> Result { +pub async fn help(state: SharedState, ua: String) -> Result, Rejection> { + + if ! ua.starts_with("curl/") { + let page = CurlHelpPage { conf: state.config.clone(), env: state.env.clone() }; + return Ok( + Box::new( + html(page.render().map_err(|x| HttpReject::AskamaError(x))?) + ) + ) + } let brand = format!( "{} \x1b[1m{}\x1b[0m {}", @@ -45,19 +55,20 @@ the HTML used at the regular web UI form wrapped into this URL \x1b[1;32mIMPORTANT:\x1b[0m Read the terms of service \x1b[1mbefore\x1b[0m uploading the file! The ToS can be found here: \x1b[34m{instance}/tos\x1b[0m . -{warns} -" +{warns}" ); - Ok(format!(" + Ok( + Box::new( +format!(" \x1b[31m┓ ╻\x1b[0m \x1b[35m┏┓•┓ \x1b[0m \x1b[32m┣┓┃\x1b[0m \x1b[95m┣ ┓┃┏┓\x1b[0m \x1b[34m┗┛•\x1b[0m \x1b[35m┻ ┗┗┗━\x1b[0m {brand} - {help} -").into()) +").to_string()) +) } pub fn get_routes(state: SharedState) -> impl Filter + Clone { @@ -67,5 +78,8 @@ pub fn get_routes(state: SharedState) -> impl Filter("user-agent") + ) .and_then(help) } \ No newline at end of file diff --git a/filed/src/web/pages.rs b/filed/src/web/pages.rs index a0faf97..6b634e8 100644 --- a/filed/src/web/pages.rs +++ b/filed/src/web/pages.rs @@ -88,6 +88,14 @@ pub struct ErrorPage { pub link_text: Option } +#[derive(Template)] +#[template( path = "curlapi_help.html" )] +#[allow(dead_code)] +pub struct CurlHelpPage { + pub env: Env, + pub conf: Config +} + pub async fn uploaded(query: HashMap, state: SharedState) -> Result, Rejection> { if ! query.contains_key("file") { diff --git a/filed/static/assets/code.css b/filed/static/assets/code.css new file mode 100644 index 0000000..10adf06 --- /dev/null +++ b/filed/static/assets/code.css @@ -0,0 +1,13 @@ +.code { + display: block; + padding: 1em; + border: 1px solid var(--header-sec-color); + border-radius: 12px; + font-family: monospace; +} +.code-inline { + display: inline; + font-family: monospace; + background: #00000010; + padding: 2px 4px; +} \ No newline at end of file diff --git a/filed/templates/curlapi_help.html b/filed/templates/curlapi_help.html new file mode 100644 index 0000000..5a92970 --- /dev/null +++ b/filed/templates/curlapi_help.html @@ -0,0 +1,101 @@ + +{% extends "base.html" %} + +{% block head %} + + + + +{% endblock %} + +{% block body %} + +
+

Curl API

+

+ blek! File has an API for uploading files via cURL. + To upload a file via cURL, follow these instructions: +

+

+ To upload a file, POST it like this: + + Copy! + +

+
+ curl -X POST {{env.instanceurl}}/curlapi/upload -F'file=@file.txt' -F'tos_consent=on' +
+

+ To add a password, do it like this: + + Copy! + +

+
+ curl -X POST {{env.instanceurl}}/curlapi/upload -F'file=@file.txt' -F'filename=uwu' -F'tos_consent=on' -F'named=on' +
+

+ Note that the + named=on + switch is required. + Its needed because the curl API is basically a wrapper of + this + HTML form. +

+ +
+

+ Important +

+

+ Read the + Terms of Service + before + uploading a file. +
+ You agree to them by adding the + tos_consent=on + switch. +

+
+ +
+

Web UI

+
+

+ Hey, it looks like you are viewing this page from a browser!
+ You can use the Web UI as well to upload a file! +

+

+ + Go to the web UI + +

+
+
+
+ +{% endblock %} + +{% block scripts %} + + + +{% endblock %} \ No newline at end of file