all repos — myprecious @ b801eb597c454df07865ba1a9678728bd8f71e15

A lightweight web service to backup precious game saves.

myprecious/__main__.py (view raw)

 1from flask import Flask, request, render_template, url_for
 2from werkzeug.utils import secure_filename
 3from contextlib import suppress
 4import os
 5app = Flask(__name__)
 6
 7content_folder = "content"
 8user = "birabittoh"
 9
10def handle_upload(game_id, file):
11    save_folder = os.path.join(content_folder, user, game_id)
12    with suppress(FileExistsError):
13        os.makedirs(save_folder)
14    save_file = os.path.join(save_folder, secure_filename(f.filename))
15    f.save(save_file)
16    return 'file uploaded successfully'
17
18@app.route('/')
19def route_main():
20    return render_template("index.html")
21
22@app.route('/uploader', methods = ['GET', 'POST'])
23def upload_file():
24    if request.method == 'GET':
25        return 'Please, use POST.'
26    f = request.files['file']
27    game_id = request.form['game_id']
28    return handle_upload(game_id, f)
29
30if __name__ == "__main__":
31    app.run(port=1111)