Skip to content

Commit

Permalink
Merge pull request #19 from lovec741/nicer-frontend
Browse files Browse the repository at this point in the history
Changed the way pdf, xml and json are handled
  • Loading branch information
Headary authored Oct 12, 2024
2 parents d2b5fa2 + db2762e commit 478a27d
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from flask import Flask, abort, redirect, render_template, send_from_directory, session, url_for, abort
from flask import Flask, abort, redirect, render_template, send_from_directory, session, url_for, request
import os
import tomllib
from functools import wraps
Expand Down Expand Up @@ -122,7 +122,7 @@ def create_breadcrumbs(repo: Repository, normalized_path):
@requireLogin
@repo_required
def repository(repo: Repository, path=''):
_, normalized_path, target_path = check_and_parse_path(repo, path)
repo_dir, normalized_path, target_path = check_and_parse_path(repo, path)
if (path != '' and os.path.isdir(target_path) and path[-1] != '/'):
return redirect(url_for('repository', repo_name=repo.name, path=path+'/'))

Expand Down Expand Up @@ -153,14 +153,16 @@ def repository(repo: Repository, path=''):
return render_template('listdir.html.jinja', path=os.path.normpath(os.path.join(repo.name, normalized_path)), repo_name=repo.name, dirs=dirs, files=files, breadcrumbs=create_breadcrumbs(repo, normalized_path))

if os.path.isfile(target_path):
if request.headers.get('Accept') == '*/*':
return send_from_directory(repo_dir, normalized_path)
file_display_type = 'other'
file_content = None

mime = magic.Magic(mime=True)
detected_mime = mime.from_file(target_path)
if detected_mime == 'application/pdf':
file_display_type = 'preview'
elif detected_mime.startswith('text/') or detected_mime in ["application/json"]:
if detected_mime in ['application/pdf', 'text/xml', "application/json"]:
return send_from_directory(repo_dir, normalized_path)
elif detected_mime.startswith('text/'):
file_display_type = 'text'
with open(target_path, 'r', encoding='utf-8', errors='ignore') as file:
file_content = file.read()
Expand Down

0 comments on commit 478a27d

Please sign in to comment.