feat: add config item for maximum upload file size

This commit is contained in:
Terry Geng 2020-05-19 09:12:52 +08:00
parent aca002d08d
commit fae93d99e0
No known key found for this signature in database
GPG Key ID: F982F8EA1DF720E7
6 changed files with 50 additions and 11 deletions

View File

@ -199,17 +199,14 @@ def cmd_play(bot, user, text, command, parameter):
index = int(params[0])
else:
bot.send_msg(constants.strings('invalid_index', index=parameter), text)
return
if len(params) > 1:
_start_at = params[1]
match = re.search("(?:(\d\d):)?(?:(\d\d):)?(\d\d(?:\.\d*)?)", _start_at, flags=re.IGNORECASE)
if match:
if match[1] is None and match[2] is None:
start_at = float(match[3])
elif match[2] is None:
start_at = float(match[3]) + 60 * int(match[1])
else:
start_at = float(match[3]) + 60 * int(match[2]) + 3600 * int(match[2])
try:
start_at = util.parse_time(params[1])
except ValueError:
bot.send_msg(constants.strings('bad_parameter', command=command), text)
return
if len(var.playlist) > 0:
if index != -1:

View File

@ -104,6 +104,9 @@ access_address = http://127.0.0.1:8181
flask_secret = ChangeThisPassword
upload_enabled = True
maximum_upload_file_size = 30M
[debug]
# Set ffmpeg to True if you want to display DEBUG level log of ffmpeg.
ffmpeg = False

View File

@ -140,6 +140,11 @@ port = 64738
# !! YOU NEED TO CHANGE IT IF auth_method IS 'token'!!
# flask_secret = ChangeThisPassword
# 'upload_enabled': Enable the upload function of the web interface.
# 'maximum_upload_file_size': Unit can be 'B', 'KB', 'MB', 'GB', 'TB'.
#upload_enabled = True
#maximum_upload_file_size = 30MB
# [debug] stores some debug settings.
[debug]
# 'ffmpeg': Set ffmpeg to True if you want to display DEBUG level log of ffmpeg.

View File

@ -187,10 +187,13 @@ def index():
time.sleep(0.1)
tags_color_lookup = build_tags_color_lookup()
max_upload_file_size = util.parse_file_size(var.config.get("webinterface", "max_upload_file_size", fallback="30MB"))
return render_template('index.html',
dirs=get_all_dirs(),
upload_enabled=var.config.getboolean("webinterface", "upload_enabled", fallback=True),
tags_color_lookup=tags_color_lookup,
max_upload_file_size=max_upload_file_size
)
@ -614,6 +617,9 @@ def library():
def upload():
global log
if not var.config.getboolean("webinterface", "upload_enabled", fallback=True):
abort(403)
file = request.files['file']
if not file:
abort(400)

View File

@ -327,7 +327,7 @@
</div>
</div>
{% if upload_enabled %}
<div id="upload" class="container mb-3">
<div class="card">
@ -375,6 +375,8 @@
</div>
</div>
{% endif %}
<div class="container mb-5">
<div class="card-deck">
<div class="card">
@ -543,7 +545,7 @@
</div>
</div>
<input type="hidden" id="maxUploadFileSize" value="31457280" />
<input type="hidden" id="maxUploadFileSize" value="{{ max_upload_file_size }}" />
<script src="static/js/jquery-3.5.1.min.js"></script>
<script src="static/js/popper.min.js"></script>

26
util.py
View File

@ -353,6 +353,32 @@ def get_media_duration(path):
return 0
def parse_time(human):
match = re.search("(?:(\d\d):)?(?:(\d\d):)?(\d\d(?:\.\d*)?)", human, flags=re.IGNORECASE)
if match:
if match[1] is None and match[2] is None:
return float(match[3])
elif match[2] is None:
return float(match[3]) + 60 * int(match[1])
else:
return float(match[3]) + 60 * int(match[2]) + 3600 * int(match[2])
else:
raise ValueError("Invalid time string given.")
def parse_file_size(human):
units = {"B": 1, "KB": 1024, "MB": 1024*1024, "GB": 1024*1024*1024, "TB": 1024*1024*1024*1024,
"K": 1024, "M": 1024*1024, "G": 1024*1024*1024, "T": 1024*1024*1024*1024}
match = re.search("(\d+(?:\.\d*)?)\s*([A-Za-z]+)", human, flags=re.IGNORECASE)
if match:
num = float(match[1])
unit = match[2].upper()
if unit in units:
return int(num * units[unit])
raise ValueError("Invalid file size given.")
class LoggerIOWrapper(io.TextIOWrapper):
def __init__(self, logger: logging.Logger, logging_level, fallback_io_buffer):
super().__init__(fallback_io_buffer, write_through=True)