diff --git a/Cargo.lock b/Cargo.lock index 7fd809b..cd65f09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1585,7 +1585,7 @@ dependencies = [ [[package]] name = "transbeam" -version = "0.1.0" +version = "0.2.0" dependencies = [ "actix", "actix-files", diff --git a/Cargo.toml b/Cargo.toml index 5c71047..9cd2f20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "transbeam" -version = "0.1.0" +version = "0.2.0" authors = ["xenofem "] edition = "2021" license = "MIT" diff --git a/README.md b/README.md index 3015747..00e4ab4 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ transbeam is configured with the following environment variables: (default: `./storage`) - `TRANSBEAM_STATIC_DIR`: path where the web app's static files live (default: `./static`) +- `TRANSBEAM_BASE_URL`: base URL for this transbeam instance, without + trailing `/` - `TRANSBEAM_PORT`: port to listen on localhost for http requests (default: 8080) - `TRANSBEAM_REVERSE_PROXY`: whether transbeam is running behind a diff --git a/src/main.rs b/src/main.rs index cf591ab..77f49ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ struct AppState { } struct Config { + base_url: String, max_upload_size: u64, max_lifetime: u16, upload_password: String, @@ -58,12 +59,14 @@ pub fn log_auth_failure(ip_addr: &str) { #[template(path = "index.html")] struct IndexPage { cachebuster: String, + base_url: String, } #[get("/")] async fn index(data: web::Data) -> impl Responder { IndexPage { cachebuster: data.config.cachebuster.clone(), + base_url: data.config.base_url.clone(), } } @@ -78,6 +81,7 @@ struct DownloadRequest { struct DownloadPage<'a> { info: DownloadInfo, cachebuster: &'a str, + base_url: &'a str, } #[derive(Serialize)] @@ -134,6 +138,7 @@ async fn handle_download( offsets, }, cachebuster: &data.config.cachebuster, + base_url: &data.config.base_url, } .to_response()) } @@ -175,6 +180,7 @@ async fn download_info( #[template(path = "404.html")] struct NotFoundPage<'a> { cachebuster: &'a str, + base_url: &'a str, } fn not_found(req: HttpRequest, data: web::Data, report: bool) -> actix_web::Result { @@ -184,6 +190,7 @@ fn not_found(req: HttpRequest, data: web::Data, report: bool) -> ac } let mut resp = NotFoundPage { cachebuster: &data.config.cachebuster, + base_url: &data.config.base_url, } .to_response(); *resp.status_mut() = StatusCode::NOT_FOUND; @@ -269,6 +276,14 @@ where .unwrap_or_else(default) } +fn env_or_panic(var: &str) -> T +where + ::Err: Debug, +{ + let val = std::env::var(var).unwrap_or_else(|_| panic!("{} must be set!", var)); + val.parse::().unwrap_or_else(|_| panic!("Invalid value {} for variable {}", val, var)) +} + #[actix_web::main] async fn main() -> std::io::Result<()> { dotenv::dotenv().ok(); @@ -276,6 +291,7 @@ async fn main() -> std::io::Result<()> { let static_dir: PathBuf = env_or_else("TRANSBEAM_STATIC_DIR", || PathBuf::from("static")); let storage_dir: PathBuf = env_or_else("TRANSBEAM_STORAGE_DIR", || PathBuf::from("storage")); + let base_url: String = env_or_panic("TRANSBEAM_BASE_URL"); let port: u16 = env_or("TRANSBEAM_PORT", 8080); let mnemonic_codes: bool = env_or("TRANSBEAM_MNEMONIC_CODES", true); let reverse_proxy: bool = env_or("TRANSBEAM_REVERSE_PROXY", true); @@ -284,13 +300,13 @@ async fn main() -> std::io::Result<()> { env_or::("TRANSBEAM_MAX_UPLOAD_SIZE", ByteSize(16 * bytesize::GB)).as_u64(); let max_storage_size: u64 = 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 upload_password: String = env_or_panic("TRANSBEAM_UPLOAD_PASSWORD"); 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?), config: Config { + base_url, max_upload_size, max_lifetime, upload_password, diff --git a/templates/404.html b/templates/404.html index 55979fa..aed17a9 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,6 +1,8 @@ {% extends "base.html" %} {% block title %}Download not found - transbeam{% endblock %} +{% block og_title %}Download not found{% endblock %} +{% block og_description %}{% endblock %} {% block head %} diff --git a/templates/base.html b/templates/base.html index 0bc7050..6c85945 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,12 +3,18 @@ - + {% block title %}transbeam{% endblock %} + + + + + + {% block head %}{% endblock %} diff --git a/templates/download.html b/templates/download.html index 4d1c6a6..bd2f7f0 100644 --- a/templates/download.html +++ b/templates/download.html @@ -2,6 +2,19 @@ {% block title %}{{ info.file.name }} - transbeam{% endblock %} +{% block og_title %}{{ info.file.name }}{% endblock %} +{% block og_description -%} + {% let formatted_total_size = bytesize::to_string(info.file.size.clone(), false).replace(" ", "") -%} + {% match info.file.contents -%} + {% when Some with (files) -%} + {{ files.len() }} files, {{ formatted_total_size }} total + {%- else -%} + {{ formatted_total_size }} + {%- endmatch %}, expires {{ info.file.expiry.format(DATE_DISPLAY_FORMAT).unwrap() }} +{%- endblock %} + +{% block relative_path %}download?code={{ info.code }}{% endblock %} + {% block head %}