jax-daemon 0.1.17

End-to-end encrypted storage buckets with peer-to-peer synchronization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use askama::Template;
use axum::response::{IntoResponse, Response};
use bytes::Bytes;
use common::linked_data::Hash;
use common::mount::{Mount, NodeLink};
use common::prelude::Mime;
use serde::Deserialize;

use super::transform::{TransformError, TransformParams};

/// Query parameters for file requests.
#[derive(Debug, Deserialize)]
pub struct FileQuery {
    /// If true, serve with Content-Disposition: attachment
    #[serde(default)]
    pub download: Option<bool>,
    /// If true, use the HTML viewer UI instead of serving raw file
    #[serde(default)]
    pub viewer: Option<bool>,
    /// Target width in pixels for image transforms.
    #[serde(default)]
    pub w: Option<u32>,
    /// Target height in pixels for image transforms.
    #[serde(default)]
    pub h: Option<u32>,
    /// Output quality 1-100 for JPEG/WebP.
    #[serde(default)]
    pub q: Option<u8>,
    /// Version hash for pinned browsing.
    #[serde(default)]
    pub at: Option<String>,
}

/// Template for file viewer.
#[derive(Template)]
#[template(path = "pages/gateway/viewer.html")]
pub struct GatewayViewerTemplate {
    pub bucket_id: String,
    pub bucket_id_short: String,
    pub bucket_name: String,
    pub bucket_link: String,
    pub bucket_link_short: String,
    pub file_path: String,
    pub file_name: String,
    pub mime_type: String,
    pub size_formatted: String,
    pub content: String,
    pub back_url: String,
}

/// Data that can be cached by the gateway layer.
pub struct CacheableData {
    pub link: Hash,
    pub data: Vec<u8>,
    pub mime_type: Mime,
}

/// Response from the file handler, with optional cacheable data.
pub struct FileResponse {
    pub response: Response,
    pub cacheable: Option<CacheableData>,
}

/// Cache-Control header for cached/transformed responses.
const CACHE_CONTROL_IMMUTABLE: &str = "public, max-age=31536000, immutable";

/// Serve cached content directly, bypassing all mount traversal and decryption.
pub fn serve_cached(data: Bytes, mime_type: &Mime, absolute_path: &str) -> Response {
    let filename = std::path::Path::new(absolute_path)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("file");

    (
        axum::http::StatusCode::OK,
        [
            (axum::http::header::CONTENT_TYPE, mime_type.to_string()),
            (
                axum::http::header::CONTENT_DISPOSITION,
                format!("inline; filename=\"{}\"", filename),
            ),
            (
                axum::http::header::CACHE_CONTROL,
                CACHE_CONTROL_IMMUTABLE.to_string(),
            ),
        ],
        data,
    )
        .into_response()
}

