Add error if uploaded file isn't zip archive
This commit is contained in:
parent
ef07edb1af
commit
c259c5722e
@ -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">×</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)
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user