Database requests

This commit is contained in:
Michał 2022-12-19 21:15:56 +00:00
parent f41312248c
commit 594431b420
8 changed files with 84 additions and 42 deletions

74
app.py
View file

@ -1,5 +1,7 @@
from flask import Flask, render_template, send_from_directory, abort, url_for, jsonify, redirect, request, session
from werkzeug.utils import secure_filename
import mysql.connector
from mysql.connector import Error
import os
# Get database stuff
@ -8,7 +10,27 @@ DB_PASS = os.environ.get('PASSWORD')
DB_HOST = os.environ.get('HOST')
DB_PORT = os.environ.get('PORT')
DB = os.environ.get('DATABASE')
DB_NAME = os.environ.get('DATABASE')
try:
DB = mysql.connector.connect(host=DB_HOST,
port=DB_PORT,
database=DB_NAME,
user=DB_USER,
password=DB_PASS)
if DB.is_connected():
db_Info = DB.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = DB.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
# Set flask config
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@ -64,17 +86,24 @@ def home():
return render_template('home.html')
@app.route('/image/<file_id>')
def image(file_id):
@app.route('/image/<request_id>')
def image(request_id):
# Check if request_id is valid
try:
file_id = int(file_id)
request_id = int(request_id)
except ValueError:
abort(404)
# SQL injection prevention
sql = "SELECT imagename FROM images WHERE id = %s"
img_id = (request_id,)
# Get image details
cursor = DB.cursor()
cursor.execute(sql, img_id)
result = cursor.fetchone()
file_list = os.listdir(os.path.join(app.config['UPLOAD_FOLDER'], 'original'))
file_name = file_list[file_id]
return render_template('image.html', fileName=file_name, id=file_id)
return render_template('image.html', fileName=result[0], id=request_id)
#
@ -84,27 +113,28 @@ def image(file_id):
def image_list(item_type):
if request.method != 'GET':
abort(405)
cursor = DB.cursor()
cursor.execute("SELECT id,imagename FROM images ORDER BY id DESC")
item_list = cursor.fetchall()
item_type = secure_filename(item_type)
type_dir = os.path.join(app.config['UPLOAD_FOLDER'], item_type)
if not os.path.isdir(type_dir):
abort(404)
return jsonify(os.listdir(type_dir))
return jsonify(item_list)
@app.route('/uploads/<item_type>/<file_id>', methods=['GET'])
def uploads(item_type, file_id):
@app.route('/uploads/<quality>/<request_file>', methods=['GET'])
def uploads(quality, request_file):
if request.method != 'GET':
abort(405)
item_type = secure_filename(item_type)
type_dir = os.path.join(app.config['UPLOAD_FOLDER'], item_type)
if not os.path.isdir(type_dir):
quality = secure_filename(quality)
quality_dir = os.path.join(app.config['UPLOAD_FOLDER'], quality)
if not os.path.isdir(quality_dir):
abort(404)
file_id = secure_filename(file_id)
if not os.path.isfile(os.path.join(type_dir, file_id)):
request_file = secure_filename(request_file)
if not os.path.isfile(os.path.join(quality_dir, request_file)):
abort(404)
return send_from_directory(type_dir, file_id)
return send_from_directory(quality_dir, request_file)

8
example.env Normal file
View file

@ -0,0 +1,8 @@
# Login
USERNAME = root
PASSWORD =
HOST = localhost
PORT = 3306
# Database name
DATABASE = onlylegs

View file

