Skip to content

Commit

Permalink
Compress backup files with gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurswag committed Sep 10, 2024
1 parent 6a63c9a commit 4ff293b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
26 changes: 20 additions & 6 deletions backend/serdes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from django.core.management.commands import dumpdata, loaddata
from django.http import HttpResponse
from rest_framework import status
from rest_framework.parsers import JSONParser
from rest_framework.parsers import FileUploadParser
from rest_framework.response import Response
from rest_framework.views import APIView
import gzip, io

from ciso_assistant.settings import VERSION
from serdes.serializers import LoadBackupSerializer
Expand All @@ -25,7 +26,8 @@ def get(self, request, *args, **kwargs):
f'attachment; filename="ciso-assistant-db-{timestamp}.json"'
)

response.write(f'[{{"meta": [{{"media_version": "{VERSION}"}}]}},\n')
buffer = io.StringIO()
buffer.write(f'[{{"meta": [{{"media_version": "{VERSION}"}}]}},\n')
# Here we dump th data to stdout
# NOTE: We will not be able to dump selected folders with this method.
management.call_command(
Expand All @@ -38,22 +40,34 @@ def get(self, request, *args, **kwargs):
"knox.authtoken",
],
indent=4,
stdout=response,
stdout=buffer,
natural_foreign=True,
)
response.write("]")
buffer.write("]")
buffer.seek(0)
buffer_data = gzip.compress(buffer.getvalue().encode())
response.write(buffer_data)
return response


class LoadBackupView(APIView):
parser_classes = (JSONParser,)
parser_classes = (FileUploadParser,)
serializer_class = LoadBackupSerializer

def post(self, request, *args, **kwargs):
if not request.user.has_backup_permission:
return Response(status=status.HTTP_403_FORBIDDEN)
if request.data:
sys.stdin = io.StringIO(json.dumps(request.data[1]))
backup_file = request.data["file"]
is_json = backup_file.name.split(".")[-1].lower() == "json"
data = backup_file.read()
decompressed_data = data if is_json else gzip.decompress(data)
# Performances could be improved (by avoiding the json.loads + json.dumps calls with a direct raw manipulation on the JSON body)
# But performances of the backup loading is not that much important.
decompressed_data = json.loads(decompressed_data)[1]
decompressed_data = json.dumps(decompressed_data)

sys.stdin = io.StringIO(decompressed_data)
request.session.flush()
management.call_command("flush", interactive=False)
# Here we load the data from stdin
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/routes/(app)/backup-restore/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export const actions: Actions = {
const endpoint = `${BASE_API_URL}/serdes/load-backup/`;
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Disposition': `attachment; filename="${file.name}"`,
'Content-Type': file.type
},
body: file
});
const data = await response.text();
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/(app)/backup-restore/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { page } from '$app/stores';
import * as m from '$paraglide/messages';
const authorizedExtensions = ['.json'];
const authorizedExtensions = ['.json', '.gzip'];
import { getModalStore, type ModalSettings } from '@skeletonlabs/skeleton';
const modalStore = getModalStore();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routes/(app)/backup-restore/dump-db/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export const GET: RequestHandler = async ({ fetch }) => {
error(400, 'Error fetching the dump file');
}

const fileName = `ciso-assistant-db-${new Date().toISOString()}.json`;
const fileName = `ciso-assistant-db-${new Date().toISOString()}.gzip`;

return new Response(await res.blob(), {
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/gzip',
'Content-Disposition': `attachment; filename="${fileName}"`
}
});
Expand Down

0 comments on commit 4ff293b

Please sign in to comment.