From d29860397aba1f60f8d48ac270387bf202d32830 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Tue, 11 Apr 2023 00:45:51 +0300 Subject: [PATCH] updated file upload --- akarpov/templates/base.html | 2 +- akarpov/templates/files/list.html | 35 ++++++++++++------- .../0008_alter_user_left_file_upload.py | 23 ++++++++++++ akarpov/users/models.py | 4 +-- 4 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 akarpov/users/migrations/0008_alter_user_left_file_upload.py diff --git a/akarpov/templates/base.html b/akarpov/templates/base.html index 46c0703..0d505aa 100644 --- a/akarpov/templates/base.html +++ b/akarpov/templates/base.html @@ -97,7 +97,7 @@ -
+
{% if messages %} diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index 5de230b..b4493fb 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -45,8 +45,8 @@ -

Drag & Drop background image(s) inside dashed region
or

- +

Drag & Drop file(s)
or

+ @@ -118,11 +118,13 @@ var md5 = "", csrf = $("input[name='csrfmiddlewaretoken']")[0].value, form_data = [{"name": "csrfmiddlewaretoken", "value": csrf}]; + function calculate_md5(file, chunk_size) { var slice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice, chunks = Math.ceil(file.size / chunk_size), current_chunk = 0, spark = new SparkMD5.ArrayBuffer(); + function onload(e) { spark.append(e.target.result); // append chunk current_chunk++; @@ -132,6 +134,7 @@ md5 = spark.end(); } }; + function read_next_chunk() { var reader = new FileReader(); reader.onload = onload; @@ -141,21 +144,31 @@ }; read_next_chunk(); } - let ind = 0; - let files = [] + + let body = $("#body") + let cur = false $("#chunked_upload").fileupload({ url: "{% url 'files:api_chunked_upload' %}", dataType: "json", maxChunkSize: 1000000, // Chunks of 1000 kB formData: form_data, + prependFiles: true, + dropZone: body, + pasteZone: body, sequentialUploads: true, + autoUpload: false, add: function(e, data) { // Called before starting upload - files.push(data.files[0]) - calculate_md5(files[ind], 1000000); // Again, chunks of 1000 kB - data.submit(); - $("#progress").css("display", "flex"); + if (cur !== true){ + cur = true + console.log("add", data.files[0]) + form_data.splice(1); + calculate_md5(data.files[0], 1000000); + data.submit(); + $("#progress").css("display", "flex"); + } }, chunkdone: function (e, data) { // Called after uploading each chunk + console.log("chunkdone", data.files[0]) if (form_data.length < 2) { form_data.push( {"name": "upload_id", "value": data.result.upload_id} @@ -167,6 +180,7 @@ sel.text(progress + "%"); }, done: function (e, data) { // Called when the file has completely uploaded + console.log("done", data.files[0]) $.ajax({ type: "POST", url: "{% url 'files:api_chunked_upload_complete' %}", @@ -188,11 +202,8 @@ $("#progress-message").empty() } }); - ind ++; - if (ind !== files.length){ - calculate_md5(files[ind], 1000000); + cur = false } - }, }) {% endblock %} diff --git a/akarpov/users/migrations/0008_alter_user_left_file_upload.py b/akarpov/users/migrations/0008_alter_user_left_file_upload.py new file mode 100644 index 0000000..c9c64a1 --- /dev/null +++ b/akarpov/users/migrations/0008_alter_user_left_file_upload.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2 on 2023-04-10 16:50 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0007_user_left_file_upload"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="left_file_upload", + field=models.BigIntegerField( + default=0, + validators=[django.core.validators.MinValueValidator(0)], + verbose_name="Left file upload(in bites)", + ), + ), + ] diff --git a/akarpov/users/models.py b/akarpov/users/models.py index f9783ba..035340e 100644 --- a/akarpov/users/models.py +++ b/akarpov/users/models.py @@ -1,6 +1,6 @@ from django.contrib.auth.models import AbstractUser from django.core.validators import MinValueValidator -from django.db.models import CharField, IntegerField, TextField +from django.db.models import BigIntegerField, CharField, TextField from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -22,7 +22,7 @@ class User(AbstractUser, BaseImageModel, ShortLink): last_name = None # type: ignore # files - left_file_upload = IntegerField( + left_file_upload = BigIntegerField( "Left file upload(in bites)", default=0, validators=[MinValueValidator(0)] )