Save v3 renaming support

This commit is contained in:
somebody
2022-11-28 17:01:30 -06:00
parent 0f8a669eac
commit e7930101c1

View File

@@ -7,6 +7,7 @@
# External packages # External packages
from dataclasses import dataclass from dataclasses import dataclass
import shutil
import eventlet import eventlet
eventlet.monkey_patch(all=True, thread=False, os=False) eventlet.monkey_patch(all=True, thread=False, os=False)
import os, inspect import os, inspect
@@ -7954,28 +7955,46 @@ def popup_rename(data):
@logger.catch @logger.catch
def popup_rename_story(data): def popup_rename_story(data):
if 'popup_renameable' not in session: if 'popup_renameable' not in session:
print("Someone is trying to rename a file in your server. Blocked.") logger.warn("Someone is trying to rename a file in your server. Blocked.")
return return
if not session['popup_renameable']: if not session['popup_renameable']:
print("Someone is trying to rename a file in your server. Blocked.") logger.warn("Someone is trying to rename a file in your server. Blocked.")
return return
if session['popup_jailed_dir'] and session["popup_jailed_dir"] not in data["file"]:
if session['popup_jailed_dir'] is None or session["popup_jailed_dir"] in data["file"]: logger.warn("User is trying to rename files in your server outside the jail. Blocked. Jailed Dir: {} Requested Dir: {}".format(session['popup_jailed_dir'], data['file']))
path = data["file"] return
print("path", path)
#if we're using a v2 file we can't just rename the file as the story name is in the file path = data["file"]
with open(data['file'], 'r') as f: new_name = data["new_name"]
json_data = json.load(f) json_path = path
if 'story_name' in json_data: is_v3 = False
json_data['story_name'] = data['new_name']
# Handle directory for v3 save
new_filename = os.path.join(os.path.dirname(os.path.abspath(data['file'])), data['new_name']+".json") if os.path.isdir(path):
if not valid_v3_story(path):
return
is_v3 = True
json_path = os.path.join(path, "story.json")
#if we're using a v2 file we can't just rename the file as the story name is in the file
with open(json_path, 'r') as f:
json_data = json.load(f)
if 'story_name' in json_data:
json_data['story_name'] = new_name
# For v3 we move the directory, not the json file.
if is_v3:
target = os.path.join(os.path.dirname(path), new_name)
shutil.move(path, target)
with open(os.path.join(target, "story.json"), "w") as file:
json.dump(json_data, file)
else:
new_filename = os.path.join(os.path.dirname(os.path.abspath(data['file'])), new_name+".json")
os.remove(data['file']) os.remove(data['file'])
with open(new_filename, "w") as f: with open(new_filename, "w") as f:
json.dump(json_data, f) json.dump(json_data, f)
get_files_folders(os.path.dirname(data['file'])) get_files_folders(os.path.dirname(path))
else:
print("User is trying to rename files in your server outside the jail. Blocked. Jailed Dir: {} Requested Dir: {}".format(session['popup_jailed_dir'], data['file']))
@socketio.on('popup_delete') @socketio.on('popup_delete')
@logger.catch @logger.catch