~garritfra/taurus

MIME type detection v2 APPLIED

Alexey Yerin: 1
 MIME type detection

 4 files changed, 53 insertions(+), 7 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~garritfra/taurus/patches/14905/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH v2] MIME type detection Export this patch

Signed-off-by: Alexey Yerin <yerinalexey98fd@gmail.com>
---
Replaced Result.or_else.unwrap with Result.unwrap_or_else

 Cargo.lock    | 32 ++++++++++++++++++++++++++++++++
 Cargo.toml    |  1 +
 src/gemini.rs |  4 ++--
 src/main.rs   | 23 ++++++++++++++++++-----
 4 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 9cb25fa..840e414 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -154,6 +154,22 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"

[[package]]
name = "mime"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"

[[package]]
name = "mime_guess"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212"
dependencies = [
 "mime",
 "unicase",
]

[[package]]
name = "native-tls"
version = "0.2.4"
@@ -373,6 +389,7 @@ version = "0.0.1"
dependencies = [
 "anyhow",
 "clap",
 "mime_guess",
 "native-tls",
 "serde",
 "toml",
@@ -417,6 +434,15 @@ dependencies = [
 "serde",
]

[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
 "version_check",
]

[[package]]
name = "unicode-bidi"
version = "0.3.4"
@@ -470,6 +496,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"

[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"

[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
diff --git a/Cargo.toml b/Cargo.toml
index 774fac0..8a04f33 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,3 +14,4 @@ serde = { version = "1.0.117", features = ["derive"] }
toml = "0.5.7"
anyhow = "1.0.34"
clap = "2.33.3"
mime_guess = "2.0.3"
diff --git a/src/gemini.rs b/src/gemini.rs
index e2367d5..d765021 100644
--- a/src/gemini.rs
+++ b/src/gemini.rs
@@ -38,10 +38,10 @@ pub struct GeminiResponse {
}

impl GeminiResponse {
    pub fn success(body: Vec<u8>) -> Self {
    pub fn success(body: Vec<u8>, mime_type: &str) -> Self {
        GeminiResponse {
            status: [b'2', b'0'],
            meta: b"text/gemini; charset=utf-8".to_vec(),
            meta: mime_type.as_bytes().to_vec(),
            body: Some(body),
        }
    }
diff --git a/src/main.rs b/src/main.rs
index 8ba1895..d2a1e84 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -99,15 +99,28 @@ fn read_file(file_path: &str) -> Result<Vec<u8>, io::Error> {
}

/// Send file as a response
fn write_file(path: &str) -> GeminiResponse {
fn write_file(path: &str) -> Result<GeminiResponse, String> {
    let extension = path::Path::new(path)
        .extension()
        .unwrap_or_else(|| std::ffi::OsStr::new(""))
        .to_str()
        .ok_or_else(|| "invalid Unicode".to_owned())?;

    let mime_type = match extension {
        "gmi" => "text/gemini; charset=utf-8",
        ext => mime_guess::from_ext(ext)
            .first_raw()
            .unwrap_or("text/plain"),
    };

    match read_file(path) {
        Ok(buf) => GeminiResponse::success(buf),
        Ok(buf) => Ok(GeminiResponse::success(buf, mime_type)),
        Err(err) => {
            // Cannot read file or it doesn't exist

            println!("Error [{}]: {}", path, err);

            GeminiResponse::not_found()
            Ok(GeminiResponse::not_found())
        }
    }
}
@@ -148,9 +161,9 @@ fn handle_client(mut stream: TlsStream<TcpStream>, static_root: &str) -> Result<
                    .ok_or("invalid Unicode".to_owned())?
                    .to_owned();

                write_file(&index_path).send(stream)
                write_file(&index_path)?.send(stream)
            } else {
                write_file(path.to_str().ok_or("invalid Unicode".to_owned())?).send(stream)
                write_file(path.to_str().ok_or("invalid Unicode".to_owned())?)?.send(stream)
            }
        } else {
            GeminiResponse::not_found().send(stream)
-- 
2.29.2
Applied, thanks!