diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 120d3a6..ffa741d 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -10,7 +10,4 @@ export JNOTE_HOME="${JNOTE_HOME:-$DEFAULT_JNOTE_HOME}" export JNOTE_SNAP="${JNOTE_SNAP:-$DEFAULT_JNOTE_SNAP}" export JNOTE_INTSNAP="${JNOTE_INTSNAP:-$DEFAULT_JNOTE_INTSNAP}" -mkdir -p $JNOTE_HOME $JNOTE_SNAP $JNOTE_INTSNAP -chmod 777 $JNOTE_HOME $JNOTE_SNAP $JNOTE_INTSNAP - exec "$@" diff --git a/usr/share/jupyter-canvas-api/api-server.py b/usr/share/jupyter-canvas-api/api-server.py index 2da7455..6a9fca3 100644 --- a/usr/share/jupyter-canvas-api/api-server.py +++ b/usr/share/jupyter-canvas-api/api-server.py @@ -48,6 +48,7 @@ HOMEDIR = str(os.getenv('JNOTE_HOME', '/mnt/efs/stat-100a-home/')) # Home Directory Root SNAPSHOT_DIR = str(os.getenv('JNOTE_SNAP', '/mnt/efs/stat-100a-snap/')) # Instructor Snapshot Directory INTERMEDIARY_DIR = str(os.getenv('JNOTE_INTSNAP', '/mnt/efs/stat-100a-internal/')) # Intermediary Snapshot Directory +all_directories = [HOMEDIR, SNAPSHOT_DIR, INTERMEDIARY_DIR] COURSE_CODE = str(os.getenv('JNOTE_COURSE_CODE', 'STAT100a')) # The API Course Code UPLOAD_FOLDER = os.path.join('/tmp', 'uploads') # Temporary Upload Folder @@ -100,6 +101,28 @@ def slugify(value, allow_unicode=False): return re.sub(r'[-\s]+', '-', value).strip('-_') +def create_directories(directories): + """ + Decorators to create a directory if it doesn't exist. + + Parameters: + - directories (list): List of the directories to be created. + """ + def decorator_create_directories(func): + @wraps(func) + def wrapper_create_directories(*args, **kwargs): + for directory_path in directories: + if not os.path.exists(directory_path): + try: + os.makedirs(directory_path) + logger.info(f"Created directory '{directory_path}'.") + except OSError as e: + logger.error(f"Error creating directory '{directory_path}': {e}") + return func(*args, **kwargs) + return wrapper_create_directories + return decorator_create_directories + + # Default Flask HTTP 401 Error @app.errorhandler(401) def not_authorized(e): @@ -151,6 +174,7 @@ def decorated(*args, **kwargs): # @app.route('/get_snapshot_file_list', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def get_snapshot_file_list(): """ Get List of Snapshot Files for the Specified Student and Snapshot. """ @@ -224,6 +248,7 @@ def get_snapshot_file_list(): # @app.route('/get_snapshot_list', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def get_snapshot_list(): """ Get List of Snapshot Directories for the Specified Student. """ @@ -276,6 +301,7 @@ def get_snapshot_list(): # @app.route('/get_snapshot_file', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def get_snapshot_file(): """ Get the Specified File from Specified Student Snapshot. """ @@ -368,6 +394,7 @@ def get_snapshot_file(): # @app.route('/get_snapshot_zip', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def get_snapshot_zip(): """ Get Zip File of Specified Student Snapshot. """ @@ -473,6 +500,7 @@ def get_snapshot_zip(): # @app.route('/put_student_report', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def put_student_report(): """ Put Specified File into Specified Student Home Directory. """ @@ -555,6 +583,7 @@ def put_student_report(): # @app.route('/snapshot', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def snapshot(): """ Create a Snapshot of the Specified Student's Home Directory with the Specified Snapshot Name. """ @@ -655,6 +684,7 @@ def snapshot(): # @app.route('/snapshot_all', methods=['POST']) @requires_apikey +@create_directories(directories=all_directories) def snapshot_all(): """ Create a Snapshot of tll the Student's Home Directories with the Specified Snapshot Name. """