diff --git a/easyinstall.sh b/easyinstall.sh index 499476d0..37084a61 100755 --- a/easyinstall.sh +++ b/easyinstall.sh @@ -710,6 +710,29 @@ function setupWirelessNetworking() { sudo reboot } +# Detects or creates the file sharing directory +function createFileSharingDir() { + if [ ! -d "$FILE_SHARE_PATH" ] && [ -d "$HOME/afpshare" ]; then + echo + echo "File server dir $HOME/afpshare detected. This script will rename it to $FILE_SHARE_PATH." + echo + echo "Do you want to proceed with the installation? [y/N]" + read -r REPLY + if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then + sudo mv "$HOME/afpshare" "$FILE_SHARE_PATH" || exit 1 + else + exit 0 + fi + elif [ -d "$FILE_SHARE_PATH" ]; then + echo "Found a $FILE_SHARE_PATH directory; will use it for file sharing." + else + echo "Creating the $FILE_SHARE_PATH directory and granting read/write permissions to all users..." + sudo mkdir -p "$FILE_SHARE_PATH" + sudo chown -R "$USER:$USER" "$FILE_SHARE_PATH" + chmod -Rv 775 "$FILE_SHARE_PATH" + fi +} + # Downloads, compiles, and installs Netatalk (AppleShare server) function installNetatalk() { NETATALK_VERSION="230301" @@ -729,19 +752,6 @@ function installNetatalk() { fi fi - if [ ! -d "$FILE_SHARE_PATH" ] && [ -d "$HOME/afpshare" ]; then - echo - echo "File server dir $HOME/afpshare detected. This script will rename it to $FILE_SHARE_PATH." - echo - echo "Do you want to proceed with the installation? [y/N]" - read -r REPLY - if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then - sudo mv "$HOME/afpshare" "$FILE_SHARE_PATH" || exit 1 - else - exit 0 - fi - fi - echo echo "Downloading tarball to $HOME..." cd $HOME || exit 1 @@ -874,26 +884,6 @@ function installSamba() { fi fi - if [ ! -d "$FILE_SHARE_PATH" ] && [ -d "$HOME/afpshare" ]; then - echo - echo "File server dir $HOME/afpshare detected. This script will rename it to $FILE_SHARE_PATH." - echo - echo "Do you want to proceed with the installation? [y/N]" - read -r REPLY - if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then - sudo mv "$HOME/afpshare" "$FILE_SHARE_PATH" || exit 1 - else - exit 0 - fi - elif [ -d "$FILE_SHARE_PATH" ]; then - echo "Found a $FILE_SHARE_PATH directory; will use it for file sharing." - else - echo "Creating the $FILE_SHARE_PATH directory and granting read/write permissions to all users..." - sudo mkdir -p "$FILE_SHARE_PATH" - sudo chown -R "$USER:$USER" "$FILE_SHARE_PATH" - chmod -Rv 775 "$FILE_SHARE_PATH" - fi - echo "" echo "Installing dependencies..." sudo apt-get update || true @@ -1279,6 +1269,7 @@ function runChoice() { ;; 7) echo "Installing AppleShare File Server" + createFileSharingDir installNetatalk echo "Installing AppleShare File Server - Complete!" ;; @@ -1290,6 +1281,7 @@ function runChoice() { echo "WARNING: The FTP server may transfer unencrypted data over the network." echo "Proceed with this installation only if you are on a private, secure network." sudoCheck + createFileSharingDir installFtp echo "Installing FTP File Server - Complete!" ;; @@ -1301,6 +1293,7 @@ function runChoice() { echo " - Create a directory in the current user's home directory where shared files will be stored" echo " - Create a Samba user for the current user" sudoCheck + createFileSharingDir installSamba echo "Installing SMB File Server - Complete!" ;; diff --git a/python/web/src/templates/index.html b/python/web/src/templates/index.html index b9d74cc4..f6364c92 100644 --- a/python/web/src/templates/index.html +++ b/python/web/src/templates/index.html @@ -383,8 +383,11 @@ @@ -399,6 +402,7 @@ {% endfor %} + {% if file_server_dir_exists %} + {% endif %} diff --git a/python/web/src/templates/upload.html b/python/web/src/templates/upload.html index 67d6b57f..c572469e 100644 --- a/python/web/src/templates/upload.html +++ b/python/web/src/templates/upload.html @@ -6,7 +6,9 @@
  • {{ _("The largest file size accepted in this form is %(max_file_size)s MiB. Use other file transfer means for larger files.", max_file_size=max_file_size) }}
  • {{ _("You have to manually clean up partially uploaded files, as a result of cancelling the upload or closing this page.") }}
  • {{ _("Disk Images") }} = {{ env["image_dir"] }}
  • + {% if file_server_dir_exists %}
  • {{ _("Shared Files") }} = {{ FILE_SERVER_DIR }}
  • + {% endif %}
  • {{ _("PiSCSI Config") }} = {{ CFG_DIR }}
  • @@ -20,6 +22,7 @@ {% endfor %} + {% if file_server_dir_exists %} + {% endif %} diff --git a/python/web/src/web.py b/python/web/src/web.py index 40d9b032..5ac8ea22 100644 --- a/python/web/src/web.py +++ b/python/web/src/web.py @@ -24,7 +24,6 @@ from flask import ( send_from_directory, make_response, session, - abort, jsonify, ) @@ -44,6 +43,7 @@ from return_code_mapper import ReturnCodeMapper from socket_cmds_flask import SocketCmdsFlask from web_utils import ( + working_dirs_exist, sort_and_format_devices, get_valid_scsi_ids, map_device_types_and_names, @@ -209,16 +209,9 @@ def index(): """ Sets up data structures for and renders the index page """ - if not piscsi_cmd.is_token_auth()["status"] and not APP.config["PISCSI_TOKEN"]: - abort( - 403, - _( - "PiSCSI is password protected. " - "Start the Web Interface with the --password parameter." - ), - ) - server_info = piscsi_cmd.get_server_info() + working_dirs_exist((server_info["image_dir"], CFG_DIR)) + devices = piscsi_cmd.list_devices() device_types = map_device_types_and_names(piscsi_cmd.get_device_types()["device_types"]) image_files = file_cmd.list_images() @@ -277,6 +270,7 @@ def index(): drive_properties=format_drive_properties(APP.config["PISCSI_DRIVE_PROPERTIES"]), images_subdirs=file_cmd.list_subdirs(server_info["image_dir"]), shared_subdirs=file_cmd.list_subdirs(FILE_SERVER_DIR), + file_server_dir_exists=Path(FILE_SERVER_DIR).exists(), RESERVATIONS=RESERVATIONS, CFG_DIR=CFG_DIR, FILE_SERVER_DIR=FILE_SERVER_DIR, @@ -302,6 +296,8 @@ def drive_list(): """ Sets up the data structures and kicks off the rendering of the drive list page """ + server_info = piscsi_cmd.get_server_info() + working_dirs_exist((server_info["image_dir"], CFG_DIR)) return response( template="drives.html", @@ -317,12 +313,14 @@ def upload_page(): Sets up the data structures and kicks off the rendering of the file uploading page """ server_info = piscsi_cmd.get_server_info() + working_dirs_exist((server_info["image_dir"], CFG_DIR)) return response( template="upload.html", page_title=_("PiSCSI File Upload"), images_subdirs=file_cmd.list_subdirs(server_info["image_dir"]), shared_subdirs=file_cmd.list_subdirs(FILE_SERVER_DIR), + file_server_dir_exists=Path(FILE_SERVER_DIR).exists(), max_file_size=int(int(MAX_FILE_SIZE) / 1024 / 1024), CFG_DIR=CFG_DIR, FILE_SERVER_DIR=FILE_SERVER_DIR, @@ -516,6 +514,7 @@ def show_diskinfo(): if not safe_path["status"]: return response(error=True, message=safe_path["msg"]) server_info = piscsi_cmd.get_server_info() + working_dirs_exist((server_info["image_dir"], CFG_DIR)) returncode, diskinfo = sys_cmd.get_diskinfo(Path(server_info["image_dir"]) / file_name) if returncode == 0: return response( @@ -1474,6 +1473,12 @@ if __name__ == "__main__": file_cmd = FileCmds(sock_cmd=sock_cmd, piscsi=piscsi_cmd, token=APP.config["PISCSI_TOKEN"]) sys_cmd = SysCmds() + if not piscsi_cmd.is_token_auth()["status"] and not APP.config["PISCSI_TOKEN"]: + raise Exception( + "PiSCSI is password protected. " + "Start the Web Interface with the --password parameter." + ) + if Path(f"{CFG_DIR}/{DEFAULT_CONFIG}").is_file(): file_cmd.read_config(DEFAULT_CONFIG) if Path(f"{DRIVE_PROPERTIES_FILE}").is_file(): diff --git a/python/web/src/web_utils.py b/python/web/src/web_utils.py index 3cf71b3e..d085510f 100644 --- a/python/web/src/web_utils.py +++ b/python/web/src/web_utils.py @@ -9,13 +9,26 @@ from pathlib import Path from ua_parser import user_agent_parser from re import findall -from flask import request, make_response +from flask import request, make_response, abort from flask_babel import _ from werkzeug.utils import secure_filename from piscsi.sys_cmds import SysCmds +def working_dirs_exist(working_dirs): + """ + Method for validating that working dirs exist. + Takes (tuple) of (str) working_dirs with paths to required dirs. + """ + for dir_path in working_dirs: + if not Path(dir_path).exists(): + abort( + 503, + _(f"Please create directory: {dir_path}"), + ) + + def get_valid_scsi_ids(devices, reserved_ids): """ Takes a list of (dict)s devices, and list of (int)s reserved_ids.