diff --git a/flake.nix b/flake.nix index f6fdb6f..ece7d09 100644 --- a/flake.nix +++ b/flake.nix @@ -42,8 +42,7 @@ buildInputs = [ pkgs.makeWrapper ]; postBuild = '' wrapProgram $out/bin/${name} \ - --set TRANSBEAM_STATIC_DIR ${./static} \ - --set TRANSBEAM_CACHEBUSTER ${builtins.substring 0 8 (builtins.hashString "sha256" (toString ./static))} + --set TRANSBEAM_STATIC_DIR ${./static} ''; }; diff --git a/src/main.rs b/src/main.rs index 76ff8d9..2fa3456 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,9 @@ mod zip; use std::{fmt::Debug, path::PathBuf, str::FromStr}; +use actix_files::NamedFile; use actix_web::{ - error::InternalError, get, middleware::Logger, post, web, App, HttpRequest, + error::InternalError, get, http::StatusCode, middleware::Logger, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder, }; use actix_web_actors::ws; @@ -30,9 +31,9 @@ struct Config { max_lifetime: u16, upload_password: String, storage_dir: PathBuf, + static_dir: PathBuf, reverse_proxy: bool, mnemonic_codes: bool, - cachebuster: String, } pub fn get_ip_addr(req: &HttpRequest, reverse_proxy: bool) -> String { @@ -50,29 +51,14 @@ pub fn log_auth_failure(ip_addr: &str) { warn!("Incorrect authentication attempt from {}", ip_addr); } -#[derive(Template)] -#[template(path = "index.html")] -struct IndexPage<'a> { cachebuster: &'a str } - -#[get("/")] -async fn index(data: web::Data) -> impl Responder { - HttpResponse::Ok().body(IndexPage { cachebuster: &data.config.cachebuster }.render().unwrap()) -} - #[derive(Deserialize)] struct DownloadRequest { code: String, download: Option, } -#[derive(Template)] +#[derive(Serialize, Template)] #[template(path = "download.html")] -struct DownloadPage<'a> { - info: DownloadInfo, - cachebuster: &'a str, -} - -#[derive(Serialize)] struct DownloadInfo { file: StoredFile, code: String, @@ -119,14 +105,11 @@ async fn handle_download( } else { let offsets = info.contents.as_deref().map(zip::file_data_offsets); Ok(HttpResponse::Ok().body( - DownloadPage { - info: DownloadInfo { - file: info, - code: code.clone(), - available: file.metadata().await?.len(), - offsets, - }, - cachebuster: &data.config.cachebuster, + DownloadInfo { + file: info, + code: code.clone(), + available: file.metadata().await?.len(), + offsets, } .render() .unwrap(), @@ -166,10 +149,6 @@ async fn download_info( })) } -#[derive(Template)] -#[template(path = "404.html")] -struct NotFoundPage<'a> { cachebuster: &'a str } - fn not_found(req: HttpRequest, data: web::Data, report: bool) -> actix_web::Result { if report { let ip_addr = get_ip_addr(&req, data.config.reverse_proxy); @@ -177,7 +156,9 @@ fn not_found(req: HttpRequest, data: web::Data, report: bool) -> ac } Err(InternalError::from_response( "Download not found", - HttpResponse::NotFound().body(NotFoundPage { cachebuster: &data.config.cachebuster }.render().unwrap()), + NamedFile::open(data.config.static_dir.join("404.html"))? + .set_status_code(StatusCode::NOT_FOUND) + .into_response(&req), ) .into()) } @@ -277,7 +258,6 @@ async fn main() -> std::io::Result<()> { env_or::("TRANSBEAM_MAX_STORAGE_SIZE", ByteSize(64 * bytesize::GB)).as_u64(); let upload_password: String = std::env::var("TRANSBEAM_UPLOAD_PASSWORD").expect("TRANSBEAM_UPLOAD_PASSWORD must be set!"); - let cachebuster: String = env_or_else("TRANSBEAM_CACHEBUSTER", String::new); let data = web::Data::new(AppState { file_store: RwLock::new(FileStore::load(storage_dir.clone(), max_storage_size).await?), @@ -286,9 +266,9 @@ async fn main() -> std::io::Result<()> { max_lifetime, upload_password, storage_dir, + static_dir: static_dir.clone(), reverse_proxy, mnemonic_codes, - cachebuster, }, }); start_reaper(data.clone()); @@ -301,13 +281,12 @@ async fn main() -> std::io::Result<()> { } else { Logger::default() }) - .service(index) .service(handle_download) .service(download_info) .service(handle_upload) .service(check_upload_password) .service(upload_limits) - .service(actix_files::Files::new("/", static_dir.clone())) + .service(actix_files::Files::new("/", static_dir.clone()).index_file("index.html")) }); if reverse_proxy { diff --git a/static/404.html b/static/404.html new file mode 100644 index 0000000..bfc07c8 --- /dev/null +++ b/static/404.html @@ -0,0 +1,39 @@ + + + + + + + + + + + transbeam + + + +
+

The download code you entered wasn't found. The download may have expired.

+
+
+ +
+ +
+
+
+

< Back

+
+ + + diff --git a/static/css/colors.css b/static/css/colors.css index e9914ac..65bd95a 100644 --- a/static/css/colors.css +++ b/static/css/colors.css @@ -1,31 +1,35 @@ -:root { - --success-primary: #060; - --success-secondary: #0a0; - --failure-primary: #d00; - --failure-secondary: #f24; - --progress-primary: #27f; - --progress-secondary: #48f; - --progress-tertiary: #7af; - --border: #ddd; - --border-active: #777; - --icon-primary: #333; - --icon-primary-active: #000; - --icon-secondary: #888; - --icon-warning: #f00; - --icon-ready: #33f; - --icon-ready-active: #00b; - --icon-unavailable: #999; - --button-text: #000; - --button-background: #ccc; - --button-background-active: #aaa; - --button-border: #bbb; - --button-disabled-text: #666; - --button-disabled-background: #eee; - --button-disabled-border: #ddd; +@media not all and (prefers-color-scheme: dark) { + :root { + color-scheme: light; + --success-primary: #060; + --success-secondary: #0a0; + --failure-primary: #d00; + --failure-secondary: #f24; + --progress-primary: #27f; + --progress-secondary: #48f; + --progress-tertiary: #7af; + --border: #ddd; + --border-active: #777; + --icon-primary: #333; + --icon-primary-active: #000; + --icon-secondary: #888; + --icon-warning: #f00; + --icon-ready: #33f; + --icon-ready-active: #00b; + --icon-unavailable: #999; + --button-text: #000; + --button-background: #ccc; + --button-background-active: #aaa; + --button-border: #bbb; + --button-disabled-text: #666; + --button-disabled-background: #eee; + --button-disabled-border: #ddd; + } } @media (prefers-color-scheme: dark) { :root { + color-scheme: dark; --success-primary: #0d0; --success-secondary: #080; --failure-primary: #f34; diff --git a/static/css/transbeam.css b/static/css/transbeam.css index c0d48c9..92361c0 100644 --- a/static/css/transbeam.css +++ b/static/css/transbeam.css @@ -9,7 +9,6 @@ body { #header h1 { margin-top: 5px; - color: CanvasText; } #header a { diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..3719884 --- /dev/null +++ b/static/index.html @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + transbeam + + + +
+

Download

+
+
+ +
+ +
+
+ +
+

Uploading is currently closed.

+
+
+

Upload

+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ +
+
+ +
+
+
+
+
Download code:
+
+
Link copied!
+
+
+
+
+
+
+
+
+
+
+ +
+ +
+
+ + + diff --git a/templates/404.html b/templates/404.html deleted file mode 100644 index 55979fa..0000000 --- a/templates/404.html +++ /dev/null @@ -1,24 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Download not found - transbeam{% endblock %} - -{% block head %} - -{% endblock %} - -{% block body %} -
-

The download code you entered wasn't found. The download may have expired.

-
-
- -
- -
-
-
-

< Back

-
-{% endblock %} diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index 0bc7050..0000000 --- a/templates/base.html +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - {% block title %}transbeam{% endblock %} - {% block head %}{% endblock %} - - - - {% block body %}{% endblock %} - - - diff --git a/templates/download.html b/templates/download.html index 8675972..a834c2a 100644 --- a/templates/download.html +++ b/templates/download.html @@ -1,39 +1,52 @@ -{% extends "base.html" %} - -{% block title %}{{ info.file.name }} - transbeam{% endblock %} - -{% block head %} - - - -{% endblock %} - -{% block body %} -
-
{{ info.file.name }}
-
{{ bytesize::to_string(info.file.size.clone(), false).replace(" ", "") }}
-
-
- {% match info.file.contents %} - {% when Some with (files) %} -
-
- Show file list - - {% let offsets = info.offsets.as_ref().unwrap() %} - {% for f in files %} - - - - - - - {% endfor %} -
{{ bytesize::to_string(f.size.clone(), false).replace(" ", "") }}{{ f.name }}
-
-
- {% else %} - {% endmatch %} -{% endblock %} + + + + + + + + + + + + + {{ file.name }} - transbeam + + + +
+
{{ file.name }}
+
{{ bytesize::to_string(file.size.clone(), false).replace(" ", "") }}
+
+
+ {% match file.contents %} + {% when Some with (files) %} +
+
+ Show file list + + {% let offsets = offsets.as_ref().unwrap() %} + {% for f in files %} + + + + + + + {% endfor %} +
{{ bytesize::to_string(f.size.clone(), false).replace(" ", "") }}{{ f.name }}
+
+
+ {% else %} + {% endmatch %} + + + diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index 81b3612..0000000 --- a/templates/index.html +++ /dev/null @@ -1,87 +0,0 @@ -{% extends "base.html" %} - -{% block head %} - - - - -{% endblock %} - -{% block body_attrs %}class="noscript landing"{% endblock %} - -{% block body %} -
-

Download

-
-
- -
- -
-
- -
-

Uploading is currently closed.

-
-
-

Upload

-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
-
Download code:
-
-
Link copied!
-
-
-
-
-
-
-
-
-
-
- -
- -
-
-{% endblock %}