Merge pull request #1 from partitioncloud/dev

This commit is contained in:
Augustin 2023-08-30 18:59:05 +02:00 committed by GitHub
commit 65c8d608e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 23 deletions

View File

@ -4,12 +4,12 @@ Main file
"""
import os
from flask import Flask, g, redirect, render_template, request, send_file, flash, session
from flask import Flask, g, redirect, render_template, request, send_file, flash, session, abort
from werkzeug.security import generate_password_hash
from .modules.utils import User, Album, get_all_albums
from .modules import albums, auth, partition, admin
from .modules.auth import admin_required
from .modules.auth import admin_required, login_required
from .modules.db import get_db
app = Flask(__name__)
@ -82,9 +82,35 @@ def add_user():
return render_template("auth/register.html", albums=get_all_albums(), user=current_user)
# Automatically close db after each request
@app.route("/static/search-thumbnails/<uuid>.jpg")
@login_required
def search_thumbnail(uuid):
db = get_db()
partition = db.execute(
"""
SELECT uuid, url FROM search_results
WHERE uuid = ?
""",
(uuid,)
).fetchone()
if partition is None:
abort(404)
if not os.path.exists(os.path.join(app.static_folder, "search-thumbnails", f"{uuid}.jpg")):
os.system(
f'/usr/bin/convert -thumbnail\
"178^>" -background white -alpha \
remove -crop 178x178+0+0 \
partitioncloud/search-partitions/{uuid}.pdf[0] \
partitioncloud/static/search-thumbnails/{uuid}.jpg'
)
return send_file(os.path.join(app.static_folder, "search-thumbnails", f"{uuid}.jpg"))
@app.after_request
def after_request(response):
"""Automatically close db after each request"""
if ('db' in g) and (g.db is not None):
g.db.close()
return response

View File

@ -38,6 +38,19 @@ def login_required(view):
return wrapped_view
def anon_required(view):
"""View decorator that redirects authenticated users to the index."""
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is not None:
return redirect(url_for("albums.index"))
return view(**kwargs)
return wrapped_view
def admin_required(view):
"""View decorator that redirects anonymous users to the login page."""
@ -72,6 +85,7 @@ def load_logged_in_user():
@bp.route("/register", methods=("GET", "POST"))
@anon_required
def register():
"""Register a new user.
Validates that the username is not already taken. Hashes the
@ -115,6 +129,7 @@ def register():
@bp.route("/login", methods=("GET", "POST"))
@anon_required
def login():
"""Log in a registered user by adding the user id to the session."""
if request.method == "POST":

View File

@ -4,6 +4,7 @@ Module implémentant la recherche de partitions par mots-clés
"""
from uuid import uuid4
import urllib.request
import threading
import os
import googlesearch
@ -40,6 +41,18 @@ def local_search(query, partitions):
return sorted_partitions[:min(5,len(sorted_partitions))]
def download_search_result(element):
uuid = element["uuid"]
url = element["url"]
try:
urllib.request.urlretrieve(url, f"partitioncloud/search-partitions/{uuid}.pdf")
except (urllib.error.HTTPError, urllib.error.URLError) as e:
with open(f"partitioncloud/search-partitions/{uuid}.pdf",'a') as f:
pass # Create empty file
def online_search(query, num_queries):
"""
Renvoie les 3 résultats les plus pertinents depuis google
@ -65,26 +78,32 @@ def online_search(query, num_queries):
(uuid, element,)
)
db.commit()
urllib.request.urlretrieve(element, f"partitioncloud/search-partitions/{uuid}.pdf")
os.system(
f'/usr/bin/convert -thumbnail\
"178^>" -background white -alpha \
remove -crop 178x178+0+0 \
partitioncloud/search-partitions/{uuid}.pdf[0] \
partitioncloud/static/search-thumbnails/{uuid}.jpg'
)
partitions.append(
{
"name": element.split("://")[1].split("/")[0],
"uuid": uuid
"uuid": uuid,
"url": element
}
)
break
except db.IntegrityError:
pass
except (urllib.error.HTTPError, urllib.error.URLError) as e:
print(e, element)
threads = [threading.Thread(target=download_search_result, args=(elem,)) for elem in partitions]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
for element in partitions:
pass
uuid = element["uuid"]
url = element["url"]
if os.stat(f"partitioncloud/search-partitions/{uuid}.pdf").st_size == 0:
print("An error occured", url)
db.execute(
"""
DELETE FROM search_results
@ -93,7 +112,11 @@ def online_search(query, num_queries):
(uuid,)
)
db.commit()
break
os.remove(f"partitioncloud/search-partitions/{uuid}.pdf")
partitions.remove(element)
return partitions