// TODO: Query param behavior is documented in templates/pages/gateway/index.html - keep in sync
pub async fn handler(
    mount: &Mount,
    path_buf: &std::path::Path,
    absolute_path: &str,
    query: &FileQuery,
    meta: &super::BucketMeta<'_>,
    node_link: NodeLink,
) -> FileResponse {
    let (content_link, file_metadata_data) = match &node_link {
        NodeLink::Data(link, _, metadata) => (link.hash(), metadata.clone()),
        _ => unreachable!("Already checked is_directory"),
    };

    let mime_type = file_metadata_data
        .mime()
        .cloned()
        .unwrap_or(mime::APPLICATION_OCTET_STREAM);

    let filename = path_buf
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("file")
        .to_string();

    let wants_download = query.download.unwrap_or(false);
    let wants_viewer = query.viewer.unwrap_or(false);

    // Validate transform params early
    let transform = TransformParams::from_query(query.w, query.h, query.q);
    if let Some(ref params) = transform {
        if let Err(msg) = params.validate() {
            return FileResponse {
                response: (axum::http::StatusCode::BAD_REQUEST, msg.to_string()).into_response(),
                cacheable: None,
            };
        }
    }

    // Read file data
    let file_data = match mount.cat(path_buf).await {
        Ok(data) => data,
        Err(e) => {
            tracing::error!("Failed to read file: {}", e);
            return FileResponse {
                response: super::error_response("Failed to read file"),
                cacheable: None,
            };
        }
    };

    // Apply image transform if requested and applicable
    let (file_data, mime_type) = if let Some(ref params) = transform {
        if TransformParams::is_transformable(&mime_type) {
            match super::transform::transform_image(&file_data, &mime_type, params) {
                Ok(transformed) => (transformed, mime_type),
                Err(TransformError::UnsupportedFormat(_)) => {
                    // Not an image type we can transform, serve as-is
                    (file_data, mime_type)
                }
                Err(e) => {
                    tracing::error!("Image transform failed: {}", e);
                    return FileResponse {
                        response: super::error_response("Image transform failed"),
                        cacheable: None,
                    };
                }
            }
        } else {
            // Non-image: ignore transform params, serve normally
            (file_data, mime_type)
        }
    } else {
        (file_data, mime_type)
    };

    let mime_str = mime_type.to_string();
    let size_formatted = format_bytes(file_data.len());

    // If download is requested, serve with attachment disposition (not cacheable)
    if wants_download {
        return FileResponse {
            response: (
                axum::http::StatusCode::OK,
                [
                    (axum::http::header::CONTENT_TYPE, mime_str.as_str()),
                    (
                        axum::http::header::CONTENT_DISPOSITION,
                        &format!("attachment; filename=\"{}\"", filename),
                    ),
                ],
                file_data,
            )
                .into_response(),
            cacheable: None,
        };
    }

    // When viewer is NOT explicitly set, act like a web server:
    // - Render HTML/Markdown/CSV with URL rewriting
    // - Serve other files raw inline
    if query.viewer.is_none() {
        let is_html = mime_type == mime::TEXT_HTML;
        let is_markdown = mime_type.essence_str() == "text/markdown";
        let is_csv = mime_type.essence_str() == "text/csv";
        let at_hash = query.at.as_deref();

        if is_html || is_markdown || is_csv {
            let (final_content, final_mime_type) = if is_markdown {
                let content_str = String::from_utf8_lossy(&file_data);
                let html = super::rewrite::markdown_to_html(&content_str);
                let rewritten = super::rewrite::rewrite_relative_urls(
                    &html,
                    absolute_path,
                    meta.id,
                    meta.host,
                    at_hash,
                );
                (rewritten.into_bytes(), "text/html; charset=utf-8")
            } else if is_csv {
                let content_str = String::from_utf8_lossy(&file_data);
                let html = super::rewrite::csv_to_html(
                    &content_str,
                    absolute_path,
                    meta.id,
                    meta.host,
                    at_hash,
                );
                (html.into_bytes(), "text/html; charset=utf-8")
            } else {
                let content_str = String::from_utf8_lossy(&file_data);
                let rewritten = super::rewrite::rewrite_relative_urls(
                    &content_str,
                    absolute_path,
                    meta.id,
                    meta.host,
                    at_hash,
                );
                (rewritten.into_bytes(), "text/html; charset=utf-8")
            };

            // Rewritten content is not cacheable (host-dependent)
            return FileResponse {
                response: (
                    axum::http::StatusCode::OK,
                    [
                        (axum::http::header::CONTENT_TYPE, final_mime_type),
                        (
                            axum::http::header::CONTENT_DISPOSITION,
                            &format!("inline; filename=\"{}\"", filename),
                        ),
                    ],
                    final_content,
                )
                    .into_response(),
                cacheable: None,
            };
        }

        // Non-HTML/Markdown: serve raw and cache
        let cacheable = Some(CacheableData {
            link: content_link,
            data: file_data.clone(),
            mime_type: mime_type.clone(),
        });

        return FileResponse {
            response: (
                axum::http::StatusCode::OK,
                [
                    (axum::http::header::CONTENT_TYPE, mime_str.clone()),
                    (
                        axum::http::header::CONTENT_DISPOSITION,
                        format!("inline; filename=\"{}\"", filename),
                    ),
                    (
                        axum::http::header::CACHE_CONTROL,
                        CACHE_CONTROL_IMMUTABLE.to_string(),
                    ),
                ],
                file_data,
            )
                .into_response(),
            cacheable,
        };
    }

    // viewer=false: serve raw file inline (with CSV URL rewriting)
    if !wants_viewer {
        let is_csv = mime_type.essence_str() == "text/csv";

        if is_csv {
            let content_str = String::from_utf8_lossy(&file_data);
            let rewritten = super::rewrite::rewrite_csv_urls(
                &content_str,
                absolute_path,
                meta.id,
                meta.host,
                query.at.as_deref(),
            );
            // CSV with rewriting is not cacheable (host-dependent)
            return FileResponse {
                response: (
                    axum::http::StatusCode::OK,
                    [
                        (axum::http::header::CONTENT_TYPE, "text/csv; charset=utf-8"),
                        (
                            axum::http::header::CONTENT_DISPOSITION,
                            &format!("inline; filename=\"{}\"", filename),
                        ),
                    ],
                    rewritten.into_bytes(),
                )
                    .into_response(),
                cacheable: None,
            };
        }

        let cacheable = Some(CacheableData {
            link: content_link,
            data: file_data.clone(),
            mime_type: mime_type.clone(),
        });

        return FileResponse {
            response: (
                axum::http::StatusCode::OK,
                [
                    (axum::http::header::CONTENT_TYPE, mime_str.clone()),
                    (
                        axum::http::header::CONTENT_DISPOSITION,
                        format!("inline; filename=\"{}\"", filename),
                    ),
                    (
                        axum::http::header::CACHE_CONTROL,
                        CACHE_CONTROL_IMMUTABLE.to_string(),
                    ),
                ],
                file_data,
            )
                .into_response(),
            cacheable,
        };
    }

    // Render file viewer template (not cacheable)
    let content = if mime_type.type_() == mime::TEXT
        || mime_type == mime::APPLICATION_JSON
        || mime_type.essence_str() == "application/xml"
        || mime_type == mime::APPLICATION_JAVASCRIPT
    {
        String::from_utf8_lossy(&file_data).to_string()
    } else {
        to_hex_dump(&file_data, 1024)
    };

    let back_url = format!("/gw/{}{}", meta.id, get_parent_path(absolute_path));

    let template = GatewayViewerTemplate {
        bucket_id: meta.id_str.to_string(),
        bucket_id_short: meta.id_short.to_string(),
        bucket_name: meta.name.to_string(),
        bucket_link: meta.link.to_string(),
        bucket_link_short: meta.link_short.to_string(),
        file_path: absolute_path.to_string(),
        file_name: filename,
        mime_type: mime_str,
        size_formatted,
        content,
        back_url,
    };

    FileResponse {
        response: match template.render() {
            Ok(html) => (
                axum::http::StatusCode::OK,
                [(axum::http::header::CONTENT_TYPE, "text/html; charset=utf-8")],
                html,
            )
                .into_response(),
            Err(e) => {
                tracing::error!("Failed to render viewer template: {}", e);
                super::error_response("Failed to render page")
            }
        },
        cacheable: None,
    }
}

