diff --git a/PDMI/store/templates/store/upload.html b/PDMI/store/templates/store/upload.html index 7ad61ba..41e4474 100644 --- a/PDMI/store/templates/store/upload.html +++ b/PDMI/store/templates/store/upload.html @@ -8,6 +8,9 @@ {% block body %}
+
@@ -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) diff --git a/PDMI/store/views.py b/PDMI/store/views.py index f6173e9..c6c9705 100644 --- a/PDMI/store/views.py +++ b/PDMI/store/views.py @@ -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() diff --git a/Pipfile b/Pipfile index fd3fc73..d4de018 100644 --- a/Pipfile +++ b/Pipfile @@ -9,6 +9,7 @@ verify_ssl = true django = "*" django-widget-tweaks = "*" django-bootstrap-modal-forms = "*" +python-magic = "*" [requires] python_version = "3.8"