diff --git a/README.md b/README.md index e69de29..f1f7893 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,132 @@ +# GitCloak + +GitCloak is a simple web frontend that lets you browse GitHub repositories and view their contents. It provides an easy-to-use interface to navigate directories, view files, and also renders `README.md` files. It is still a work in progress, but the basic functionality is there. + +Instead of using the GitHub API or screen scraping, GitCloak interfaces directly with the GitHub repository using the git protocol. This should make it more resilient to changes on the GitHub side, and also allows future modifications or forks to support other git hosting services like Forgejo or GitLab instances. + +## Features + +- Browse directories and files in GitHub repositories. +- View the content of files directly in the browser. +- Responsive design with Bootstrap for a clean and professional look. + +## Getting Started + +### Prerequisites + +- Python 3 (tested with 3.12) +- pip +- venv (optional but recommended) + +### Production + +1. Set up a virtual environment and activate it: + + ```sh + python -m venv venv + source venv/bin/activate + ``` + +2. Install GitCloak from PyPI: + + ```sh + pip install gitcloak + ``` + +3. Run the GitCloak server: + + ```sh + gitcloak + ``` + +4. Open your browser and navigate to: + ``` + http://localhost:8107/ + ``` + +### Development + +1. Clone the repository: + + ```sh + git clone https://git.private.coffee/PrivateCoffee/gitcloak + cd gitcloak + ``` + +2. Create a virtual environment and activate it: + + ```sh + python -m venv venv + source venv/bin/activate + ``` + +3. Install the Python package: + + ```sh + pip install -Ue . + + ``` + +4. Run the development server: + + ```sh + gitcloak + ``` + +5. Open your browser and navigate to: + ``` + http://localhost:8107/ + ``` + +### Configuration + +GitCloak currently supports the following environment variables: + +- `PORT`: The port number to run the server on. Default is `8107`. +- `HOST`: The host to bind the server to. Default is `127.0.0.1`. + +### Usage + +- **Landing Page**: The landing page provides information about the app and instructions on how to use it. + + ``` + http://localhost:8107/ + ``` + +- **Browse Repository**: To browse a repository, use the following URL format: + + ``` + http://localhost:8107/// + ``` + +- **View Subdirectory**: To view a specific directory, use the following URL format: + + ``` + http://localhost:8107///tree/main/ + ``` + +- **View File Content**: To view the raw content of a specific file, use the following URL format: + ``` + http://localhost:8107///raw/main/ + ``` + +## Contributing + +We welcome contributions to improve GitCloak! To contribute: + +1. Fork the repository. +2. Create a new branch for your feature or bugfix. +3. Make your changes and commit them. +4. Push your changes to your fork. +5. Create a pull request to merge your changes into the main repository. + +## License + +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details. + +## Acknowledgments + +- [Flask](https://flask.palletsprojects.com/) - A lightweight WSGI web application framework in Python. +- [Bootstrap](https://getbootstrap.com/) - A powerful front-end framework for faster and easier web development. +- [markdown2](https://github.com/trentm/python-markdown2) - A fast and complete implementation of Markdown in Python. +- [Phosphor Icons](https://phosphoricons.com/) - Beautifully designed icons for use in web projects. diff --git a/pyproject.toml b/pyproject.toml index a8b2177..8929937 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "hatchling.build" [project] name = "gitcloak" version = "0.0.1" -authors = [{ name = "Kumi Mitterer", email = "gitcloak@kumi.email" }] +authors = [{ name = "Private.coffee Team", email = "support@private.coffee" }] description = "Simple Python-based private frontend for GitHub repositories" readme = "README.md" license = { file = "LICENSE" } @@ -17,6 +17,9 @@ classifiers = [ ] dependencies = ["requests", "flask", "markdown2[all]"] +[project.scripts] +gitcloak = "gitcloak.app:main" + [project.urls] -"Homepage" = "https://git.private.coffee/kumi/gitcloak" -"Bug Tracker" = "https://git.private.coffee/kumi/gitcloak/issues" +"Homepage" = "https://git.private.coffee/PrivateCoffee/gitcloak" +"Bug Tracker" = "https://git.private.coffee/PrivateCoffee/gitcloak/issues" diff --git a/src/gitcloak/app.py b/src/gitcloak/app.py index 8bc5e2a..1f5d1ec 100644 --- a/src/gitcloak/app.py +++ b/src/gitcloak/app.py @@ -2,6 +2,9 @@ from flask import Flask, render_template, abort, send_from_directory from .classes.git import Git from .classes.markdown import RelativeURLRewriter import logging +import os +import base64 +import mimetypes from pathlib import Path logger = logging.getLogger(__name__) @@ -91,7 +94,16 @@ def get_raw(owner, repo, file_path): except UnicodeDecodeError: content_type = "application/octet-stream" - return file_content, 200, {"Content-Type": content_type} + content_type, _ = mimetypes.guess_type(file_path) + + file_name = file_path.split("/")[-1] + + headers = { + "Content-Type": content_type, + "Content-Disposition": f"attachment; filename={file_name}", + } + + return file_content, 200, headers except Exception as e: logger.error( f"Error getting file content for {file_path} in {owner}/{repo}: {e}" @@ -99,5 +111,48 @@ def get_raw(owner, repo, file_path): abort(404, description=str(e)) +@app.route("///blob/main/", methods=["GET"]) +def preview_file(owner, repo, file_path): + repo_url = f"https://github.com/{owner}/{repo}.git" + git = Git(repo_url) + try: + file_content = git.get_file_content(file_path) + + content_type, _ = mimetypes.guess_type(file_path) + is_text = content_type and content_type.startswith("text") + is_image = content_type and content_type.startswith("image") + is_safe = False + + if content_type == "text/markdown": + base_url = f"/{owner}/{repo}/raw/main/{"/".join(file_path.split("/")[:-1])}".rstrip("/") + file_content = RelativeURLRewriter(base_url).convert( + file_content.decode("utf-8") + ) + is_safe = True + + if is_image: + file_content = base64.b64encode(file_content).decode("utf-8") + + return render_template( + "preview.html", + owner=owner, + repo=repo, + file_path=file_path, + file_content=file_content, + is_text=is_text, + is_image=is_image, + is_safe=is_safe + ) + except Exception as e: + logger.error(f"Error previewing file {file_path} in {owner}/{repo}: {e}") + abort(404, description=str(e)) + + +def main(): + port = os.environ.get("PORT", 8107) + host = os.environ.get("HOST", "127.0.0.1") + app.run(debug=True, port=port, host=host) + + if __name__ == "__main__": - app.run(debug=True, port=8107) + main() diff --git a/src/gitcloak/classes/markdown.py b/src/gitcloak/classes/markdown.py index 024c78b..be8b621 100644 --- a/src/gitcloak/classes/markdown.py +++ b/src/gitcloak/classes/markdown.py @@ -10,7 +10,6 @@ class RelativeURLRewriter(markdown2.Markdown): # Rewrite relative URLs def replace_url(match): url = match.group(1) - print(f"URL: {match}") if not (":" in url or url.startswith("/") or url.startswith("#") or url.startswith("md5-")): return f'src="{self.base_url}/{url}"' return match.group(0) diff --git a/src/gitcloak/templates/index.html b/src/gitcloak/templates/index.html index e0fffd7..31953a2 100644 --- a/src/gitcloak/templates/index.html +++ b/src/gitcloak/templates/index.html @@ -12,7 +12,7 @@
  • /<owner>/<repo>/ - View the root directory of the repository
  • /<owner>/<repo>/tree/main/<path> - View a specific directory
  • -
  • /<owner>/<repo>/raw/<file_path> - View the raw content of a specific file
  • +
  • /<owner>/<repo>/raw/main/<file_path> - View the raw content of a specific file

