commit 68b6d94110ba3c2ee68768608dc751503e017f30
parent 3273d86ab4b211eeffa4ecd4a0cab58f92119a2c
Author: lash <dev@holbrook.no>
Date: Sun, 3 Jul 2022 07:03:38 +0000
Add mime metadata store
Diffstat:
4 files changed, 85 insertions(+), 1 deletion(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -12,6 +12,12 @@ log = "^0.4"
sha2 = "^0.10"
hex = "^0.4"
tempfile = "^3.3.0"
+mime = "^0.3.13"
+
+[dependencies.tree_magic]
+version = "^0.2.3"
+features = ["cli"]
+optional = true
[dependencies.pgp]
version = "^0.7.2"
@@ -25,3 +31,5 @@ optional = true
[features]
pgpauth = ["pgp", "base64"]
dev = []
+magic = ["tree_magic"]
+meta = []
diff --git a/src/main.rs b/src/main.rs
@@ -7,6 +7,7 @@ use tiny_http::{
Header,
Method,
};
+use mime::Mime;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::str::FromStr;
use std::path::{PathBuf, Path};
@@ -48,6 +49,8 @@ use crate::auth::mock::auth_check as mock_auth_check;
#[cfg(feature = "pgpauth")]
use crate::auth::pgp::auth_check as pgp_auth_check;
+#[cfg(feature = "meta")]
+mod meta;
#[derive(Debug)]
pub struct NoAuthError;
@@ -206,6 +209,41 @@ fn process_request(req: &mut Request, f: &File) -> AuthResult {
}
}
+fn process_meta(req: &Request, path: &Path, digest: Vec<u8>) -> Option<Mime> {
+ let headers = req.headers();
+ let mut m: Option<mime::Mime> = None;
+
+ for h in headers {
+ let k = &h.field;
+ if k.equiv("Content-Type") {
+ let v = &h.value;
+ m = match Mime::from_str(v.as_str()) {
+ Err(e) => {
+ error!("invalid mime type");
+ return None;
+ },
+ Ok(v) => {
+ Some(v)
+ },
+ };
+ }
+ }
+
+ match m {
+ Some(v) => {
+ match meta::register_type(path, digest, v) {
+ Err(e) => {
+ error!("could not register content type: {}", &e);
+ },
+ _ => {},
+ };
+ },
+ _ => {},
+ };
+
+ None
+}
+
fn main() {
env_logger::init();
@@ -273,6 +311,15 @@ fn main() {
},
};
+ match &result.typ {
+ RequestResultType::Changed => {
+ let digest_hex = result.v.clone().unwrap();
+ let digest = hex::decode(&digest_hex).unwrap();
+ process_meta(&req, &path, digest);
+ },
+ _ => {},
+ }
+
exec_response(req, result);
}
}
diff --git a/src/meta.rs b/src/meta.rs
@@ -0,0 +1,30 @@
+use std::fs::File;
+use std::path::Path;
+use std::io::Write;
+use mime::Mime;
+
+
+pub fn register_type(path: &Path, digest: Vec<u8>, typ: Mime) -> Result<(), std::io::Error> {
+ let digest_hex = hex::encode(digest);
+ let fp = path.join(digest_hex);
+
+ let mut path_canon = match fp.canonicalize() {
+ Ok(v) => {
+ v
+ },
+ Err(e) => {
+ return Err(e);
+ }
+ };
+
+ path_canon.set_extension("meta");
+ match File::create(path_canon) {
+ Ok(mut f) => {
+ f.write(typ.as_ref().as_bytes());
+ }
+ Err(e) => {
+ return Err(e);
+ }
+ };
+ Ok(())
+}
diff --git a/src/record.rs b/src/record.rs
@@ -254,7 +254,6 @@ mod tests {
let mut r = read(immutable_path).unwrap();
assert_eq!(r, b.to_vec());
-
}
#[test]