cloudreve_api/api/v4/
uri.rs1pub const CLOUDREVE_URI_PREFIX: &str = "cloudreve://my/";
8
9pub fn path_to_uri(path: &str) -> String {
26 if path.starts_with("cloudreve://") {
28 return path.to_string();
29 }
30
31 let trimmed_path = path.strip_prefix('/').unwrap_or(path);
33
34 format!("{}{}", CLOUDREVE_URI_PREFIX, trimmed_path)
36}
37
38pub fn is_valid_uri(uri: &str) -> bool {
46 uri.starts_with(CLOUDREVE_URI_PREFIX) && uri.len() >= CLOUDREVE_URI_PREFIX.len()
47}
48
49pub fn uri_to_path(uri: &str) -> Result<&str, String> {
57 if !uri.starts_with(CLOUDREVE_URI_PREFIX) {
58 return Err(format!(
59 "Invalid Cloudreve URI: expected format 'cloudreve://my/...', got: {}",
60 uri
61 ));
62 }
63
64 let path = &uri[CLOUDREVE_URI_PREFIX.len()-1..];
65 Ok(path)
66}
67
68pub fn paths_to_uris(paths: &[&str]) -> Vec<String> {
76 paths.iter().map(|p| path_to_uri(p)).collect()
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_path_to_uri_absolute() {
85 assert_eq!(path_to_uri("/path/to/file.txt"), "cloudreve://my/path/to/file.txt");
86 }
87
88 #[test]
89 fn test_path_to_uri_relative() {
90 assert_eq!(path_to_uri("path/to/file.txt"), "cloudreve://my/path/to/file.txt");
91 }
92
93 #[test]
94 fn test_path_to_uri_already_uri() {
95 assert_eq!(
96 path_to_uri("cloudreve://my/path/to/file.txt"),
97 "cloudreve://my/path/to/file.txt"
98 );
99 }
100
101 #[test]
102 fn test_path_to_uri_root() {
103 assert_eq!(path_to_uri("/"), "cloudreve://my/");
104 }
105
106 #[test]
107 fn test_is_valid_uri() {
108 assert!(is_valid_uri("cloudreve://my/path/to/file.txt"));
109 assert!(is_valid_uri("cloudreve://my/"));
110 assert!(!is_valid_uri("/path/to/file.txt"));
111 assert!(!is_valid_uri("path/to/file.txt"));
112 assert!(!is_valid_uri("cloudreve://"));
113 }
114
115 #[test]
116 fn test_uri_to_path() {
117 assert_eq!(
118 uri_to_path("cloudreve://my/path/to/file.txt").unwrap(),
119 "path/to/file.txt"
120 );
121 assert_eq!(uri_to_path("cloudreve://my/").unwrap(), "");
122 }
123
124 #[test]
125 fn test_uri_to_path_invalid() {
126 assert!(uri_to_path("/path/to/file.txt").is_err());
127 assert!(uri_to_path("cloudreve://").is_err());
128 }
129
130 #[test]
131 fn test_paths_to_uris() {
132 let paths = vec!["/file1.txt", "file2.txt", "cloudreve://my/file3.txt"];
133 let uris = paths_to_uris(&paths);
134 assert_eq!(
135 uris,
136 vec![
137 "cloudreve://my/file1.txt",
138 "cloudreve://my/file2.txt",
139 "cloudreve://my/file3.txt"
140 ]
141 );
142 }
143}