Skip to content

Commit

Permalink
Stop garbling binary files on raw display
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Dec 31, 2023
1 parent 7c5415e commit fcc1871
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
47 changes: 42 additions & 5 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use anyhow::{anyhow, Context, Result};
use axum::response::IntoResponse;
use bytes::{BufMut, Bytes, BytesMut};
use comrak::{ComrakOptions, ComrakPlugins};
use git2::{
Expand Down Expand Up @@ -126,10 +127,15 @@ impl OpenRepository {
.extension()
.or_else(|| path.file_name())
.map_or_else(|| Cow::Borrowed(""), OsStr::to_string_lossy);
let content = if formatted {
format_file(blob.content(), &extension, &self.git.syntax_set)?
} else {
String::from_utf8_lossy(blob.content()).to_string()
let content = match (formatted, blob.is_binary()) {
(true, true) => Content::Binary(vec![]),
(true, false) => Content::Text(
format_file(blob.content(), &extension, &self.git.syntax_set)?.into(),
),
(false, true) => Content::Binary(blob.content().to_vec()),
(false, false) => Content::Text(
String::from_utf8_lossy(blob.content()).to_string().into(),
),
};

return Ok(PathDestination::File(FileWithContent {
Expand Down Expand Up @@ -464,7 +470,38 @@ pub struct File {
#[derive(Debug)]
pub struct FileWithContent {
pub metadata: File,
pub content: String,
pub content: Content,
}

#[derive(Debug)]
pub enum Content {
Text(Cow<'static, str>),
Binary(Vec<u8>),
}

impl IntoResponse for Content {
fn into_response(self) -> axum::response::Response {
use axum::http;

match self {
Self::Text(t) => {
let headers = [(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("text/plain; charset=UTF-8"),
)];

(headers, t).into_response()
}
Self::Binary(b) => {
let headers = [(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("application/octet-stream"),
)];

(headers, b).into_response()
}
}
}
}

#[derive(Debug)]
Expand Down
10 changes: 1 addition & 9 deletions src/methods/repo/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
use askama::Template;
use axum::{
extract::Query,
http,
response::{IntoResponse, Response},
Extension,
};
Expand Down Expand Up @@ -86,14 +85,7 @@ pub async fn handle(
branch: query.branch.clone(),
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) if query.raw => file.content.into_response(),
PathDestination::File(file) => into_response(&FileView {
repo,
file,
Expand Down
9 changes: 8 additions & 1 deletion templates/repo/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@
{% endblock %}

{% block content %}
<pre>{{ file.content|safe }}</pre>
<pre>
{%- match file.content -%}
{%- when crate::git::Content::Text with (content) -%}
{{- content|safe -}}
{%- when crate::git::Content::Binary with (_) -%}
&lt;binary file not displayed&gt;
{%- endmatch -%}
</pre>
{% endblock %}

0 comments on commit fcc1871

Please sign in to comment.