generate hashes on server

This commit is contained in:
Bui 2020-03-17 01:27:06 +00:00
parent 5a68978ec4
commit fe90a35625
5 changed files with 54 additions and 5 deletions

38
app.py
View File

@ -1,9 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import base64
import bcrypt import bcrypt
import hashlib
import config import config
import json import json
import re import re
import requests
import schema import schema
import os import os
@ -28,10 +31,34 @@ db = client[config.MONGO['database']]
db.users.create_index('username', unique=True) db.users.create_index('username', unique=True)
db.songs.create_index('id', unique=True) db.songs.create_index('id', unique=True)
class HashException(Exception):
pass
def api_error(message): def api_error(message):
return jsonify({'status': 'error', 'message': message}) return jsonify({'status': 'error', 'message': message})
def generate_hash(id, form):
md5 = hashlib.md5()
if form['type'] == 'tja':
urls = ['%s%s/main.tja' % (config.SONGS_BASEURL, id)]
else:
urls = []
for diff in ['easy', 'normal', 'hard', 'oni', 'ura']:
if form['course_' + diff]:
urls.append('%s%s/%s.osu' % (config.SONGS_BASEURL, id, diff))
for url in urls:
resp = requests.get(url)
if resp.status_code != 200:
raise Exception('Invalid response from %s (status code %s)' % (resp.url, resp.status_code))
md5.update(resp.content)
return base64.b64encode(md5.digest())[:-2].decode('utf-8')
def login_required(f): def login_required(f):
@wraps(f) @wraps(f)
def decorated_function(*args, **kwargs): def decorated_function(*args, **kwargs):
@ -69,7 +96,8 @@ def get_config():
'songs_baseurl': config.SONGS_BASEURL, 'songs_baseurl': config.SONGS_BASEURL,
'assets_baseurl': config.ASSETS_BASEURL, 'assets_baseurl': config.ASSETS_BASEURL,
'email': config.EMAIL, 'email': config.EMAIL,
'accounts': config.ACCOUNTS 'accounts': config.ACCOUNTS,
'custom_js': config.CUSTOM_JS
} }
if not config_out.get('songs_baseurl'): if not config_out.get('songs_baseurl'):
@ -218,6 +246,14 @@ def route_admin_songs_id_post(id):
output['preview'] = float(request.form.get('preview')) or None output['preview'] = float(request.form.get('preview')) or None
output['volume'] = float(request.form.get('volume')) or None output['volume'] = float(request.form.get('volume')) or None
output['maker_id'] = int(request.form.get('maker_id')) or None output['maker_id'] = int(request.form.get('maker_id')) or None
output['hash'] = request.form.get('hash')
if request.form.get('gen_hash'):
try:
output['hash'] = generate_hash(id, request.form)
except HashException as e:
flash('An error occurred: %s' % str(e), 'error')
return redirect('/admin/songs/%s' % id)
db.songs.update_one({'id': id}, {'$set': output}) db.songs.update_one({'id': id}, {'$set': output})
flash('Changes saved.') flash('Changes saved.')

View File

@ -10,9 +10,12 @@ EMAIL = 'taiko@example.com'
# Whether to use the user account system. # Whether to use the user account system.
ACCOUNTS = True ACCOUNTS = True
# Custom JavaScript file to load with the simulator.
CUSTOM_JS = ''
# MongoDB server settings. # MongoDB server settings.
MONGO = { MONGO = {
'host': ['localhost:27017'], 'host': ['127.0.0.1:27017'],
'database': 'taiko' 'database': 'taiko'
} }

View File

@ -131,6 +131,10 @@ h1 small {
color: white; color: white;
} }
.message-error {
background: #b92222;
}
.save-song { .save-song {
font-size: 22pt; font-size: 22pt;
width: 120px; width: 120px;

View File

@ -13,7 +13,7 @@
<a href="/admin/songs">Songs</a> <a href="/admin/songs">Songs</a>
</div> </div>
</header> </header>
<main> <main>
<div class="container"> <div class="container">
{% block content %}{% endblock %} {% block content %}{% endblock %}

View File

@ -1,8 +1,8 @@
{% extends 'admin.html' %} {% extends 'admin.html' %}
{% block content %} {% block content %}
<h1>{{ song.title }} <small>(ID: {{ song.id }})</small></h1> <h1>{{ song.title }} <small>(ID: {{ song.id }})</small></h1>
{% for message in get_flashed_messages() %} {% for cat, message in get_flashed_messages(with_categories=true) %}
<div class="message">{{ message }}</div> <div class="message{% if cat %} message-{{cat}}{% endif %}">{{ message }}</div>
{% endfor %} {% endfor %}
<div class="song-form"> <div class="song-form">
<form method="post"> <form method="post">
@ -115,6 +115,11 @@
</select> </select>
</div> </div>
<div class="form-field">
<p><label for="hash">Hash</label></p>
<input type="text" id="hash" value="{{song.hash}}" name="hash"> <span class="checkbox"><input type="checkbox" name="gen_hash" id="gen_hash"{><label for="gen_hash"> Generate</label></span>
</div>
<button type="submit" class="save-song">Save</button> <button type="submit" class="save-song">Save</button>
</form> </form>
{% if admin.user_level >= 100 %} {% if admin.user_level >= 100 %}
@ -124,3 +129,4 @@
{% endif %} {% endif %}
</div> </div>
{% endblock %} {% endblock %}