Skip to content

Commit

Permalink
Allow ?raw=true to be passed to blob view
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
w4 committed Sep 1, 2023
1 parent 89ff469 commit de44b40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl OpenRepository {
path: Option<PathBuf>,
tree_id: Option<&str>,
branch: Option<String>,
formatted: bool,
) -> Result<PathDestination> {
let tree_id = tree_id
.map(Oid::from_str)
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 17 additions & 2 deletions src/methods/repo/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -22,6 +22,8 @@ pub struct UriQuery {
id: Option<String>,
#[serde(rename = "h")]
branch: Option<String>,
#[serde(default)]
raw: bool,
}

impl Display for UriQuery {
Expand Down Expand Up @@ -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 }),
},
)
Expand Down

0 comments on commit de44b40

Please sign in to comment.