bobashare_web/views/
upload.rs

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
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use chrono::TimeDelta;
use tracing::{event, instrument, Level};

use super::{filters, render_template, CurrentNavigation, ErrorResponse, TemplateState};
use crate::AppState;

#[derive(Debug, Clone)]
pub struct ExpiryUnit {
    pub name: &'static str,
    pub value: &'static str,
    pub default: bool,
    pub duration: TimeDelta,
}
pub fn iter_expiry_units() -> impl Iterator<Item = ExpiryUnit> {
    [
        ExpiryUnit {
            name: "seconds",
            value: "s",
            default: false,
            duration: TimeDelta::try_seconds(1).unwrap(),
        },
        ExpiryUnit {
            name: "minutes",
            value: "m",
            default: false,
            duration: TimeDelta::try_minutes(1).unwrap(),
        },
        ExpiryUnit {
            name: "hours",
            value: "h",
            default: false,
            duration: TimeDelta::try_hours(1).unwrap(),
        },
        ExpiryUnit {
            name: "days",
            value: "d",
            default: true,
            duration: TimeDelta::try_days(1).unwrap(),
        },
        ExpiryUnit {
            name: "weeks",
            value: "w",
            default: false,
            duration: TimeDelta::try_days(7).unwrap(),
        },
        ExpiryUnit {
            name: "months",
            value: "mon",
            default: false,
            duration: TimeDelta::try_days(30).unwrap(),
        },
        ExpiryUnit {
            name: "years",
            value: "y",
            default: false,
            duration: TimeDelta::try_days(365).unwrap(),
        },
    ]
    .into_iter()
}

#[derive(Template)]
#[template(path = "upload.html.jinja")]
pub struct UploadTemplate<'s> {
    pub state: TemplateState<'s>,
    // TODO: make this iterator and not vec
    pub expiry_units: Vec<ExpiryUnit>,
    pub never_expiry_allowed: bool,
}

#[instrument(skip(state))]
pub async fn upload(
    State(state): State<&'static AppState>,
) -> Result<impl IntoResponse, ErrorResponse> {
    let mut state = TemplateState::from(state);
    state.current_navigation = Some(CurrentNavigation::Upload);
    event!(Level::DEBUG, "returning upload template");
    render_template(UploadTemplate {
        expiry_units: iter_expiry_units()
            .take_while(|e| {
                if let Some(max) = state.max_expiry {
                    max >= e.duration
                } else {
                    true
                }
            })
            .collect(),
        never_expiry_allowed: state.max_expiry.is_none(),
        state,
    })
}

#[derive(Template)]
#[template(path = "paste.html.jinja")]
pub struct PasteTemplate<'s> {
    pub state: TemplateState<'s>,
    pub expiry_units: Vec<ExpiryUnit>,
    pub never_expiry_allowed: bool,
}

#[instrument(skip(state))]
pub async fn paste(
    State(state): State<&'static AppState>,
) -> Result<impl IntoResponse, ErrorResponse> {
    let mut state = TemplateState::from(state);
    state.current_navigation = Some(CurrentNavigation::Paste);
    event!(Level::DEBUG, "returning paste template");
    render_template(PasteTemplate {
        expiry_units: iter_expiry_units()
            .take_while(|e| {
                if let Some(max) = state.max_expiry {
                    max >= e.duration
                } else {
                    true
                }
            })
            .collect(),
        never_expiry_allowed: state.max_expiry.is_none(),
        state,
    })
}