pub const CONTENT_LINKS_UNAVAILABLE: &str = "links to content are not available here";
pub const CONTENT_LINK_TTL_SECS: u64 = 15 * 60;
pub const MAX_CONTENT_LINK_FILES: usize = 65_535;
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContentLinkFile {
pub path: String,
pub hash: String,
}
fn is_hash(hash: &str) -> bool {
hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit())
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContentLinkAsk {
pub name: String,
pub files: Vec<ContentLinkFile>,
pub zip: bool,
}
impl ContentLinkAsk {
pub fn check(&self) -> Result<(), String> {
if self.name.trim().is_empty() || self.name.contains(['/', '\\', '\0']) {
return Err(format!("{:?} is no name to save under", self.name));
}
if !self.zip && self.files.len() != 1 {
return Err("a link to more than one file is a zip".into());
}
if self.files.len() > MAX_CONTENT_LINK_FILES {
return Err(format!(
"a link names at most {MAX_CONTENT_LINK_FILES} files"
));
}
for file in &self.files {
let safe = !file.path.is_empty()
&& !file.path.starts_with('/')
&& !file.path.contains(['\\', '\0'])
&& file
.path
.split('/')
.all(|part| !part.is_empty() && part != "." && part != "..");
if !safe {
return Err(format!("{:?} is no path in a zip", file.path));
}
if !is_hash(&file.hash) {
return Err(format!("{:?} is no content hash", file.hash));
}
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContentLink {
pub path: String,
pub expires_in_secs: u64,
}
#[must_use]
pub fn viewable(name: &str) -> Option<&'static str> {
let (_, extension) = name.rsplit_once('.')?;
Some(match extension.to_ascii_lowercase().as_str() {
"mp4" | "m4v" => "video/mp4",
"mov" => "video/quicktime",
"webm" => "video/webm",
"ogv" => "video/ogg",
"mp3" => "audio/mpeg",
"m4a" => "audio/mp4",
"wav" => "audio/wav",
"ogg" | "oga" | "opus" => "audio/ogg",
"flac" => "audio/flac",
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"gif" => "image/gif",
"webp" => "image/webp",
"avif" => "image/avif",
"txt" => "text/plain; charset=utf-8",
_ => return None,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn file(path: &str) -> ContentLinkFile {
ContentLinkFile {
path: path.into(),
hash: "ab".repeat(32),
}
}
#[test]
fn a_link_names_a_file_or_a_zip_of_safe_paths() {
let one = ContentLinkAsk {
name: "plan.md".into(),
files: vec![file("plan.md")],
zip: false,
};
assert!(one.check().is_ok());
let folder = ContentLinkAsk {
name: "Plans.zip".into(),
files: vec![file("Plans/a.md"), file("Plans/2026/b.csv")],
zip: true,
};
assert!(folder.check().is_ok());
let empty_folder = ContentLinkAsk {
files: Vec::new(),
..folder.clone()
};
assert!(
empty_folder.check().is_ok(),
"an empty folder is an empty zip"
);
let two_unzipped = ContentLinkAsk {
zip: false,
..folder.clone()
};
assert!(two_unzipped.check().is_err());
for path in ["../etc/passwd", "/abs", "a//b", "a/./b", "a\\b", ""] {
let bad = ContentLinkAsk {
files: vec![file(path)],
..folder.clone()
};
assert!(bad.check().is_err(), "{path:?}");
}
let unnamed = ContentLinkAsk {
name: "a/b.zip".into(),
..folder.clone()
};
assert!(unnamed.check().is_err());
let not_a_hash = ContentLinkAsk {
files: vec![ContentLinkFile {
path: "a".into(),
hash: "tilo.content:xyz".into(),
}],
..folder
};
assert!(not_a_hash.check().is_err());
}
}