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
|
//! digital garden/wiki
//!
//!
mod api;
pub mod index;
mod markup;
pub use markup::PageFrontmatter;
use std::{str::FromStr, sync::Arc};
use axum::{
Extension, Router,
body::Body,
extract::{Path, RawQuery},
http::{StatusCode, header},
response::Response,
routing::{get, post},
};
use bempline::{Document, Options, options::IncludeMethod};
use camino::Utf8PathBuf;
use markup::PageTemplate;
use sessionhash::RequestContext;
use somethings::auth::Session;
use wikarden::{PathType, Wikarden};
use crate::{
Query, RuntimeError,
config::Settings,
extractor::Wrap,
fs::{self, RequestPath, SafePath},
handler::{quark::markup::Source, temporary_redirect},
state::SplormState,
};
/// nested `/quark`
///
/// returns quark related routes
pub fn routes() -> Router<SplormState> {
Router::new()
.route("/api/create/section", post(api::create_section))
.route("/api/create", post(api::create))
.route("/api/edit/section", post(api::edit_section))
.route("/api/edit", post(api::edit))
.route("/api/move", post(api::move_page))
.route("/api/delete", post(api::delete))
.route("/", get(index::index))
.route("/{*name}", get(page))
}
/// redirect `/quark` to `/quark/`
///
/// if I didn't do this, the `/quark` nesting didn't catch `/quark/`,
/// for some reason, and it went down [`handler::static_path()`]
pub async fn redirect_index_not_directory() -> Result<Response, RuntimeError> {
super::temporary_redirect("quark/".to_owned()).await
}
/// GET /quark/{*name}
async fn page(
Extension(wikarden): Extension<Arc<Wikarden>>,
Extension(settings): Extension<Settings>,
Path(name): Path<String>,
maybe_session: Option<Wrap<Session>>,
ctx: RequestContext,
RawQuery(rquery): RawQuery,
) -> Result<Response, RuntimeError> {
tracing::info!("{ctx} GET /quark/{name}");
if let Some(rq) = rquery {
if let Ok(q) = Query::from_str(&rq) {
if maybe_session.is_none() {
return Err(RuntimeError::Generic403);
}
if q.has("edit") {
return map_edit(name, &wikarden, settings).await;
}
if q.has("move") {
return page_move(name, &wikarden, settings).await;
}
if q.has("delete") {
return page_delete(name, settings).await;
}
}
}
// If we're serving the index, redirect there
if name == settings.wikarden_index() {
return temporary_redirect(format!(".")).await;
}
// Figure out if we're a Page or a Section. If we're a Section, check for a
// section home and redirect there if we have one
let resolved_path = match wikarden.path_type(&name).await {
Some(PathType::Page) => name,
Some(PathType::Section) => {
let data = wikarden.get_section_data(&name).await?;
if let Some(home) = data.home() {
return temporary_redirect(home.to_string()).await;
} else {
name
}
}
None => name,
};
PageTemplate::new(&settings, &wikarden, &resolved_path)
.set_with_tools(maybe_session.is_some())
.set_can_be_deleted(true)
.set_can_be_moved(true)
.rendered_response()
.await
}
async fn map_edit(
page: String,
wik: &Wikarden,
settings: Settings,
) -> Result<Response, RuntimeError> {
match wik.path_type(&page).await {
Some(PathType::Page) => page_edit(page, wik, settings).await,
Some(PathType::Section) => section_edit(page, wik, settings).await,
None => Err(RuntimeError::Generic404),
}
}
/// handles page edits
///
/// Returns a `Response` of the editing page
///
/// [`page()`] calls into this if the `"edit"` query boolean-parameter is
/// present and if authentication is successful.
async fn page_edit(
page: String,
wik: &Wikarden,
settings: Settings,
) -> Result<Response, RuntimeError> {
let template_path = settings.quark_template_dir().join("minimal.html");
let mut template = open_template_path(template_path, &settings).await?;
let rp = RequestPath::verify_safe(settings.root(), "quark/edit.html")?;
let edit = fs::read_file_to_string(rp.system_path()).await?;
let mut page_template = open_template(&edit, rp.system_path(), &settings).await?;
let src = match Source::from_wik(wik, &page).await {
//FIXME: Make a variant for unfound wiki pages
Err(_e) => return Err(RuntimeError::Generic404),
Ok(src) => src,
};
page_template.set("page-editing", page);
page_template.set("page-content", src.content());
match src.frontmatter().description() {
Some(desc) => page_template.set("og-description", desc),
None => page_template.set("og-description", ""),
}
template.set("mainClass", "main-create");
template.set("quarkForm", "true");
template.set("main", page_template.compile());
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html")
.body(Body::from(template.compile()))
.map_err(|_| RuntimeError::Unknown)
}
async fn page_move(
page: String,
wik: &Wikarden,
settings: Settings,
) -> Result<Response, RuntimeError> {
let template_path = settings.quark_template_dir().join("minimal.html");
let mut template = open_template_path(template_path, &settings).await?;
let rp = RequestPath::verify_safe(settings.root(), "quark/move.html")?;
let edit = fs::read_file_to_string(rp.system_path()).await?;
let mut page_template = open_template(&edit, rp.system_path(), &settings).await?;
if !wik.page_exists(&page).await? {
//FIXME: Make a variant for unfound wiki pages
return Err(RuntimeError::Generic404);
};
page_template.set("page-moving", page);
template.set("mainClass", "main-create");
template.set("quarkForm", "true");
template.set("main", page_template.compile());
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html")
.body(Body::from(template.compile()))
.map_err(|_| RuntimeError::Unknown)
}
/// handles page deletion
///
/// Returns a `Response` of the deletion confirmation page
///
/// [`page()`] calls into this if the `"delete"` query boolean-parameter is
/// present and if authentication is successful
async fn page_delete(page: String, settings: Settings) -> Result<Response, RuntimeError> {
let template_path = settings.quark_template_dir().join("minimal.html");
let mut template = open_template_path(template_path, &settings).await?;
let rp = RequestPath::verify_safe(settings.root(), "quark/delete.html")?;
let delete = fs::read_file_to_string(rp.system_path()).await?;
let mut page_template = open_template(&delete, rp.system_path(), &settings).await?;
page_template.set("page", page);
template.set("mainClass", "main-create");
template.set("quarkForm", "true");
template.set("main", page_template.compile());
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html")
.body(Body::from(template.compile()))
.map_err(|_| RuntimeError::Unknown)
}
async fn section_edit(
page: String,
wik: &Wikarden,
settings: Settings,
) -> Result<Response, RuntimeError> {
let template_path = settings.quark_template_dir().join("minimal.html");
let mut template = open_template_path(template_path, &settings).await?;
let rp = RequestPath::verify_safe(settings.root(), "quark/edit-section.html")?;
let edit = fs::read_file_to_string(rp.system_path()).await?;
let mut page_template = open_template(&edit, rp.system_path(), &settings).await?;
let data = match wik.get_section_data(&page).await {
//FIXME: Make a variant for unfound wiki pages
Err(_e) => return Err(RuntimeError::Generic404),
Ok(data) => data,
};
page_template.set("section-editing", page);
page_template.set(
"section-home",
data.home().map(|w| w.to_string()).unwrap_or_default(),
);
template.set("mainClass", "main-create");
template.set("quarkForm", "true");
template.set("main", page_template.compile());
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html")
.body(Body::from(template.compile()))
.map_err(|_| RuntimeError::Unknown)
}
/// gets quark template from the filesystem
///
/// assumes quark templates are in the template directory under the `quark` sub-directory
pub async fn open_template_path<P: Into<Utf8PathBuf>>(
path: P,
settings: &Settings,
) -> Result<Document, RuntimeError> {
let path = path.into();
let str = fs::unsafe_read_file_to_string(path.clone()).await?;
let options = Options::new().include_path(IncludeMethod::Path(
settings.quark_template_dir().into_std_path_buf(),
));
Document::from_str(str, options).map_err(|perr| RuntimeError::template_parse_error(perr, path))
}
/// creates quark template from a `&str`
pub async fn open_template<'p>(
content: &str,
path: SafePath<'p>,
settings: &Settings,
) -> Result<Document, RuntimeError> {
let options = Options::new().include_path(IncludeMethod::Path(
settings.quark_template_dir().into_std_path_buf(),
));
Document::from_str(content, options)
.map_err(|perr| RuntimeError::template_parse_error(perr, path.to_path_buf()))
}
|