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 %} {% block body %}
<main> <main>
<div class="container pt-5"> <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"> <form class="dropzone" enctype="multipart/form-data" method="post" id="multiFileUpload">
<div class="fallback"> <div class="fallback">
<input type="file" name="file" id="file" multiple /> <input type="file" name="file" id="file" multiple />
@ -64,7 +67,8 @@ $('#multiFileUpload').dropzone({
}); });
this.on("error", function(file, message) { this.on("error", function(file, message) {
console.log(message); console.log(message);
this.removeFile(file); $('#errorZone').append(message.error);
$('#errorZone').show();
}); });
this.on('sending', function(file, xhr, formData) { this.on('sending', function(file, xhr, formData) {
xhr.setRequestHeader("X-CSRFToken", csrftoken) xhr.setRequestHeader("X-CSRFToken", csrftoken)

View File

@ -9,10 +9,12 @@ from .response import response_mimetype, JsonResponse
from .models import Module, Version, Dependency from .models import Module, Version, Dependency
from PDMI import settings from PDMI import settings
from packaging.specifiers import SpecifierSet from packaging.specifiers import SpecifierSet
from django.http import HttpResponse
import os import os
import zipfile import zipfile
import toml import toml
import shutil import shutil
import magic
class SignUpView(BSModalCreateView): class SignUpView(BSModalCreateView):
@ -44,6 +46,11 @@ class UploadView(LoginRequiredMixin, FormView):
with open(zip_path, "wb+") as f: # Writing archive with open(zip_path, "wb+") as f: # Writing archive
for chunk in file.chunks(): for chunk in file.chunks():
f.write(chunk) 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 with zipfile.ZipFile(zip_path, 'r') as zip: # Unzip archive
zip.extractall(extract_path) zip.extractall(extract_path)
with open(os.path.join(extract_path, 'infos.toml'), 'r') as f: 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(), module = Module(name=module_info['name'].lower(),
desc=module_info['description'], desc=module_info['description'],
creator=request.user) 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 # If version already exists, edit existing version
version = Version.objects.get( version = Version.objects.get(
module=module, ver=module_info['version']) module=module, ver=module_info['version'])
@ -77,7 +84,7 @@ class UploadView(LoginRequiredMixin, FormView):
version.save() version.save()
for dependency in module_info['dependencies']: for dependency in module_info['dependencies']:
if not Dependency.objects.filter(version=version, dep_module=dependency, 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 = Dependency(version=version, dep_module=dependency,
dep_version=module_info['dependencies'][dependency]) dep_version=module_info['dependencies'][dependency])
dep.save() dep.save()

View File

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