Skip to main content

lockbook_server_lib/
static_files.rs

1use tracing::{Level, info, span};
2use warp::{Filter, http::Response, hyper::Body};
3
4const APPLE_APP_SITE_ASSOCIATION: &str = include_str!("../static/apple-app-site-association");
5
6pub fn static_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
7    open_route().or(well_known_route())
8}
9
10fn open_route() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
11    warp::path("open")
12        .and(warp::path::param::<String>())
13        .map(|uuid: String| {
14            let span = span!(Level::INFO, "matched_request", method = "GET", route = "/open");
15            let _enter = span.enter();
16
17            info!("external link routed");
18
19            let redirect_html = get_files_preview_html(&uuid);
20            warp::reply::html(redirect_html)
21        })
22}
23
24fn well_known_route() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
25    warp::path(".well-known")
26        .and(warp::path("apple-app-site-association"))
27        .map(|| {
28            Response::builder()
29                .header("Content-Type", "application/json")
30                .body(Body::from(APPLE_APP_SITE_ASSOCIATION))
31                .unwrap()
32        })
33}
34
35pub fn get_files_preview_html(uuid: &str) -> String {
36    format!(
37        r#"
38<!doctype html>
39<html lang="en">
40    <head>
41        <meta charset="UTF-8" />
42        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
43        <title>Open in Lockbook</title>
44        <script>
45            window.onload = function() {{
46                const url = "lb://{uuid}";
47
48                // Try to open the app
49                window.location.href = url;
50            }};
51        </script>
52    </head>
53    <body>
54        <h1>Opening Lockbook...</h1>
55        <p>If nothing happens, use the link below:</p>
56        <div>
57            <a href="lb://{uuid}">Open in App</a>
58        </div>
59    </body>
60</html>
61"#,
62        uuid = uuid
63    )
64}