bug fix: some improvements in download file func

This commit is contained in:
usmannasir
2024-05-28 12:45:51 +04:00
parent 3977cd753d
commit 16b408ab40
2 changed files with 28 additions and 6 deletions

View File

@@ -751,7 +751,7 @@ class BackupManager:
return HttpResponse(final_json) return HttpResponse(final_json)
except BaseException as msg: except BaseException as msg:
final_dic = {'restoreStatus': 0, 'error_message': str(msg)} final_dic = {'restoreStatus': 0, 'error_message': str(msg), 'abort': 0, 'running': 'Running..', 'status': ''}
final_json = json.dumps(final_dic) final_json = json.dumps(final_dic)
return HttpResponse(final_json) return HttpResponse(final_json)

View File

@@ -6364,6 +6364,15 @@ class ApplicationInstaller(multi.Thread):
logging.statusWriter(self.tempStatusPath, str(msg)) logging.statusWriter(self.tempStatusPath, str(msg))
return 0, str(msg) return 0, str(msg)
def UpdateDownloadStatus(self, transferred, total):
percentage = (transferred / total) * 100
statusFile = open(self.tempStatusPath, 'w')
statusFile.writelines(f'{int(percentage)}% of file is downloaded from remote server..,50')
statusFile.close()
def StartOCRestore(self): def StartOCRestore(self):
try: try:
@@ -6400,12 +6409,13 @@ class ApplicationInstaller(multi.Thread):
ssh.connect(ip, username=ocb.sftpUser, pkey=key) ssh.connect(ip, username=ocb.sftpUser, pkey=key)
sftp = ssh.open_sftp() sftp = ssh.open_sftp()
sftp.get(f'cpbackups/{folder}/{backupfile}', f'/home/cyberpanel/{backupfile}') sftp.get(f'cpbackups/{folder}/{backupfile}', f'/home/cyberpanel/{backupfile}', callback=self.UpdateDownloadStatus)
if not os.path.exists('/home/backup'): if not os.path.exists('/home/backup'):
command = 'mkdir /home/backup' command = 'mkdir /home/backup'
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
command = f'mv /home/cyberpanel/{backupfile} /home/backup/{backupfile}' command = f'mv /home/cyberpanel/{backupfile} /home/backup/{backupfile}'
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
@@ -6413,6 +6423,12 @@ class ApplicationInstaller(multi.Thread):
wm = BackupManager() wm = BackupManager()
resp = wm.submitRestore({'backupFile': backupfile}, userID) resp = wm.submitRestore({'backupFile': backupfile}, userID)
statusFile = open(tempStatusPath, 'w')
statusFile.writelines("Download finished..,60")
statusFile.close()
time.sleep(6)
if json.loads(resp.content)['restoreStatus'] == 0: if json.loads(resp.content)['restoreStatus'] == 0:
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines(f"Failed to restore backup. Error {json.loads(resp.content)['error_message']}. [404]") statusFile.writelines(f"Failed to restore backup. Error {json.loads(resp.content)['error_message']}. [404]")
@@ -6423,13 +6439,21 @@ class ApplicationInstaller(multi.Thread):
return 0 return 0
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f'Name of of the backup file downloaded: {backupfile}')
while True: while True:
resp = wm.restoreStatus({'backupFile': backupfile}) resp = wm.restoreStatus({'backupFile': backupfile})
resp = json.loads(resp.content) resp = json.loads(resp.content)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(f'Responce from status function: {str(resp)}')
if resp['abort'] == 1 and resp['running'] == 'Completed': if resp['abort'] == 1 and resp['running'] == 'Completed':
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines("Successfully Installed. [200]") statusFile.writelines("Successfully Restored. [200]")
statusFile.close() statusFile.close()
command = f'rm -f /home/backup/{backupfile}' command = f'rm -f /home/backup/{backupfile}'
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
@@ -6437,7 +6461,7 @@ class ApplicationInstaller(multi.Thread):
elif resp['abort'] == 1 and resp['running'] == 'Error': elif resp['abort'] == 1 and resp['running'] == 'Error':
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines( statusFile.writelines(
f"Failed to restore backup. Error {resp['status']}. [404]") f"Failed to restore backup. Error {str(resp['status'])}. [404]")
statusFile.close() statusFile.close()
command = f'rm -f /home/backup/{backupfile}' command = f'rm -f /home/backup/{backupfile}'
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
@@ -6446,8 +6470,6 @@ class ApplicationInstaller(multi.Thread):
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines(f"{resp['status']},60") statusFile.writelines(f"{resp['status']},60")
statusFile.close() statusFile.close()
command = f'rm -f /home/backup/{backupfile}'
ProcessUtilities.executioner(command)
time.sleep(3) time.sleep(3)
except BaseException as msg: except BaseException as msg: