myprecious/files.py (view raw)
1import os
2from contextlib import suppress
3from werkzeug.utils import secure_filename
4import myprecious.constants as c
5
6def get_title_platform(game_id, platform_id):
7 # TODO: use IGDB api to validate game_id, platform_id and return title and platform name
8 res = { "title": "title", "platform": "platform"}
9 if res is None:
10 raise Exception
11 return res["title"], res["platform"]
12
13def handle_upload(request, user_id):
14 f = request.files['file']
15 if f.filename is None:
16 return "Bad file upload."
17 try:
18 game_id = int(request.form['game_id'])
19 platform_id = int(request.form['platform_id'])
20 except ValueError:
21 return "Wrong parameter type."
22
23 try:
24 title, platform = get_title_platform(game_id, platform_id)
25 except Exception:
26 return "Bad or manipulated game data."
27
28 # TODO: save game in DB
29 # db.add_platform(platform_id, name)
30 # db.add_game(game_id, platform_id, title)
31 # db.add_save(user_id, game_id, platform_id, f.filename)
32
33 save_folder = os.path.join(c.BASE_DIRECTORY, c.CONTENT_DIRECTORY, str(user_id), str(game_id), str(platform_id))
34 with suppress(FileExistsError):
35 os.makedirs(save_folder)
36 save_file = os.path.join(save_folder, secure_filename(f.filename))
37 f.save(save_file)
38 return None