parent
a45ecc76b0
commit
48bf325d77
@ -1,49 +1,90 @@
|
||||
import os.path
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def message(category, text):
|
||||
if message.level <= 0:
|
||||
sep = " "
|
||||
else:
|
||||
sep = "--"
|
||||
print "%s %s%s[%s]%s%s" % (datetime.now().isoformat(), max(0, message.level) * " |", sep, category, max(1, (14 - len(category))) * " ", text)
|
||||
if message.level <= 0:
|
||||
sep = " "
|
||||
else:
|
||||
sep = "--"
|
||||
print "%s %s%s[%s]%s%s" % (
|
||||
datetime.now().isoformat(),
|
||||
max(0, message.level) * " |",
|
||||
sep,
|
||||
category,
|
||||
max(1, (14 - len(category))) * " ",
|
||||
text)
|
||||
message.level = -1
|
||||
|
||||
|
||||
def next_level():
|
||||
message.level += 1
|
||||
message.level += 1
|
||||
|
||||
|
||||
def back_level():
|
||||
message.level -= 1
|
||||
message.level -= 1
|
||||
|
||||
|
||||
def set_cache_path_base(base):
|
||||
trim_base.base = base
|
||||
trim_base.base = base
|
||||
|
||||
|
||||
def untrim_base(path):
|
||||
return os.path.join(trim_base.base, path)
|
||||
return os.path.join(trim_base.base, path)
|
||||
|
||||
|
||||
def trim_base_custom(path, base):
|
||||
if path.startswith(base):
|
||||
path = path[len(base):]
|
||||
if path.startswith('/'):
|
||||
path = path[1:]
|
||||
return path
|
||||
if path.startswith(base):
|
||||
path = path[len(base):]
|
||||
if path.startswith('/'):
|
||||
path = path[1:]
|
||||
return path
|
||||
|
||||
|
||||
def trim_base(path):
|
||||
return trim_base_custom(path, trim_base.base)
|
||||
return trim_base_custom(path, trim_base.base)
|
||||
|
||||
|
||||
def cache_base(path, filepath=False):
|
||||
if len(path) == 0:
|
||||
return "root"
|
||||
elif filepath and len(path.split(os.sep)) < 2:
|
||||
path = "root-" + path
|
||||
path = trim_base(path).replace('/', '-').replace(' ', '_').replace('(', '').replace('&', '').replace(',', '').replace(')', '').replace('#', '').replace('[', '').replace(']', '').replace('"', '').replace("'", '').replace('_-_', '-').lower()
|
||||
while path.find("--") != -1:
|
||||
path = path.replace("--", "-")
|
||||
while path.find("__") != -1:
|
||||
path = path.replace("__", "_")
|
||||
return path
|
||||
if len(path) == 0:
|
||||
return "root"
|
||||
elif filepath and len(path.split(os.sep)) < 2:
|
||||
path = "root-" + path
|
||||
path = trim_base(path).replace(
|
||||
'/', '-').replace(
|
||||
' ', '_').replace(
|
||||
'(', '').replace(
|
||||
'&', '').replace(
|
||||
',', '').replace(
|
||||
')', '').replace(
|
||||
'#', '').replace(
|
||||
'[', '').replace(
|
||||
']', '').replace(
|
||||
'"', '').replace(
|
||||
"'", '').replace(
|
||||
'_-_', '-').lower()
|
||||
while path.find("--") != -1:
|
||||
path = path.replace("--", "-")
|
||||
while path.find("__") != -1:
|
||||
path = path.replace("__", "_")
|
||||
return path
|
||||
|
||||
|
||||
def json_cache(path):
|
||||
return cache_base(path) + ".json"
|
||||
return cache_base(path) + ".json"
|
||||
|
||||
|
||||
def image_cache(path, size, square=False):
|
||||
if square:
|
||||
suffix = str(size) + "s"
|
||||
else:
|
||||
suffix = str(size)
|
||||
return cache_base(path, True) + "_" + suffix + ".jpg"
|
||||
if square:
|
||||
suffix = str(size) + "s"
|
||||
else:
|
||||
suffix = str(size)
|
||||
return cache_base(path, True) + "_" + suffix + ".jpg"
|
||||
|
||||
|
||||
def video_cache(path):
|
||||
return cache_base(path, True) + ".mp4"
|
||||
return cache_base(path, True) + ".mp4"
|
||||
|
||||
|
||||
def file_mtime(path):
|
||||
return datetime.fromtimestamp(int(os.path.getmtime(path)))
|
||||
return datetime.fromtimestamp(int(os.path.getmtime(path)))
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,118 +1,160 @@
|
||||
import os
|
||||
from mimetypes import guess_type
|
||||
from random import shuffle
|
||||
|
||||
from flask import Response, abort, json, jsonify, request
|
||||
from flask_login import current_user, login_user
|
||||
|
||||
from floatapp import app
|
||||
from floatapp.login import admin_required, login_required, is_authenticated, query_is_photo_user, query_is_admin_user, photo_user, admin_user
|
||||
from floatapp.jsonp import jsonp
|
||||
from floatapp.login import (admin_required, admin_user, is_authenticated,
|
||||
login_required, photo_user, query_is_admin_user,
|
||||
query_is_photo_user)
|
||||
from process import send_process
|
||||
from TreeWalker import TreeWalker
|
||||
from flask import Response, abort, json, request, jsonify
|
||||
from flask_login import login_user, current_user
|
||||
from random import shuffle
|
||||
import os
|
||||
from mimetypes import guess_type
|
||||
|
||||
cwd = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
@app.route("/scan")
|
||||
@admin_required
|
||||
def scan_photos():
|
||||
global cwd
|
||||
response = send_process([ "stdbuf", "-oL", os.path.abspath(os.path.join(cwd, "../main.py")),
|
||||
os.path.abspath(app.config["ALBUM_PATH"]), os.path.abspath(app.config["CACHE_PATH"]) ],
|
||||
os.path.join(cwd, "scanner.pid"))
|
||||
response.headers.add("X-Accel-Buffering", "no")
|
||||
response.cache_control.no_cache = True
|
||||
return response
|
||||
global cwd
|
||||
response = send_process([
|
||||
"stdbuf",
|
||||
"-oL",
|
||||
os.path.abspath(os.path.join(cwd, "../venv/bin/python")),
|
||||
os.path.abspath(os.path.join(cwd, "../main.py")),
|
||||
os.path.abspath(app.config["ALBUM_PATH"]),
|
||||
os.path.abspath(app.config["CACHE_PATH"])
|
||||
],
|
||||
os.path.join(cwd, "scanner.pid"))
|
||||
response.headers.add("X-Accel-Buffering", "no")
|
||||
response.cache_control.no_cache = True
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/auth")
|
||||
def login():
|
||||
success = False
|
||||
if current_user.is_authenticated():
|
||||
success = True
|
||||
elif query_is_photo_user(request.form) or query_is_photo_user(request.args):
|
||||
success = login_user(photo_user, remember=True)
|
||||
elif query_is_admin_user(request.form) or query_is_admin_user(request.args):
|
||||
success = login_user(admin_user, remember=True)
|
||||
if not success:
|
||||
abort(403)
|
||||
return ""
|
||||
success = False
|
||||
if current_user.is_authenticated:
|
||||
success = True
|
||||
elif (query_is_photo_user(request.form) or
|
||||
query_is_photo_user(request.args)):
|
||||
success = login_user(photo_user, remember=True)
|
||||
elif (query_is_admin_user(request.form) or
|
||||
query_is_admin_user(request.args)):
|
||||
success = login_user(admin_user, remember=True)
|
||||
if not success:
|
||||
abort(403)
|
||||
return ""
|
||||
|
||||
|
||||
def cache_base(path):
|
||||
path = path.replace('/', '-').replace(' ', '_').replace('(', '').replace('&', '').replace(',', '').replace(')', '').replace('#', '').replace('[', '').replace(']', '').replace('"', '').replace("'", '').replace('_-_', '-').lower()
|
||||
while path.find("--") != -1:
|
||||
path = path.replace("--", "-")
|
||||
while path.find("__") != -1:
|
||||
path = path.replace("__", "_")
|
||||
if len(path) == 0:
|
||||
path = "root"
|
||||
return path
|
||||
|
||||
auth_list = [ ]
|
||||
path = path.replace(
|
||||
'/', '-').replace(
|
||||
' ', '_').replace(
|
||||
'(', '').replace(
|
||||
'&', '').replace(
|
||||
',', '').replace(
|
||||
')', '').replace(
|
||||
'#', '').replace(
|
||||
'[', '').replace(
|
||||
']', '').replace(
|
||||
'"', '').replace(
|
||||
"'", '').replace(
|
||||
'_-_', '-').lower()
|
||||
|
||||
while path.find("--") != -1:
|
||||
path = path.replace("--", "-")
|
||||
|
||||
while path.find("__") != -1:
|
||||
path = path.replace("__", "_")
|
||||
|
||||
if len(path) == 0:
|
||||
path = "root"
|
||||
|
||||
return path
|
||||
|
||||
|
||||
auth_list = []
|
||||
|
||||
|
||||
def read_auth_list():
|
||||
global auth_list, cwd
|
||||
f = open(os.path.join(cwd, "auth.txt"), "r")
|
||||
paths = [ ]
|
||||
for path in f:
|
||||
path = path.strip()
|
||||
paths.append(path)
|
||||
paths.append(cache_base(path))
|
||||
f.close()
|
||||
auth_list = paths
|
||||
global auth_list, cwd
|
||||
f = open(os.path.join(cwd, "auth.txt"), "r")
|
||||
paths = []
|
||||
for path in f:
|
||||
path = path.strip()
|
||||
paths.append(path)
|
||||
paths.append(cache_base(path))
|
||||
f.close()
|
||||
auth_list = paths
|
||||
|
||||
|
||||
# TODO: Make this run via inotify
|
||||
read_auth_list()
|
||||
|
||||
|
||||
def check_permissions(path):
|
||||
if not is_authenticated():
|
||||
for auth_path in auth_list:
|
||||
if path.startswith(auth_path):
|
||||
abort(403)
|
||||
if not is_authenticated():
|
||||
for auth_path in auth_list:
|
||||
if path.startswith(auth_path):
|
||||
abort(403)
|
||||
|
||||
|
||||
@app.route("/albums/<path:path>")
|
||||
def albums(path):
|
||||
check_permissions(path)
|
||||
return accel_redirect(app.config["ALBUM_ACCEL"], app.config["ALBUM_PATH"], path)
|
||||
check_permissions(path)
|
||||
return accel_redirect(
|
||||
app.config["ALBUM_ACCEL"], app.config["ALBUM_PATH"], path)
|
||||
|
||||
|
||||
@app.route("/cache/<path:path>")
|
||||
def cache(path):
|
||||
check_permissions(path)
|
||||
return accel_redirect(app.config["CACHE_ACCEL"], app.config["CACHE_PATH"], path)
|
||||
check_permissions(path)
|
||||
return accel_redirect(
|
||||
app.config["CACHE_ACCEL"], app.config["CACHE_PATH"], path)
|
||||
|
||||
|
||||
def accel_redirect(internal, real, relative_name):
|
||||
real_path = os.path.join(real, relative_name)
|
||||
internal_path = os.path.join(internal, relative_name)
|
||||
if not os.path.isfile(real_path):
|
||||
abort(404)
|
||||
mimetype = None
|
||||
types = guess_type(real_path)
|
||||
if len(types) != 0:
|
||||
mimetype = types[0]
|
||||
response = Response(mimetype=mimetype)
|
||||
response.headers.add("X-Accel-Redirect", internal_path)
|
||||
response.cache_control.public = True
|
||||
if mimetype == "application/json":
|
||||
response.cache_control.max_age = 3600
|
||||
else:
|
||||
response.cache_control.max_age = 29030400
|
||||
return response
|
||||
real_path = os.path.join(real, relative_name)
|
||||
internal_path = os.path.join(internal, relative_name)
|
||||
if not os.path.isfile(real_path):
|
||||
abort(404)
|
||||
mimetype = None
|
||||
types = guess_type(real_path)
|
||||
if len(types) != 0:
|
||||
mimetype = types[0]
|
||||
response = Response(mimetype=mimetype)
|
||||
response.headers.add("X-Accel-Redirect", internal_path)
|
||||
response.cache_control.public = True
|
||||
if mimetype == "application/json":
|
||||
response.cache_control.max_age = 3600
|
||||
else:
|
||||
response.cache_control.max_age = 29030400
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/photos")
|
||||
@jsonp
|
||||
def photos():
|
||||
f = open(os.path.join(app.config["CACHE_PATH"], "all_photos.json"), "r")
|
||||
photos = json.load(f)
|
||||
f.close()
|
||||
if not is_authenticated():
|
||||
def allowed(photo):
|
||||
for auth_path in auth_list:
|
||||
if photo.startswith(auth_path):
|
||||
return False
|
||||
return True
|
||||
photos = [photo for photo in photos if allowed(photo)]
|
||||
count = int(request.args.get("count", len(photos)))
|
||||
random = request.args.get("random") == "true"
|
||||
if random:
|
||||
shuffle(photos)
|
||||
else:
|
||||
photos.reverse()
|
||||
response = jsonify(photos=photos[0:count])
|
||||
response.cache_control.no_cache = True
|
||||
return response
|
||||
f = open(os.path.join(app.config["CACHE_PATH"], "all_photos.json"), "r")
|
||||
photos = json.load(f)
|
||||
f.close()
|
||||
if not is_authenticated():
|
||||
def allowed(photo):
|
||||
for auth_path in auth_list:
|
||||
if photo.startswith(auth_path):
|
||||
return False
|
||||
return True
|
||||
photos = [photo for photo in photos if allowed(photo)]
|
||||
count = int(request.args.get("count", len(photos)))
|
||||
random = request.args.get("random") == "true"
|
||||
if random:
|
||||
shuffle(photos)
|
||||
else:
|
||||
photos.reverse()
|
||||
response = jsonify(photos=photos[0:count])
|
||||
response.cache_control.no_cache = True
|
||||
return response
|
||||
|
Loading…
Reference in new issue