From de44b40fa4fa7b3408f93c8e564ee36da8da158f Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 1 Sep 2023 02:50:53 +0100 Subject: [PATCH] Allow ?raw=true to be passed to blob view Fixes #25 --- src/git.rs | 7 ++++++- src/methods/repo/tree.rs | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/git.rs b/src/git.rs index 97fe616..e9699d9 100644 --- a/src/git.rs +++ b/src/git.rs @@ -80,6 +80,7 @@ impl OpenRepository { path: Option, tree_id: Option<&str>, branch: Option, + formatted: bool, ) -> Result { let tree_id = tree_id .map(Oid::from_str) @@ -119,7 +120,11 @@ impl OpenRepository { .extension() .or_else(|| path.file_name()) .map_or_else(|| Cow::Borrowed(""), OsStr::to_string_lossy); - let content = format_file(blob.content(), &extension, &self.git.syntax_set)?; + let content = if formatted { + format_file(blob.content(), &extension, &self.git.syntax_set)? + } else { + String::from_utf8_lossy(blob.content()).to_string() + }; return Ok(PathDestination::File(FileWithContent { metadata: File { diff --git a/src/methods/repo/tree.rs b/src/methods/repo/tree.rs index 0101353..06d2e3c 100644 --- a/src/methods/repo/tree.rs +++ b/src/methods/repo/tree.rs @@ -4,7 +4,7 @@ use std::{ }; use askama::Template; -use axum::{extract::Query, response::Response, Extension}; +use axum::{extract::Query, http, response::IntoResponse, response::Response, Extension}; use serde::Deserialize; use crate::{ @@ -22,6 +22,8 @@ pub struct UriQuery { id: Option, #[serde(rename = "h")] branch: Option, + #[serde(default)] + raw: bool, } impl Display for UriQuery { @@ -68,10 +70,23 @@ pub async fn handle( Ok( match open_repo - .path(child_path, query.id.as_deref(), query.branch.clone()) + .path( + child_path, + query.id.as_deref(), + query.branch.clone(), + !query.raw, + ) .await? { PathDestination::Tree(items) => into_response(&TreeView { repo, items, query }), + PathDestination::File(file) if query.raw => { + let headers = [( + http::header::CONTENT_TYPE, + http::HeaderValue::from_static("text/plain"), + )]; + + (headers, file.content).into_response() + } PathDestination::File(file) => into_response(&FileView { repo, file }), }, ) -- libgit2 1.7.2