From dc5cce23e9b0869455f546d968052bc200dd0011 Mon Sep 17 00:00:00 2001 From: Benjamin Morrison Date: Tue, 20 Jun 2023 23:29:12 -0400 Subject: init --- src/file.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/file.rs (limited to 'src/file.rs') diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..4500473 --- /dev/null +++ b/src/file.rs @@ -0,0 +1,73 @@ +/* Copyright (C) 2023 Ben Morrison + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use std::io; + +use tokio::fs; + +use crate::err::Supernova; +use crate::response; + +pub async fn get(path: &str) -> Result<(fs::File, String), Supernova> { + let metadata = match fs::metadata(path).await { + Ok(m) => m, + Err(e) => { + let code = match e.kind() { + io::ErrorKind::NotFound => response::Code::NotFound, + _ => response::Code::PermanentFailure, + }; + + let msg = format!("{}", e); + let wrapped_err = Supernova::boom(&msg).with_code(code); + + return Err(wrapped_err); + } + }; + + let path = if metadata.is_dir() { + format!("{}/index.gmi", path) + } else { + path.to_string() + }; + + let fd = match fs::File::open(&path).await { + Ok(fd) => fd, + Err(e) => { + let code = match e.kind() { + io::ErrorKind::NotFound => response::Code::NotFound, + _ => response::Code::PermanentFailure, + }; + + let msg = format!("{}", e); + let wrapped_err = Supernova::boom(&msg).with_code(code); + + return Err(wrapped_err); + } + }; + + let mime = match tree_magic_mini::from_filepath(path.as_ref()) { + Some(m) => { + if path.ends_with(".gmi") { + response::GEMINI_MIME + } else { + m + } + } + None => response::GEMINI_MIME, + }; + + Ok((fd, mime.to_string())) +} -- cgit 1.4.1