reorganize

This commit is contained in:
lelo 2025-03-22 10:44:31 +01:00
parent afad6b0595
commit 48a4316d66

152
app.py
View File

@ -114,18 +114,6 @@ def get_cached_image(size):
resized_img.save(img_byte_arr, format='PNG') resized_img.save(img_byte_arr, format='PNG')
return img_byte_arr.getvalue() return img_byte_arr.getvalue()
@app.route('/static/icons/<string:size>.png')
def serve_resized_icon(size):
cached_image_bytes = get_cached_image(size)
return send_file(
io.BytesIO(cached_image_bytes),
mimetype='image/png'
)
@app.route('/sw.js')
def serve_sw():
return send_from_directory(os.path.join(app.root_path, 'static'), 'sw.js', mimetype='application/javascript')
def list_directory_contents(directory, subpath): def list_directory_contents(directory, subpath):
""" """
List only the immediate contents of the given directory. List only the immediate contents of the given directory.
@ -191,7 +179,6 @@ def list_directory_contents(directory, subpath):
pass pass
return directories, files return directories, files
def generate_breadcrumbs(subpath=None): def generate_breadcrumbs(subpath=None):
breadcrumbs = [{'name': 'Home', 'path': ''}] breadcrumbs = [{'name': 'Home', 'path': ''}]
if subpath: if subpath:
@ -202,6 +189,82 @@ def generate_breadcrumbs(subpath=None):
breadcrumbs.append({'name': part, 'path': path_accum}) breadcrumbs.append({'name': part, 'path': path_accum})
return breadcrumbs return breadcrumbs
def lookup_location(ip, reader):
try:
response = reader.city(ip)
country = response.country.name if response.country.name else "Unknown"
city = response.city.name if response.city.name else "Unknown"
return country, city
except Exception:
return "Unknown", "Unknown"
def get_device_type(user_agent):
"classify device type based on user agent string"
if 'Android' in user_agent:
return 'Android'
elif 'iPhone' in user_agent or 'iPad' in user_agent:
return 'iOS'
elif 'Windows' in user_agent:
return 'Windows'
elif 'Macintosh' in user_agent or 'Mac OS' in user_agent:
return 'MacOS'
elif 'Linux' in user_agent:
return 'Linux'
else:
return 'Other'
def shorten_referrer(url):
segments = [seg for seg in url.split('/') if seg]
segment = segments[-1]
# Decode all percent-encoded characters (like %20, %2F, etc.)
segment_decoded = unquote(segment)
return segment_decoded
def log_file_access(full_path):
"""
Log file access details to a SQLite database.
Records the timestamp, full file path, client IP, user agent, and referrer.
"""
# Connect to the database (this will create the file if it doesn't exist)
conn = sqlite3.connect('access_log.db')
cursor = conn.cursor()
# Create the table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS file_access_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
full_path TEXT,
ip_address TEXT,
user_agent TEXT,
referrer TEXT
)
''')
# Gather information from the request
timestamp = datetime.now().isoformat()
ip_address = request.remote_addr
user_agent = request.headers.get('User-Agent')
referrer = request.headers.get('Referer')
# Insert the access record into the database
cursor.execute('''
INSERT INTO file_access_log (timestamp, full_path, ip_address, user_agent, referrer)
VALUES (?, ?, ?, ?, ?)
''', (timestamp, full_path, ip_address, user_agent, referrer))
conn.commit()
conn.close()
@app.route('/static/icons/<string:size>.png')
def serve_resized_icon(size):
cached_image_bytes = get_cached_image(size)
return send_file(
io.BytesIO(cached_image_bytes),
mimetype='image/png'
)
@app.route('/sw.js')
def serve_sw():
return send_from_directory(os.path.join(app.root_path, 'static'), 'sw.js', mimetype='application/javascript')
# API endpoint for AJAX: returns JSON for a given directory. # API endpoint for AJAX: returns JSON for a given directory.
@app.route('/api/path/', defaults={'subpath': ''}) @app.route('/api/path/', defaults={'subpath': ''})
@app.route('/api/path/<path:subpath>') @app.route('/api/path/<path:subpath>')
@ -234,37 +297,6 @@ def api_browse(subpath):
'files': files 'files': files
}) })
def lookup_location(ip, reader):
try:
response = reader.city(ip)
country = response.country.name if response.country.name else "Unknown"
city = response.city.name if response.city.name else "Unknown"
return country, city
except Exception:
return "Unknown", "Unknown"
# Helper function to classify device type based on user agent string
def get_device_type(user_agent):
if 'Android' in user_agent:
return 'Android'
elif 'iPhone' in user_agent or 'iPad' in user_agent:
return 'iOS'
elif 'Windows' in user_agent:
return 'Windows'
elif 'Macintosh' in user_agent or 'Mac OS' in user_agent:
return 'MacOS'
elif 'Linux' in user_agent:
return 'Linux'
else:
return 'Other'
def shorten_referrer(url):
segments = [seg for seg in url.split('/') if seg]
segment = segments[-1]
# Decode all percent-encoded characters (like %20, %2F, etc.)
segment_decoded = unquote(segment)
return segment_decoded
@app.route("/dashboard") @app.route("/dashboard")
@require_secret @require_secret
def dashboard(): def dashboard():
@ -394,38 +426,6 @@ def dashboard():
unique_files=unique_files, unique_files=unique_files,
unique_ips=unique_ips) unique_ips=unique_ips)
def log_file_access(full_path):
"""
Log file access details to a SQLite database.
Records the timestamp, full file path, client IP, user agent, and referrer.
"""
# Connect to the database (this will create the file if it doesn't exist)
conn = sqlite3.connect('access_log.db')
cursor = conn.cursor()
# Create the table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS file_access_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
full_path TEXT,
ip_address TEXT,
user_agent TEXT,
referrer TEXT
)
''')
# Gather information from the request
timestamp = datetime.now().isoformat()
ip_address = request.remote_addr
user_agent = request.headers.get('User-Agent')
referrer = request.headers.get('Referer')
# Insert the access record into the database
cursor.execute('''
INSERT INTO file_access_log (timestamp, full_path, ip_address, user_agent, referrer)
VALUES (?, ?, ?, ?, ?)
''', (timestamp, full_path, ip_address, user_agent, referrer))
conn.commit()
conn.close()
@app.route("/media/<path:subpath>") @app.route("/media/<path:subpath>")
@require_secret @require_secret