imgin/imgin/__init__.py

77 lines
1.6 KiB
Python
Raw Normal View History

2021-10-03 00:56:33 +00:00
from gevent import monkey
from gevent import sleep
monkey.patch_all()
from threading import Thread
2021-10-03 22:26:25 +00:00
from os import remove, mkdir, path, stat
2021-10-03 00:56:33 +00:00
from shutil import rmtree
from glob import glob
from uuid import uuid4
from bottle import route, run
from bottle import static_file
from bottle import SimpleTemplate
from .get import get
from .config import IMAGE_CACHE, template_dir
2021-10-03 00:56:33 +00:00
2021-10-03 22:26:25 +00:00
def get_timestamp_of_file(file):
return stat(file).st_ctime
2021-10-03 00:56:33 +00:00
def album(id):
req_id = str(uuid4())
req = IMAGE_CACHE
get("/a/" + id, req)
imgs = glob(req + "*")
2021-10-03 22:26:25 +00:00
# sort image order (file creation time)
imgs = sorted(imgs, key=get_timestamp_of_file)
2021-10-03 00:56:33 +00:00
for c, img in enumerate(imgs):
imgs[c] = img.replace(IMAGE_CACHE, '/')
2021-10-03 22:26:25 +00:00
2021-10-03 00:56:33 +00:00
with open(f'{template_dir}gallery.html', 'r') as img_view:
tpl = SimpleTemplate(img_view)
return tpl.render(imgs=imgs)
@route('/')
@route('')
def home():
return static_file("index.html", root=template_dir)
@route('/static/<file>')
def static(file=''):
return static_file(file, root=template_dir)
@route('/gallery/<id>')
def gallery(id=''):
return album(id)
@route('/a/<id>')
def gallery(id=''):
return album(id)
@route('/<img>')
def img(img=''):
if not img.endswith("jpeg") and not img.endswith("jpg") and not img.endswith("png"):
img = img + ".jpg"
2021-10-03 00:56:33 +00:00
if not path.exists(IMAGE_CACHE + img):
get(img, IMAGE_CACHE)
return static_file(img, root=IMAGE_CACHE)
def start_server():
try:
rmtree(IMAGE_CACHE)
except FileNotFoundError:
pass
mkdir(IMAGE_CACHE)
2021-10-03 07:07:12 +00:00
run(server='gevent', host='0.0.0.0')