SMall tweaks on the UI + edit the ConfigFiles edits

This commit is contained in:
TheophileDiot 2022-11-09 16:33:02 +01:00
parent 0811aad7f5
commit f65a4cdd65
3 changed files with 41 additions and 26 deletions

View File

@ -575,6 +575,10 @@ def configs():
if variables["type"] == "file":
variables["name"] = f"{variables['name']}.conf"
if "old_name" in variables:
variables["old_name"] = f"{variables['old_name']}.conf"
variables["content"] = BeautifulSoup(
variables["content"], "html.parser"
).get_text()
@ -591,11 +595,16 @@ def configs():
elif request.form["operation"] == "edit":
if variables["type"] == "folder":
operation, error = app.config["CONFIGFILES"].edit_folder(
variables["path"], variables["name"]
variables["path"],
variables["name"],
variables.get("old_name", variables["name"]),
)
elif variables["type"] == "file":
operation, error = app.config["CONFIGFILES"].edit_file(
variables["path"], variables["name"], variables["content"]
variables["path"],
variables["name"],
variables.get("old_name", variables["name"]),
variables["content"],
)
if error:
@ -979,7 +988,6 @@ def plugins():
plugins_pages = app.config["CONFIG"].get_plugins_pages()
pages = []
active = True
for page in plugins_pages:
with open(
f"/opt/bunkerweb/"
@ -1007,11 +1015,8 @@ def plugins():
url_for=url_for,
**app.config["PLUGIN_ARGS"]["args"],
),
# Only the first plugin page is active
"active": active,
}
)
active = False
app.config["PLUGIN_ARGS"] = None

View File

@ -103,55 +103,65 @@ class ConfigFiles:
def create_file(self, path: str, name: str, content: str) -> Tuple[str, int]:
file_path = join(path, name)
mkdir(path, exist_ok=True)
with open(file_path, "w") as f:
f.write(content)
return f"The file {file_path} was successfully created", 0
def edit_folder(self, path: str, name: str) -> Tuple[str, int]:
def edit_folder(self, path: str, name: str, old_name: str) -> Tuple[str, int]:
new_folder_path = dirname(join(path, name))
old_folder_path = dirname(join(path, old_name))
if path == new_folder_path:
if old_folder_path == new_folder_path:
return (
f"{path} was not renamed because the name didn't change",
f"{old_folder_path} was not renamed because the name didn't change",
0,
)
try:
shutil_move(path, new_folder_path)
shutil_move(old_folder_path, new_folder_path)
except OSError:
return f"Could not move {path}", 1
return f"Could not move {old_folder_path}", 1
return f"The folder {path} was successfully renamed to {new_folder_path}", 0
return (
f"The folder {old_folder_path} was successfully renamed to {new_folder_path}",
0,
)
def edit_file(self, path: str, name: str, content: str) -> Tuple[str, int]:
def edit_file(
self, path: str, name: str, old_name: str, content: str
) -> Tuple[str, int]:
new_path = dirname(join(path, name))
old_path = dirname(join(path, old_name))
try:
with open(path, "r") as f:
with open(old_path, "r") as f:
file_content = f.read()
except FileNotFoundError:
return f"Could not find {path}", 1
return f"Could not find {old_path}", 1
if path == new_path and file_content == content:
if old_path == new_path and file_content == content:
return (
f"{path} was not edited because the content and the name didn't change",
f"{old_path} was not edited because the content and the name didn't change",
0,
)
elif file_content == content:
try:
replace(path, new_path)
return f"{path} was successfully renamed to {new_path}", 0
return f"{old_path} was successfully renamed to {new_path}", 0
except OSError:
return f"Could not rename {path} into {new_path}", 1
elif path == new_path:
new_path = path
return f"Could not rename {old_path} into {new_path}", 1
elif old_path == new_path:
new_path = old_path
else:
try:
remove(path)
remove(old_path)
except OSError:
return f"Could not remove {path}", 1
return f"Could not remove {old_path}", 1
with open(new_path, "w") as f:
f.write(content)
return f"The file {path} was successfully edited", 0
return f"The file {old_path} was successfully edited", 0

View File

@ -363,7 +363,7 @@ def path_to_dict(
{
"type": "file",
"path": path,
"can_download": is_cache,
"can_download": True,
}
)
@ -429,7 +429,7 @@ def path_to_dict(
"type": "file",
"path": f"{path}/{type_lower}{'/' + conf['service_id'] if conf['service_id'] else ''}/{conf['name']}.conf",
"can_edit": conf["method"] == "ui",
"can_download": is_cache,
"can_download": True,
"content": b64encode(conf["data"]).decode("utf-8"),
}