Add error if uploaded file isn't zip archive

This commit is contained in:
HugoNeveux 2020-04-26 21:26:03 +02:00
parent ef07edb1af
commit c259c5722e
3 changed files with 15 additions and 3 deletions

View File

@ -8,6 +8,9 @@
{% block body %}
<main>
<div class="container pt-5">
<div class="alert alert-danger alert-dismissible fade show" id="errorZone" style="display:none;">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
</div>
<form class="dropzone" enctype="multipart/form-data" method="post" id="multiFileUpload">
<div class="fallback">
<input type="file" name="file" id="file" multiple />
@ -64,7 +67,8 @@ $('#multiFileUpload').dropzone({
});
this.on("error", function(file, message) {
console.log(message);
this.removeFile(file);
$('#errorZone').append(message.error);
$('#errorZone').show();
});
this.on('sending', function(file, xhr, formData) {
xhr.setRequestHeader("X-CSRFToken", csrftoken)

View File

@ -9,10 +9,12 @@ from .response import response_mimetype, JsonResponse
from .models import Module, Version, Dependency
from PDMI import settings
from packaging.specifiers import SpecifierSet
from django.http import HttpResponse
import os
import zipfile
import toml
import shutil
import magic
class SignUpView(BSModalCreateView):
@ -44,6 +46,11 @@ class UploadView(LoginRequiredMixin, FormView):
with open(zip_path, "wb+") as f: # Writing archive
for chunk in file.chunks():
f.write(chunk)
if magic.from_file(zip_path, mime=True) != 'application/zip':
print(magic.from_file(zip_path))
os.remove(zip_path)
return JsonResponse({'error': 'You must upload a zip archive.'}, status=500)
with zipfile.ZipFile(zip_path, 'r') as zip: # Unzip archive
zip.extractall(extract_path)
with open(os.path.join(extract_path, 'infos.toml'), 'r') as f:
@ -61,7 +68,7 @@ class UploadView(LoginRequiredMixin, FormView):
module = Module(name=module_info['name'].lower(),
desc=module_info['description'],
creator=request.user)
if Version.objects.filter(module=module, ver=module_info['version']).count() > 0:
if Version.objects.filter(module=module, ver=module_info['version']).exists():
# If version already exists, edit existing version
version = Version.objects.get(
module=module, ver=module_info['version'])
@ -77,7 +84,7 @@ class UploadView(LoginRequiredMixin, FormView):
version.save()
for dependency in module_info['dependencies']:
if not Dependency.objects.filter(version=version, dep_module=dependency,
dep_version=module_info['dependencies'][dependency]).count() > 0:
dep_version=module_info['dependencies'][dependency]).exists():
dep = Dependency(version=version, dep_module=dependency,
dep_version=module_info['dependencies'][dependency])
dep.save()

View File

@ -9,6 +9,7 @@ verify_ssl = true
django = "*"
django-widget-tweaks = "*"
django-bootstrap-modal-forms = "*"
python-magic = "*"
[requires]
python_version = "3.8"