diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..a2f64789 Binary files /dev/null and b/.DS_Store differ diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 5469bd06..beab8043 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -2,6 +2,7 @@ import os import re import sys import json +import tempfile import time import uuid import string @@ -258,6 +259,40 @@ class Helpers: logger.critical("Unable to write to {} - Error: {}".format(path, e)) return False + def unzipFile(self, zip_path): + new_dir_list = zip_path.split('/') + new_dir = '' + for i in range(len(new_dir_list)-1): + if i == 0: + new_dir += new_dir_list[i] + else: + new_dir += '/'+new_dir_list[i] + + if helper.check_file_perms(zip_path): + helper.ensure_dir_exists(new_dir) + tempDir = tempfile.mkdtemp() + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(tempDir) + for item in os.listdir(tempDir): + print(item) + test = zip_ref.filelist[1].filename + print(test) + path_list = test.split('/') + root_path = path_list[0] + if len(path_list) > 1: + for i in range(len(path_list) - 2): + root_path = os.path.join(root_path, path_list[i + 1]) + + full_root_path = os.path.join(tempDir, root_path) + + for item in os.listdir(full_root_path): + try: + shutil.move(os.path.join(full_root_path, item), os.path.join(new_dir, item)) + except Exception as ex: + logger.error('ERROR IN ZIP IMPORT: {}'.format(ex)) + else: + return "false" + def ensure_logging_setup(self): log_file = os.path.join(os.path.curdir, 'logs', 'commander.log') session_log_file = os.path.join(os.path.curdir, 'logs', 'session.log') diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index e9a9f1c4..6baa8713 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -503,6 +503,7 @@ class Server: while db_helper.get_server_stats_by_id(self.server_id)['updating']: if downloaded and not self.is_backingup: + print("Backup Status: " + str(self.is_backingup)) logger.info("Executable updated successfully. Starting Server") db_helper.set_update(self.server_id, False) diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index c84983ad..d236186e 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -1,5 +1,8 @@ import json import logging +import tempfile +import zipfile + import tornado.web import tornado.escape import bleach @@ -183,10 +186,64 @@ class AjaxHandler(BaseHandler): logger.warning("Invalid path in create_dir ajax call ({})".format(dir_path)) console.warning("Invalid path in create_dir ajax call ({})".format(dir_path)) return - # Create the directory os.mkdir(dir_path) + elif page == "upload_files": + server_id = self.get_argument('id', None) + path = self.get_argument('path', None) + unzip = self.get_argument('unzip', None) + + if helper.in_path(db_helper.get_server_data_by_id(server_id)['path'], path): + try: + files = self.request.files['files'] + for file in files: + if file['filename'].split('.') is not None: + self._upload_file(file['body'], path, file['filename'], unzip) + else: + logger.error("Directory Detected. Skipping") + self.redirect("/panel/server_detail?id={}&subpage=files".format(server_id)) + except Exception as e: + print(e) + self.redirect("/panel/server_detail?id={}&subpage=files".format(server_id)) + else: + logger.error("Invalid directory requested. Canceling upload") + + elif page == 'unzip_file': + print("in unzip file") + path = self.get_argument('path', None) + helper.unzipFile(path) + + def _upload_file(self, file_data, file_path, file_name, unzip): + error = "" + + file_full_path = os.path.join(file_path, file_name) + if os.path.exists(file_full_path): + error = "A file with this name already exists." + + if not helper.check_writeable(file_path): + error = "Unwritable Path" + + if error != "": + logger.error("Unable to save uploaded file due to: {}".format(error)) + return False + + output_file = open(file_full_path, 'wb') + output_file.write(file_data) + logger.info('Saving File: {}'.format(file_full_path)) + uploading = True + while uploading: + try: + new_output = open(file_full_path, 'wb') + new_output.close() + uploading = False + except: + print("file is still uploading") + if unzip == "True": + helper.unzipFile(file_full_path) + print("DONE") + return True + @tornado.web.authenticated def delete(self, page): if page == "del_file": diff --git a/app/frontend/templates/panel/server_files.html b/app/frontend/templates/panel/server_files.html index fe8501f9..10f97718 100644 --- a/app/frontend/templates/panel/server_files.html +++ b/app/frontend/templates/panel/server_files.html @@ -81,6 +81,8 @@ {{ translate('serverFiles', 'rename') }} {{ translate('serverFiles', 'delete') }} {{ translate('serverFiles', 'delete') }} + Upload Files + Unzip @@ -519,6 +521,39 @@ }); } + function unZip(path, callback) { + var token = getCookie("_xsrf") + $.ajax({ + type: "POST", + headers: {'X-XSRFToken': token}, + url: '/ajax/unzip_file?id={{ data['server_stats']['server_id']['server_id'] }}', + data: { + path: path + }, + }); + } + + function uploadFilesE(e){ + path = event.target.parentElement.getAttribute('data-path'); + server_id = {{ data['server_stats']['server_id']['server_id'] }}; + var uploadHtml = '
'; + + bootbox.dialog({ + message: uploadHtml, + title: "Upload Files To "+path, + }); + + var fileList = document.getElementById("upload"); + fileList.addEventListener("change", function (e) { + var list = ""; + for (var i = 0; i < this.files.length; i++) { + list += "