This is broken right now. Trying to add unzip function

This commit is contained in:
Andrew 2021-08-19 16:50:18 -04:00
parent 9883d980f4
commit 90d59faf5b
5 changed files with 135 additions and 1 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -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')

View File

@ -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)

View File

@ -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":

View File

@ -81,6 +81,8 @@
<a onclick="renameItemE(event)" href="javascript:void(0)" id="renameItem" href="#">{{ translate('serverFiles', 'rename') }}</a>
<a onclick="deleteFileE(event)" href="javascript:void(0)" id="deleteFile" href="#">{{ translate('serverFiles', 'delete') }}</a>
<a onclick="deleteDirE(event)" href="javascript:void(0)" id="deleteDir" href="#">{{ translate('serverFiles', 'delete') }}</a>
<a onclick="uploadFilesE(event)" href="javascript:void(0)" id="upload" href="#">Upload Files</a>
<a onclick="unzipFilesE(event)" href="javascript:void(0)" id="unzip" href="#">Unzip</a>
</div>
</div>
@ -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 = '<form enctype="multipart/form-data" action="/ajax/upload_files?id=' + server_id +'&path='+ path +'"method="post">{% raw xsrf_form_html() %}<div class="form-group"><label for="files">Upload A File Here</label><input id="files" type="file" name="files" class="form-control-file" multiple></div><input id="unzip" type = checkbox value="1" checked> Would you like us Unzip directories? </input><br><br><input id="upload_file" type="submit"value="Upload File" class="btn btn-success hidden"/></form>';
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 += "<div class='col-xs-12 file-list'>"+this.files[i].name+"</div>";
}
$("#fileList").text(list);
}, false);
}
function getTreeView() {
$.ajax({
type: "GET",
@ -572,6 +607,7 @@
$('#createFile').toggle(isDir);
$('#createDir').toggle(isDir);
$('#deleteDir').toggle(isDir);
$('#upload').toggle(isDir);
var isFile = event.target.classList.contains('tree-file');
$('#deleteFile').toggle(isFile);
@ -583,6 +619,7 @@
$('#renameItem').hide();
$('#deleteDir').hide();
$('#deleteFile').hide();
$('#upload').show();
}
@ -636,6 +673,10 @@
});
})
}
function unzipFilesE(event) {
path = event.target.parentElement.getAttribute('data-path');
unZip(path)
}
function deleteFileE(event) {
path = event.target.parentElement.getAttribute('data-path');