Radish alpha
r
rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5
Radicle web interface
Radicle
Git
httpd: Infer mime type of file blob in `file_by_oid` handler
Sebastian Martinez committed 1 year ago
commit ef46915d7726757ec5ade3c2b08f257f9df10fe0
parent 061aaa1
3 files changed +33 -3
modified radicle-httpd/Cargo.lock
@@ -376,6 +376,17 @@ dependencies = [
]

[[package]]
+
name = "cfb"
+
version = "0.7.3"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f"
+
dependencies = [
+
 "byteorder",
+
 "fnv",
+
 "uuid",
+
]
+

+
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1006,6 +1017,15 @@ dependencies = [
]

[[package]]
+
name = "infer"
+
version = "0.16.0"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "bc150e5ce2330295b8616ce0e3f53250e53af31759a9dbedad1621ba29151847"
+
dependencies = [
+
 "cfb",
+
]
+

+
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1683,6 +1703,7 @@ dependencies = [
 "chrono",
 "flate2",
 "hyper",
+
 "infer",
 "lexopt",
 "lru",
 "nonempty",
@@ -2565,6 +2586,12 @@ dependencies = [
]

[[package]]
+
name = "uuid"
+
version = "1.12.1"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b"
+

+
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
modified radicle-httpd/Cargo.toml
@@ -27,6 +27,7 @@ base64 = { version = "0.22.1" }
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
flate2 = { version = "1" }
hyper = { version = "1.4", default-features = false }
+
infer = { version = "0.16.0" }
lexopt = { version = "0.3.0" }
lru = { version = "0.12.4" }
nonempty = { version = "0.9.0", features = ["serialize"] }
modified radicle-httpd/src/raw.rs
@@ -161,7 +161,7 @@ fn blob_response(
async fn file_by_oid_handler(
    Path((rid, oid)): Path<(RepoId, Oid)>,
    State(profile): State<Arc<Profile>>,
-
    Query(qs): Query<RawQuery>,
+
    Query(_qs): Query<RawQuery>,
) -> impl IntoResponse {
    let storage = &profile.storage;
    let repo = storage.repository(rid)?;
@@ -172,6 +172,8 @@ async fn file_by_oid_handler(
    }

    let blob = repo.blob(oid)?;
+
    let content = blob.content();
+
    let mime = infer::get(content).map(|i| i.mime_type().to_string());
    let mut response_headers = HeaderMap::new();

    if blob.size() > MAX_BLOB_SIZE {
@@ -180,10 +182,10 @@ async fn file_by_oid_handler(

    response_headers.insert(
        header::CONTENT_TYPE,
-
        HeaderValue::from_str(&qs.mime.unwrap_or("application/octet-stream".to_string()))?,
+
        HeaderValue::from_str(&mime.unwrap_or("application/octet-stream".to_string()))?,
    );

-
    Ok::<_, Error>((StatusCode::OK, response_headers, blob.content().to_vec()))
+
    Ok::<_, Error>((StatusCode::OK, response_headers, content.to_vec()))
}

#[cfg(test)]