For example, to see the contents of the PrivateCoffee/transfer.coffee repository, simply visit /PrivateCoffee/transfer.coffee/.

Other features are still in development, so stay tuned!

diff --git a/src/gitcloak/templates/path.html b/src/gitcloak/templates/path.html index b54b60e..a6502d4 100644 --- a/src/gitcloak/templates/path.html +++ b/src/gitcloak/templates/path.html @@ -23,7 +23,7 @@ > {{ 'folder' if entry in
diff --git a/src/gitcloak/templates/preview.html b/src/gitcloak/templates/preview.html
new file mode 100644
index 0000000..4b07841
--- /dev/null
+++ b/src/gitcloak/templates/preview.html
@@ -0,0 +1,35 @@
+{% extends 'base.html' %}
+
+{% block title %}{{ owner }}/{{ repo }} - {{ file_path }}{% endblock %}
+
+{% block content %}
+<div class= +
+

{{ owner }}/{{ repo }} - {{ file_path }}

+
+
+
+ {% if is_text %} + {% if is_safe %} +
{{ file_content | safe }}
+ {% else %} +
{{ file_content }}
+ {% endif %} + {% elif is_image %} + {{ file_path }} + {% else %} +

This file is binary and cannot be displayed.

+ {% endif %} +
+

+ Download +

+
+
+
+ +{% endblock %} \ No newline at end of file