diff --git a/gallery/__init__.py b/gallery/__init__.py index d8987eb..d2cabfa 100644 --- a/gallery/__init__.py +++ b/gallery/__init__.py @@ -17,17 +17,20 @@ import os from flask import * from werkzeug.utils import secure_filename -def create_app(test_config=None): - # Get environment variables - from dotenv import load_dotenv - load_dotenv(os.path.join('./gallery', 'user', '.env')) - +# Import dotenv +from dotenv import load_dotenv + +def create_app(test_config=None): # create and configure the app app = Flask(__name__) + # Get environment variables + load_dotenv(os.path.join(app.root_path, 'user', '.env')) + app.config.from_mapping( SECRET_KEY=os.environ.get('FLASK_SECRET'), DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'), + UPLOAD_FOLDER=os.path.join(app.root_path, 'user', 'uploads'), ) if test_config is None: @@ -49,8 +52,8 @@ def create_app(test_config=None): db.init_app(app) # Load theme - #from . import sassy - #sassy.compile('default') + from . import sassy + sassy.compile('default', app.root_path) @app.errorhandler(405) diff --git a/gallery/gallery.py b/gallery/gallery.py index a9d44b9..1b943b3 100644 --- a/gallery/gallery.py +++ b/gallery/gallery.py @@ -1,10 +1,12 @@ from flask import ( - Blueprint, flash, g, redirect, render_template, request, url_for, jsonify + Blueprint, flash, g, redirect, render_template, request, url_for, jsonify, current_app ) from werkzeug.exceptions import abort from werkzeug.utils import secure_filename from gallery.auth import login_required from gallery.db import get_db +import json +import os blueprint = Blueprint('gallery', __name__) @@ -41,9 +43,13 @@ def upload(): file = request.files['file'] form = request.form - if secure_filename(file.filename) == '': + if not file: flash('No selected file') - return redirect('gallery.upload') + return abort(404) + + file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], secure_filename(file.filename))) + + return json.dumps({'filename': secure_filename(file.filename), 'form': form}) return render_template('upload.html') diff --git a/gallery/sassy.py b/gallery/sassy.py index 37058ca..cd67194 100644 --- a/gallery/sassy.py +++ b/gallery/sassy.py @@ -6,36 +6,37 @@ import os import sass class compile(): - def __init__(self, theme): + def __init__(self, theme, dir): print(f"Loading '{theme}' theme...") - theme_path = os.path.join('./user', 'themes', theme, 'style.scss') - font_path = os.path.join('./user', 'themes', theme, 'fonts') + theme_path = os.path.join(dir, 'user', 'themes', theme, 'style.scss') + font_path = os.path.join(dir, 'user', 'themes', theme, 'fonts') + dest = os.path.join(dir, 'static', 'theme') print(f"Theme path: {theme_path}") if os.path.exists(theme_path): self.sass = sass - self.loadTheme(theme_path) - self.loadFonts(font_path) + self.loadTheme(theme_path, dest) + self.loadFonts(font_path, dest) else: print("No theme found!") sys.exit(1) print(f"{now.hour}:{now.minute}:{now.second} - Done!\n") - def loadTheme (self, theme): - with open('static/theme/style.css', 'w') as f: + def loadTheme (self, theme, dest): + with open(os.path.join(dest, 'style.css'), 'w') as f: try: f.write(self.sass.compile(filename=theme, output_style='compressed')) - print("Compiled successfully to:", f.name) + print("Compiled successfully!") except self.sass.CompileError as e: print("Failed to compile!\n", e) sys.exit(1) - def loadFonts (self, font_path): - dest = os.path.join('./static', 'theme', 'fonts') + def loadFonts (self, source, dest): + dest = os.path.join(dest, 'fonts') if os.path.exists(dest): print("Removing old fonts...") @@ -46,7 +47,7 @@ class compile(): sys.exit(1) try: - shutil.copytree(font_path, dest) + shutil.copytree(source, dest) print("Copied fonts to:", dest) except Exception as e: print("Failed to copy fonts!\n", e) diff --git a/gallery/templates/upload.html b/gallery/templates/upload.html index 2a81953..349323f 100644 --- a/gallery/templates/upload.html +++ b/gallery/templates/upload.html @@ -8,52 +8,42 @@

Upload!!!!!

-
- - - - - +