Kumi
04907d6a5b
Includes MIT License file to clarify usage terms. Introduces a README with setup and configuration instructions for the noBSdelivr app. Updates the main Python application to use an environment variable for port configuration, improving deployment flexibility. Enhances code organization and documentation to aid user setup and compliance.
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
from flask import Flask, render_template, request, Response, abort
|
|
from werkzeug.exceptions import InternalServerError
|
|
|
|
from urllib.request import urlopen
|
|
from urllib.error import HTTPError
|
|
from urllib.parse import unquote
|
|
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
@app.route("/")
|
|
def home():
|
|
domain = request.url_root
|
|
return render_template("index.html", domain=domain)
|
|
|
|
|
|
@app.route("/<path:url>")
|
|
def proxy(url):
|
|
jsdelivr_url = f"https://cdn.jsdelivr.net/{url}"
|
|
|
|
def generate():
|
|
# Subfunction to allow streaming the data instead of
|
|
# downloading all of it at once
|
|
try:
|
|
with urlopen(unquote(jsdelivr_url)) as data:
|
|
while True:
|
|
chunk = data.read(1024 * 1024)
|
|
if not chunk:
|
|
break
|
|
yield chunk
|
|
except HTTPError as e:
|
|
abort(e.code)
|
|
|
|
try:
|
|
with urlopen(unquote(jsdelivr_url)) as data:
|
|
content_type = data.headers["content-type"]
|
|
except HTTPError as e:
|
|
abort(e.code)
|
|
except KeyError:
|
|
raise InternalServerError()
|
|
|
|
headers = dict()
|
|
filename = url.split("/")[-1]
|
|
headers["Content-Disposition"] = f'attachment; filename="{filename}"'
|
|
|
|
return Response(generate(), content_type=content_type, headers=headers)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=os.environ.get("PORT", 5680))
|