@ -52,7 +52,7 @@ nav div {
}
nav div a {
margin: 0;
padding: 1rem 1rem 1rem 0.7rem;
padding: 1rem 1rem 1rem calc(1rem - 2px);
width: 100%;
height: 3.5rem;
display: flex;
@ -61,7 +61,7 @@ nav div a {
gap: 0.5rem;
text-decoration: none;
color: #e8e3e3;
border-left: 0.3rem solid rgba(0, 0, 0, 0);
border-left: 2px solid rgba(0, 0, 0, 0);
box-sizing: border-box;
}
nav div a i {
@ -81,7 +81,7 @@ nav div a span {
}
nav div a:hover {
background-color: #121212;
border-left: 0.3rem solid #8C977D;
border-left: 2px solid #8C977D;
}
nav div a:hover * {
color: #8C977D;
@ -101,6 +101,7 @@ main header {
padding: 0;
width: 100%;
height: 40vh;
position: relative;
background-color: #121212;
border-radius: 8px;
box-sizing: border-box;
@ -153,7 +154,7 @@ main header span {
margin: 0 0 0 3.5rem;
padding: 0.5rem;
width: auto;
min-height: 90vh;
min-height: 100vh;
position: relative;
display: flex;
flex-direction: column;

View file

@ -1 +1 @@
{"version":3,"sources":["style.scss","style.css"],"names":[],"mappings":"AAkBA;E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A;EACA,gBAAA;EAEA,cA7aA;EA+aA,uBAAA;EACA,gBAAA;AC7HR;AD+HI;EACI,SAAA;EACA,sBAAA;EAEA,oCAhbI;EAibJ,iBAAA;EACA,gBAAA;EAEA,cA/bG;EAicH,uBAAA;EACA,gBAAA;AChIR","file":"style.css"}

View file

@ -83,7 +83,7 @@ nav {
a {
margin: 0;
padding: 1rem 1rem 1rem 0.7rem;
padding: 1rem 1rem 1rem calc(1rem - 2px);
width: 100%;
height: 3.5rem;
@ -95,7 +95,7 @@ nav {
text-decoration: none;
color: $white100;
border-left: 0.3rem solid #00000000;
border-left: 2px solid #00000000;
box-sizing: border-box;
@ -123,7 +123,7 @@ nav {
&:hover {
background-color: $black200;
border-left: 0.3rem solid $green;
border-left: 2px solid $green;
* {
color: $green;
@ -152,6 +152,8 @@ main {
width: 100%;
height: 40vh;
position: relative;
background-color: $black200;
border-radius: $rad;
@ -220,7 +222,7 @@ main {
padding: 0.5rem;
width: auto;
min-height: 90vh;
min-height: 100vh;
position: relative;

View file

@ -2,6 +2,7 @@
{% block content %}
<header>
<img src="{{ url_for('static', filename='images/leaves.jpg') }}" alt="leaves"/>
<span></span>
</header>
<div class="app">
<h1>{{error}}</h1>

View file

@ -12,10 +12,10 @@
let imageList = [];
let imageIndex = 0;
function loadMore(startIndex, amount = 10) {
function loadMore(startIndex, amount = 20) {
for (let i = startIndex; i < startIndex + amount; i++) {
if (i < imageList.length) {
loadImg(i, imageList[i]);
loadImg(imageList[i][0], imageList[i][1]);
}
}
}
@ -41,13 +41,13 @@
success: function(response) {
imageList = response;
loadMore(0, 20);
imageIndex += 20;
loadMore(0, 30);
imageIndex += 30;
}
});
$(window).scroll(function() {
if ($(window).height() + $(window).scrollTop() >= $(document).height() - 200) {
if ($(window).height() + $(window).scrollTop() >= $(document).height() - 500) {
loadMore(imageIndex);
imageIndex += 10;
}

View file

@ -15,19 +15,19 @@
<body>
<nav id="navRoot">
<div>
<a href="{{ url_for('home') }}"><i class="ph-house-line-fill"></i><span>Home</span></a>
<a href=""><i class="ph-package-fill"></i><span>Groups</span></a>
<a href=""><i class="ph-upload-fill"></i><span>Upload</span></a>
<a href="{{ url_for('home') }}"><i class="ph-house-line"></i><span>Home</span></a>
<a href=""><i class="ph-package"></i><span>Groups</span></a>
<a href=""><i class="ph-upload"></i><span>Upload</span></a>
</div>
<div>
<a href=""><i class="ph-user-circle-fill"></i><span>Profile</span></a>
<a href=""><i class="ph-gear-fill"></i><span>Settings</span></a>
<a href=""><i class="ph-user-circle"></i><span>Profile</span></a>
<a href=""><i class="ph-gear"></i><span>Settings</span></a>
</div>
</nav>
<main>
{% block content %}
{% endblock %}
<i class="ph-arrow-circle-up-fill" id="topButton"></i>
<i class="ph-arrow-circle-up" id="topButton"></i>
</main>
<script>