imgs/imgs.py

98 lines
2.9 KiB
Python
Raw Normal View History

2022-01-05 08:18:54 +03:00
__version__ = '1.1'
2021-08-08 00:33:09 +03:00
import os
import random
import string
from bottle import default_app as app
from bottle import run, get, post, request, response, error, template, static_file
config = app().config.load_config('./imgs.ini')
def generate_image_name(image: str) -> str:
name = ''
chars = string.ascii_letters + string.digits + '-_'
while len(name) <= int(config['imgs.image_name_lenght']) - 1:
name = name + random.choice(chars)
return name + os.path.splitext(image)[1].lower()
def get_base_url():
try:
base_url = config['imgs.base_url']
except KeyError:
base_url = request.url
return base_url
def get_image_url(image_name: str) -> str:
image_url = get_base_url() + '/' + image_name
return image_url.replace('//', '/').replace(':/', '://')
def upload_file(file):
image_name = generate_image_name(file.filename)
file.save(os.path.join(config['imgs.uploads_dir'], image_name))
return image_name
@error(404)
def error404(error):
return template('index.tpl',
uploaded = False, not_found = True, bad_mime_type = False,
base_url = get_base_url())
@get('/')
def index():
return template('index.tpl',
uploaded = False, not_found = False, bad_mime_type = False,
base_url = get_base_url())
@post('/')
def upload_image():
# Handle request from CLI
if request.files.get('image'):
file = request.files.get('image')
rq = 'cli'
2021-08-08 00:33:09 +03:00
# Handle request from web-browser
elif request.files.get('image_web'):
file = request.files.get('image_web')
rq = 'web'
if config['imgs.allowed_mime_types'] == '*':
# Skip MIME checking.
image_name = upload_file(file)
else:
2021-08-08 00:33:09 +03:00
if file.content_type in config['imgs.allowed_mime_types']:
# Upload file!
2021-08-08 00:33:09 +03:00
image_name = upload_file(file)
else:
# Show MIME type error!
2021-08-08 00:33:09 +03:00
# Prevent recource leek. Force close buffered file
request.body.close()
response.status = 415
if rq == 'cli':
return 'Error: bad file MIME type\n'
else:
return template('index.tpl',
uploaded = False, not_found = False, bad_mime_type = True,
allowed_mime_types = config['imgs.allowed_mime_types'],
base_url = get_base_url(), image_url = 'None')
# Return 200 OK
if rq == 'cli':
return get_image_url(image_name) + '\n'
else:
return template('index.tpl',
uploaded = True, not_found = False, bad_mime_type = False,
base_url = get_base_url(), image_url = get_image_url(image_name))
2021-08-08 00:33:09 +03:00
@get('/<image_name>')
def send_image(image_name):
return static_file(image_name, root = config['imgs.uploads_dir'])
@get('/style.css')
def send_style():
return static_file('style.css', root = './')
2022-01-05 09:31:27 +03:00
app = app() # Create WSGI application
2021-08-08 00:33:09 +03:00
if __name__ == '__main__':
2022-01-05 07:52:05 +03:00
run()