summaryrefslogtreecommitdiff
path: root/git-gmi/git.py
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@macaw.me>2020-07-10 20:30:54 +0800
committerFrederick Yin <fkfd@macaw.me>2020-07-10 20:30:54 +0800
commit109ec9a7d86bcadb16541873d95068f737586692 (patch)
tree405116ed82038be22f8fb0a234f57ebd1dc65400 /git-gmi/git.py
parenteb1bd1e074b81a428cb689d3a381e86e2fd530dd (diff)
Enhance blob view
Changes: - set maximum size of displayable blobs - raw blobs are now of type `bytes` - do not try to decode handle binary blobs
Diffstat (limited to 'git-gmi/git.py')
-rw-r--r--git-gmi/git.py25
1 files changed, 19 insertions, 6 deletions
diff --git a/git-gmi/git.py b/git-gmi/git.py
index 3de904e..f249ec5 100644
--- a/git-gmi/git.py
+++ b/git-gmi/git.py
@@ -263,16 +263,29 @@ class GitGmiRepo:
f"{STATUS_SUCCESS} {META_GEMINI}\r\n"
+ self.generate_header()
+ f"## {self.name}/{'/'.join(location)} | {convert_filesize(blob.size)}\n\n"
- f"=> {blob.name}?raw view raw\n\n"
- f"```\n"
)
- response += blob.data.decode("utf-8") + "\n```"
+
+ if blob.is_binary:
+ response += (
+ "This file seems to be binary. Open link below to download.\n"
+ f"=> {blob.name}?raw download"
+ )
+ elif blob.size < MAX_DISPLAYED_BLOB_SIZE:
+ response += (
+ f"=> {blob.name}?raw view raw\n\n"
+ "```\n" + blob.data.decode("utf-8") + "\n```"
+ )
+ else:
+ response += (
+ "This file is too large to be displayed. Open link below to download.\n"
+ f"=> {blob.name}?raw download\n\n"
+ )
return response
- def view_raw_blob(self, branch: str, location=[]) -> str:
+ def view_raw_blob(self, branch: str, location=[]) -> bytes:
blob = self.get_blob(branch, location)
# if mimetypes can't make out the type, set it to plaintext
guessed_mimetype = mimetypes.guess_type(blob.name)[0] or META_PLAINTEXT
- response = f"{STATUS_SUCCESS} {guessed_mimetype}\r\n"
- response += blob.data.decode("utf-8")
+ response = bytes(f"{STATUS_SUCCESS} {guessed_mimetype}\r\n", encoding="utf-8")
+ response += blob.data
return response