-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from markski1/dev
Admin panel, bugfixes
- Loading branch information
Showing
15 changed files
with
383 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
werkzeug | ||
flask | ||
flask-login | ||
python-dotenv | ||
b2sdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,18 @@ | ||
import os | ||
from flask import Flask, send_file, request | ||
from dotenv import load_dotenv | ||
from werkzeug.utils import secure_filename | ||
from core.config import app, app_host, app_port, app_debug, panel_enabled | ||
|
||
from misc import generate_random_string | ||
from b2connect import b2_file_upload, b2_file_exists, b2_cache_file | ||
# Routes | ||
import routes.file | ||
|
||
load_dotenv() | ||
from routes.panel import panel_bp | ||
|
||
authkey = os.getenv('AUTH_KEY') | ||
url_path = os.getenv('URL_PATH') | ||
name_length = os.getenv('NAME_LENGTH') | ||
cache_folder = os.getenv('CACHE_FOLDER') | ||
app = Flask(__name__) | ||
if panel_enabled: | ||
app.register_blueprint(panel_bp) | ||
|
||
|
||
@app.route("/") | ||
def index(): | ||
return 'ok' | ||
|
||
|
||
@app.post("/upload") | ||
def upload_file(): | ||
key = request.form.get('key', None) | ||
if not key or key != authkey: | ||
return "Invalid upload key." | ||
|
||
if 'fileupload' not in request.files: | ||
return "No file provided." | ||
|
||
file = request.files['fileupload'] | ||
filename = secure_filename(file.filename) | ||
|
||
if '.' in filename: | ||
filename = f"{generate_random_string(name_length)}.{filename.split('.')[1]}" | ||
else: | ||
return "Filename does not contain extension." | ||
|
||
filepath = os.path.join(cache_folder, filename) | ||
|
||
# Save to immediate cache | ||
file.save(filepath) | ||
|
||
# Upload file to B2 | ||
success = b2_file_upload(filepath) | ||
|
||
if not success: | ||
return "Error uploading file. Check console output for details." | ||
|
||
return f"{url_path}{filename}" | ||
|
||
|
||
@app.route("/<string:filename>") | ||
def get_file(filename): | ||
# Check local cache | ||
filepath = os.path.join(cache_folder, filename) | ||
if os.path.isfile(filepath): | ||
return send_file(filepath) | ||
|
||
# If not in cache, check B2 | ||
if b2_file_exists(filename): | ||
filepath = b2_cache_file(filename) | ||
if not filepath: | ||
return "Error caching file. Check console output for details." | ||
return send_file(filepath) | ||
|
||
return "File does not exist." | ||
return "BSXU is running." | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host=os.getenv('APP_HOST'), port=os.getenv('APP_PORT'), debug=os.getenv('APP_DEBUG')) | ||
app.run(host=app_host, port=app_port, debug=app_debug) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
from dotenv import load_dotenv | ||
from flask import Flask | ||
from core import session | ||
|
||
load_dotenv() | ||
|
||
authkey = os.getenv('AUTH_KEY') | ||
url_path = os.getenv('URL_PATH') | ||
name_length = os.getenv('NAME_LENGTH') | ||
|
||
cache_folder = os.getenv('CACHE_FOLDER') | ||
os.makedirs(cache_folder, exist_ok=True) | ||
|
||
b2_app_id = os.getenv('B2_KEY_ID') | ||
b2_app_key = os.getenv('B2_APP_KEY') | ||
b2_bucket_name = os.getenv('B2_BUCKET_NAME') | ||
|
||
app_host = os.getenv('APP_HOST') | ||
app_port = os.getenv('APP_PORT') | ||
app_debug = os.getenv('APP_DEBUG') | ||
|
||
panel_enabled = os.getenv('PANEL_ENABLED') == 'true' | ||
|
||
app = Flask(__name__, | ||
static_folder="../webpanel/static", | ||
template_folder="../webpanel/templates") | ||
|
||
session.init_app(app) | ||
|
||
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from flask_login import ( | ||
LoginManager, | ||
UserMixin | ||
) | ||
|
||
from misc import generate_random_string | ||
|
||
import os | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
authkey = os.getenv('AUTH_KEY') | ||
gen_token = "" | ||
|
||
|
||
def init_app(app): | ||
login_manager = LoginManager() | ||
login_manager.init_app(app) | ||
login_manager.session_protection = "strong" | ||
|
||
@login_manager.user_loader | ||
def user_loader(load_id): | ||
if load_id == gen_token: | ||
user_model = Session() | ||
user_model.id = load_id | ||
return user_model | ||
|
||
return None | ||
|
||
|
||
def attempt_login(auth_key): | ||
global gen_token | ||
if auth_key == authkey: | ||
gen_token = generate_random_string(32) | ||
return gen_token | ||
|
||
return None | ||
|
||
|
||
|
||
class Session(UserMixin): | ||
... |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
|
||
from core.config import authkey, name_length, cache_folder, url_path, app | ||
|
||
from flask import send_file, request | ||
from werkzeug.utils import secure_filename | ||
|
||
from misc import generate_random_string | ||
from core.b2connect import b2_file_upload, b2_cache_file | ||
|
||
|
||
@app.post("/upload") | ||
def upload_file(): | ||
key = request.form.get('key', None) | ||
if not key or key != authkey: | ||
return "Invalid upload key." | ||
|
||
if 'fileupload' not in request.files: | ||
return "No file provided." | ||
|
||
file = request.files['fileupload'] | ||
filename = secure_filename(file.filename) | ||
|
||
if '.' in filename: | ||
filename = f"{generate_random_string(name_length)}.{filename.split('.')[1]}" | ||
else: | ||
return "Filename does not contain extension." | ||
|
||
filepath = os.path.join(cache_folder, filename) | ||
|
||
# Save to immediate cache | ||
file.save(filepath) | ||
|
||
# Upload file to B2 | ||
success = b2_file_upload(filepath) | ||
|
||
if not success: | ||
return "Error uploading file. Check console output for details." | ||
|
||
return f"{url_path}{filename}" | ||
|
||
|
||
@app.route("/<string:filename>") | ||
def get_file(filename): | ||
# Check local cache | ||
filepath = str(os.path.join(cache_folder, filename)) | ||
if os.path.isfile(filepath): | ||
return send_file(filepath) | ||
|
||
# If not in cache, check B2 | ||
filepath = b2_cache_file(filename) | ||
if filepath: | ||
return send_file(filepath) | ||
|
||
return "File does not exist." |
Oops, something went wrong.