BDServer/modules/Render.py

142 lines
3.4 KiB
Python
Raw Normal View History

2024-11-19 13:47:45 +02:00
# AGPL 3 or any later version
# (C) J.Y.Amihud ( Blender Dumbass )
import os
import json
2024-11-19 13:47:45 +02:00
from modules import Set
from modules.Common import *
2024-11-19 13:47:45 +02:00
def guess_type(path):
if "/json/" in path or ".json" in path:
return "application/json"
if "/css" in path or ".css" in path:
return "text/css"
if "/icon" in path or path.endswith(".png"):
return "image/png"
if path.endswith("jpg"):
return "image/jpg"
return "text/html"
def headers(server, code):
# Basic cookie for logins to work
cookie = False
if "Cookie" not in str(server.headers):
cookie = True
2024-11-19 13:47:45 +02:00
server.send_response(code)
server.send_header("Content-type", guess_type(server.path))
if cookie:
server.send_header("Set-Cookie", RandString())
2024-11-19 13:47:45 +02:00
server.end_headers()
def head(title="", description="", image="", config={}):
if image.startswith("/"): image = config.get("url","")+image
favicon = config.get("favicon", "/icon/internet")
2024-11-19 13:47:45 +02:00
html = """
2024-11-19 13:47:45 +02:00
<!-- The head. Part of the HTML code where metadata about the page is storred.
Including the metadata readable by social media websites, when generating link
previwews, which are called mata-tags in HTML. -->
<meta charset="utf-8">
<title>"""+title+"""</title> <!-- Title in the browser tab -->
<link media="all" href="/css" type="text/css" rel="stylesheet" /> <!-- CSS theme link -->
<link rel="icon" href=\""""+favicon+""""> <!-- Tiny image in the tab -->
2024-11-19 13:47:45 +02:00
<!-- Now meta tags for social media -->
<meta property="og:site_name" content=\""""+config.get("title", "My Website")+"""">
<meta property="og:title" content=\""""+title+"""">
<meta property="og:description" content=\""""+description+"""">
<meta property="og:image" content=\""""+image+"""">
</head>
<body>
"""
return html
def send(server, html, code):
# Add headers
headers(server, code)
server.wfile.write(html.encode("utf-8"))
def tabs():
folder = Set.Folder()+"/tabs"
tabs = {}
for tab in sorted(list(os.walk(folder))[0][1]):
try:
with open(folder+"/"+tab+"/config.json") as o:
data = json.load(o)
tabs[tab] = data
except Exception as e:
print(e)
pass
return tabs
2024-11-19 13:47:45 +02:00
def MainPage(server):
# Reading config
config = Set.Load()
# Generating <head>
html = head(title = config.get("title", "Website"),
description = config.get("description", "Description"),
config = config
)
html = html + """
<br>
<br>
<center>
<img src=\""""+config.get("favicon", "icon/internet")+"""" alt="[LOGO]" style="height:150px;vertical-align: middle">
<br>
<br>
<h2>"""+config.get("title", "My Website")+"""</h2>
"""+config.get("tagline", "")+"""
<br>
<br>
"""
Tabs = tabs()
for tab in Tabs:
html = html + Button(Tabs[tab].get("title", tab),
"/"+tab,
Tabs[tab].get("icon", "folder"))
2024-11-19 13:47:45 +02:00
send(server, html, 200)
def Button(text, link, icon=""):
2024-11-19 13:47:45 +02:00
html = """
<a class="button" href=\""""+link+"""">"""
if icon:
html = html + """
<img src="/icon/"""+icon+"""" style="vertical-align: middle">"""
html = html + """
"""+text+"""</a>
"""
return html
2024-11-19 13:47:45 +02:00