tangled
alpha
login
or
join now
vielle.dev
/
atcities.dev
8
fork
atom
[WIP] A (somewhat barebones) atproto app for creating custom sites without hosting!
8
fork
atom
overview
issues
pulls
pipelines
upload: add the utility rkey <=> url functions
vielle.dev
5 months ago
deae99f7
7a1053cf
verified
This commit was signed with the committer's
known signature
.
vielle.dev
SSH Key Fingerprint:
SHA256:/4bvxqoEh9iMdjAPgcgAgXKZZQTROL3ULiPt6nH9RSs=
+65
2 changed files
expand all
collapse all
unified
split
upload
src
main.rs
utils.rs
+1
upload/src/main.rs
···
2
2
use std::path::PathBuf;
3
3
4
4
mod sitemap;
5
5
+
mod utils;
5
6
6
7
#[derive(Parser, Debug)]
7
8
#[command(version, about, long_about = None)]
+64
upload/src/utils.rs
···
1
1
+
use regex::Regex;
2
2
+
3
3
+
pub fn rkey_to_url(rkey: String) -> Option<String> {
4
4
+
let regex = Regex::new(
5
5
+
// symbols A-Za-z0-9 -._~: are all valid rkey characters
6
6
+
// however we use a subset of record keys where : is an escape character
7
7
+
// allow any rkey character except colons
8
8
+
// or any of the valid escape sequences
9
9
+
// from start of string to end between 0 and unlimited times
10
10
+
r"^(?:[A-Za-z0-9.\-_~]|(?:::)|(?::~)|(?::21)|(?::24)|(?::26)|(?::27)|(?::28)|(?::29)|(?::2A)|(?::2B)|(?::2C)|(?::3A)|(?::3B)|(?::3D)|(?::40))*$",
11
11
+
)
12
12
+
.expect("Regex failed to generate");
13
13
+
14
14
+
if !regex.is_match(&rkey) {
15
15
+
return None;
16
16
+
};
17
17
+
18
18
+
let res = rkey
19
19
+
.replace("::", "/")
20
20
+
.replace(":~", "%")
21
21
+
.replace(":21", "!")
22
22
+
.replace(":24", "$")
23
23
+
.replace(":26", "&")
24
24
+
.replace(":27", "'")
25
25
+
.replace(":28", "(")
26
26
+
.replace(":29", ")")
27
27
+
.replace(":2A", "*")
28
28
+
.replace(":2B", "+")
29
29
+
.replace(":2C", ",")
30
30
+
.replace(":3A", ":")
31
31
+
.replace(":3B", ";")
32
32
+
.replace(":3D", "=")
33
33
+
.replace(":40", "@");
34
34
+
35
35
+
return Some(res);
36
36
+
}
37
37
+
38
38
+
pub fn url_to_rkey(url: String) -> Option<String> {
39
39
+
let regex = Regex::new(r"^(?:[a-zA-Z0-9/\-._~!$&'()*+,;=:@]|(?:%[0-9a-fA-F]{2}))*$")
40
40
+
.expect("Regex failed to generate");
41
41
+
if !regex.is_match(&url) {
42
42
+
return None;
43
43
+
}
44
44
+
45
45
+
let res = url
46
46
+
// : replace is hoisted so it doesnt replace colons from elsewhere
47
47
+
.replace(":", ":3A")
48
48
+
.replace("/", "::")
49
49
+
.replace("%", ":~")
50
50
+
.replace("!", ":21")
51
51
+
.replace("$", ":24")
52
52
+
.replace("&", ":26")
53
53
+
.replace("'", ":27")
54
54
+
.replace("(", ":28")
55
55
+
.replace(")", ":29")
56
56
+
.replace("*", ":2A")
57
57
+
.replace("+", ":2B")
58
58
+
.replace(",", ":2C")
59
59
+
.replace(";", ":3B")
60
60
+
.replace("=", ":3D")
61
61
+
.replace("@", ":40");
62
62
+
63
63
+
return Some(res);
64
64
+
}