fn format_bytes(bytes: usize) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];

    if bytes == 0 {
        return "0 B".to_string();
    }

    let bytes_f64 = bytes as f64;
    let k = 1024_f64;
    let i = (bytes_f64.log(k).floor() as usize).min(UNITS.len() - 1);
    let size = bytes_f64 / k.powi(i as i32);

    format!("{:.2} {}", size, UNITS[i])
}

fn get_parent_path(path: &str) -> String {
    if path == "/" {
        return "/".to_string();
    }

    let trimmed = path.trim_end_matches('/');
    match trimmed.rfind('/') {
        Some(0) => "/".to_string(),
        Some(pos) => trimmed[..pos].to_string(),
        None => "/".to_string(),
    }
}

fn to_hex_dump(data: &[u8], max_bytes: usize) -> String {
    let bytes_to_show = data.len().min(max_bytes);
    let mut result = String::new();

    for (i, chunk) in data[..bytes_to_show].chunks(16).enumerate() {
        result.push_str(&format!("{:08x}  ", i * 16));

        for (j, byte) in chunk.iter().enumerate() {
            result.push_str(&format!("{:02x} ", byte));
            if j == 7 {
                result.push(' ');
            }
        }

        for j in chunk.len()..16 {
            result.push_str("   ");
            if j == 7 {
                result.push(' ');
            }
        }

        result.push(' ');

        for byte in chunk {
            if *byte >= 32 && *byte < 127 {
                result.push(*byte as char);
            } else {
                result.push('.');
            }
        }

        result.push('\n');
    }

    if data.len() > max_bytes {
        result.push_str(&format!("\n... ({} more bytes)\n", data.len() - max_bytes));
    }

    result
}