summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--git-gmi/config.py4
-rw-r--r--git-gmi/const.py3
-rw-r--r--git-gmi/gateway.py3
-rw-r--r--git-gmi/git.py27
-rw-r--r--git-gmi/index.gmi18
-rw-r--r--git-gmi/utils.py17
7 files changed, 62 insertions, 12 deletions
diff --git a/README.md b/README.md
index 2c7be41..dea47ef 100644
--- a/README.md
+++ b/README.md
@@ -65,4 +65,4 @@ Access your instance at `gemini://your-domain-or-ip/git/cgi/`. Don't forget the
## Contributing
-The mailing list is `~fkfd/git.gmi@lists.sr.ht`. Send patches ([git-send-email](https://git-send-email.io)) and feedback to the list.
+The mailing list is `~fkfd/gemini@lists.sr.ht`. Send patches ([git-send-email](https://git-send-email.io)) and feedback to the list.
diff --git a/git-gmi/config.py b/git-gmi/config.py
index d9b04e7..e72c6f9 100644
--- a/git-gmi/config.py
+++ b/git-gmi/config.py
@@ -1,7 +1,7 @@
# where on the disk are the repos located
-GIT_CATALOG = "/home/fakefred/p/gemini/repos/"
+GIT_CATALOG = "/home/fakefred/p/_lab/gemini/repos/"
# which path leads to your cgi app after the URL's host part
-CGI_PATH = "/git/cgi/"
+CGI_PATH = "/cgi/"
# cache dir
CACHE_DIR = "/home/fakefred/Archive/_cache/"
# how long before cache expires, in seconds: int
diff --git a/git-gmi/const.py b/git-gmi/const.py
index 44818c6..f1f757f 100644
--- a/git-gmi/const.py
+++ b/git-gmi/const.py
@@ -2,4 +2,5 @@ STATUS_SUCCESS = "20"
STATUS_NOT_FOUND = "51 NOT FOUND"
STATUS_TEMPORARY_FAILURE = "40 TEMPORARY FAILURE"
META_GEMINI = "text/gemini"
-META_PLAINTEXT = "text/plain" \ No newline at end of file
+META_PLAINTEXT = "text/plain"
+MAX_DISPLAYED_BLOB_SIZE = 500 * 1024 # 500KB
diff --git a/git-gmi/gateway.py b/git-gmi/gateway.py
index 29f0268..b0c9b0a 100644
--- a/git-gmi/gateway.py
+++ b/git-gmi/gateway.py
@@ -2,6 +2,7 @@ from git import *
from const import *
from config import *
from os import environ, listdir
+import sys
# be careful when using print(); stdout is passed to the client.
# this cgi uses \n as newline.
@@ -56,7 +57,7 @@ def handle_cgi_request(path: str, query: str):
except FileNotFoundError: # is file
try:
if query == "raw":
- print(repo.view_raw_blob(branch, location))
+ sys.stdout.buffer.write(repo.view_raw_blob(branch, location))
else:
print(repo.view_blob(branch, location))
except FileNotFoundError:
diff --git a/git-gmi/git.py b/git-gmi/git.py
index 8a91f0b..b758440 100644
--- a/git-gmi/git.py
+++ b/git-gmi/git.py
@@ -8,6 +8,7 @@ import shutil
import mimetypes
from const import *
from config import *
+from utils import *
mimetypes.add_type("text/gemini", ".gmi")
mimetypes.add_type("text/gemini", ".gemini")
@@ -342,17 +343,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```"
- self._write_cache(["tree", branch] + location, response)
+
+ 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" + add_line_numbers(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
diff --git a/git-gmi/index.gmi b/git-gmi/index.gmi
new file mode 100644
index 0000000..49a4cbe
--- /dev/null
+++ b/git-gmi/index.gmi
@@ -0,0 +1,18 @@
+# This is a git.gmi instance - a frontend for Git on Gemini
+
+```git.gmi
+ _ _ _
+ (_) | | (_)
+ __ _ _ | |_ __ _ _ __ ___ _
+ / _` | | | | __| / _` | | '_ ` _ \ | |
+| (_| | | | | |_ _ | (_| | | | | | | | | |
+ \__, | |_| \__| (_) \__, | |_| |_| |_| |_|
+ __/ | __/ |
+ |___/ |___/
+```
+
+=> /cgi/ Repo index
+
+=> gemini://git.fkfd.me/cgi/git.gmi/ Source code
+=> https://git.sr.ht/~fkfd/git.gmi/ Source code (HTTPS)
+
diff --git a/git-gmi/utils.py b/git-gmi/utils.py
new file mode 100644
index 0000000..c3dbd59
--- /dev/null
+++ b/git-gmi/utils.py
@@ -0,0 +1,17 @@
+import math
+
+
+def add_line_numbers(code: str) -> str:
+ lines = code.splitlines()
+ if not lines:
+ return code # empty anyway
+
+ # cannot use math.ceil() here bc lg100=2
+ max_digits = math.floor(math.log10(len(lines))) + 1
+
+ for n, l in enumerate(lines, 1):
+ digits_in_n = math.floor(math.log10(n)) + 1
+ spaces_before_number = max_digits - digits_in_n
+ lines[n - 1] = " " * spaces_before_number + str(n) + " " + l
+
+ return "\n".join(lines)