-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
72 lines (59 loc) · 2.61 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import fcntl, json, os, syslog
from flask import Flask, make_response, render_template, request, send_from_directory, safe_join
from jsonschema import validate, ValidationError
from config_schema import config_schema
from threading import Thread
class WebHandler(Thread):
def __init__(self):
super().__init__()
self.app = Flask(__name__)
@self.app.route('/')
def index():
return render_template('index.html')
@self.app.route('/config')
def get_config():
try:
with open('config.json', 'r') as file:
fcntl.flock(file.fileno(), fcntl.LOCK_EX)
config = file.read()
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
return config
except Exception as e:
syslog.syslog(syslog.LOG_INFO, f'Error opening config: {e}')
return ''
@self.app.route('/update', methods=['POST'])
def update():
try:
config = json.loads(request.form['config'])
validate(instance=config, schema=config_schema)
with open('config.json', 'w') as file:
fcntl.flock(file.fileno(), fcntl.LOCK_EX)
file.write(json.dumps(config))
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
return 'Config updated successfully.'
except Exception as e:
syslog.syslog(syslog.LOG_INFO, f'Error opening config: {e}')
return 'Config update failed.'
@self.app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No filename'
if file.content_type != 'image/png':
return 'Only PNG files are allowed'
if len(file.read()) > 1 * 1024 * 1024: # 1MB limit
return 'File size exceeds 1MB limit'
filename = safe_join('images', file.filename)
file.seek(0)
file.save(filename)
return 'File uploaded successfully'
@self.app.route('/images/<path:path>')
def send_images(path):
response = make_response(send_from_directory('images', path))
response.headers['Cache-Control'] = 'no-store'
return response
@self.app.route('/default_images/<path:path>')
def send_default_images(path):
return send_from_directory('default_images', path)