Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backup feature views.py #736

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion web/projects/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from projects.models import Project
from django.core.files.base import ContentFile
from django.shortcuts import redirect
Expand All @@ -11,6 +12,7 @@
import shutil, os, time, requests, favicon
from pathlib import Path
from subprocess import Popen
import zipfile

# Main Projects Page
@login_required(login_url='/login/')
Expand Down Expand Up @@ -67,7 +69,7 @@ def index(request):
print("final_date: "+str(final_date))

pjtfor = Project.objects.filter(domain=sgdomain)
print("pjt: "+str(pjtfor))
# print("pjt: "+str(pjtfor))


# Save Domain
Expand Down Expand Up @@ -194,6 +196,42 @@ def delete_project(request, id):

return redirect('projects:index')

@login_required(login_url='/login/')
def DownloadBackup(requests, id):

project = Project.objects.get(id=id)
if project.status == "FINISHED":
command = str(project.command).split("'")
del command[0::2]

tempFolder = "/tmp"
folderPath = command[-1].rsplit("/",1)[0]
folderName = command[-1].rsplit("/",1)[1]

if "/" in folderName:
tmp = folderName.rsplit("/", 1)

folderPath = tmp[0]
folderName = tmp[1]

if os.path.exists(tempFolder+"/Backup-"+folderName+".zip"):
os.remove(tempFolder+"/Backup-"+folderName+".zip")

os.chdir(folderPath)
with zipfile.ZipFile(tempFolder+"/Backup-"+folderName+".zip", "w") as zf:
for item in Path(folderName).rglob("*"):
zf.write(item)
zf.close()

backupFileName = "Backup-"+folderName+".zip"

file = open(tempFolder+"/"+backupFileName, "rb")

response = HttpResponse(file, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename='+backupFileName
return response
else:
return HttpResponse('Scanning is not completed, please wait.')

# TODO: Cancel Scan Function
@login_required(login_url='/login/')
Expand Down