Inquire the server about current image dir (#313)

This commit is contained in:
Daniel Markstedt 2021-10-13 17:54:29 -07:00 committed by GitHub
parent c68c17e366
commit fa12f49078
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 13 deletions

View File

@ -2,6 +2,7 @@ import os
import logging
from ractl_cmds import (
get_server_info,
attach_image,
detach_all,
list_devices,
@ -132,9 +133,11 @@ def unzip_file(file_name):
Returns dict with boolean status and str msg
"""
from subprocess import run
server_info = get_server_info()
unzip_proc = run(
["unzip", "-d", base_dir, "-o", "-j", base_dir + file_name], capture_output=True
["unzip", "-d", server_info["image_dir"], "-o", "-j", \
server_info["image_dir"] + file_name], capture_output=True
)
if unzip_proc.returncode != 0:
logging.warning(f"Unzipping failed: {unzip_proc}")
@ -153,12 +156,14 @@ def download_file_to_iso(scsi_id, url):
import time
from subprocess import run
server_info = get_server_info()
file_name = url.split("/")[-1]
tmp_ts = int(time.time())
tmp_dir = "/tmp/" + str(tmp_ts) + "/"
os.mkdir(tmp_dir)
tmp_full_path = tmp_dir + file_name
iso_filename = base_dir + file_name + ".iso"
iso_filename = server_info["image_dir"] + file_name + ".iso"
try:
urllib.request.urlretrieve(url, tmp_full_path)
@ -187,8 +192,10 @@ def download_image(url):
import urllib.request
import urllib.error as error
server_info = get_server_info()
file_name = url.split("/")[-1]
full_path = base_dir + file_name
full_path = server_info["image_dir"] + file_name
try:
urllib.request.urlretrieve(url, full_path)

View File

@ -27,6 +27,7 @@ def get_server_info():
log_levels = result.server_info.log_level_info.log_levels
current_log_level = result.server_info.log_level_info.current_log_level
reserved_ids = list(result.server_info.reserved_ids_info.ids)
image_dir = result.server_info.image_files_info.default_image_folder
# Creates lists of file endings recognized by RaSCSI
mappings = result.server_info.mapping_info.mapping
@ -53,6 +54,7 @@ def get_server_info():
"log_levels": log_levels,
"current_log_level": current_log_level,
"reserved_ids": reserved_ids,
"image_dir": image_dir,
"sahd": sahd,
"schd": schd,
"scrm": scrm,

View File

@ -1,6 +1,5 @@
from os import getenv, getcwd
base_dir = getenv("BASE_DIR", "/home/pi/images/")
cfg_dir = getenv("HOME", "/home/pi/") + ".config/rascsi/"
home_dir = getcwd()

View File

@ -63,8 +63,8 @@ def index():
config_files = list_config_files()
drive_files = list_files(tuple(server_info["sahd"] + \
server_info["schd"] + server_info["scrm"] + \
server_info["scmo"]), base_dir)
cdrom_files = list_files(tuple(server_info["sccd"]), base_dir)
server_info["scmo"]), server_info["image_dir"])
cdrom_files = list_files(tuple(server_info["sccd"]), server_info["image_dir"])
sorted_image_files = sorted(files["files"], key = lambda x: x["name"].lower())
sorted_config_files = sorted(config_files, key = lambda x: x.lower())
@ -95,7 +95,7 @@ def index():
config_files=sorted_config_files,
drive_files=drive_files,
cdrom_files=cdrom_files,
base_dir=base_dir,
base_dir=server_info["image_dir"],
cfg_dir=cfg_dir,
scsi_ids=scsi_ids,
recommended_id=recommended_id,
@ -163,7 +163,7 @@ def drive_list():
return render_template(
"drives.html",
files=sorted_image_files,
base_dir=base_dir,
base_dir=server_info["image_dir"],
hd_conf=hd_conf,
cd_conf=cd_conf,
rm_conf=rm_conf,
@ -506,7 +506,9 @@ def upload_file():
file = request.files["file"]
filename = secure_filename(file.filename)
save_path = path.join(app.config["UPLOAD_FOLDER"], filename)
server_info = get_server_info()
save_path = path.join(server_info["image_dir"], filename)
current_chunk = int(request.form['dzchunkindex'])
# Makes sure not to overwrite an existing file,
@ -581,10 +583,13 @@ def image_padding():
else:
target_size = int(size) - (int(size) % int(multiple)) + int(multiple)
server_info = get_server_info()
from pathlib import PurePath
padded_image = base_dir + str(PurePath(file).stem) + "_padded" + str(PurePath(file).suffix)
padded_image = server_info["image_dir"] + str(PurePath(file).stem) + \
"_padded" + str(PurePath(file).suffix)
from shutil import copyfile
copyfile(base_dir + file, padded_image)
copyfile(server_info["image_dir"] + file, padded_image)
process = pad_image(padded_image, target_size)
if process["status"] == True:
@ -600,7 +605,8 @@ def image_padding():
@app.route("/files/download", methods=["POST"])
def download():
image = request.form.get("image")
return send_file(base_dir + image, as_attachment=True)
server_info = get_server_info()
return send_file(server_info["image_dir"] + image, as_attachment=True)
@app.route("/files/delete", methods=["POST"])
@ -658,7 +664,9 @@ def show_properties():
if __name__ == "__main__":
app.secret_key = "rascsi_is_awesome_insecure_secret_key"
app.config["SESSION_TYPE"] = "filesystem"
app.config["UPLOAD_FOLDER"] = base_dir
server_info = get_server_info()
app.config["UPLOAD_FOLDER"] = server_info["image_dir"]
from os import makedirs
makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)