From a6faf8720d8c0324e3c78ac31eb6546e28ecf2fc Mon Sep 17 00:00:00 2001 From: Devoalda Date: Mon, 23 Oct 2023 15:44:50 +0800 Subject: [PATCH] feature(Frontend multiple file upload fix): Fixed multi file upload for frontend Allowed missing ttl in backend --- .../safeshare-frontend/package-lock.json | 29 +++++++++++++++++++ safeshare/safeshare-frontend/package.json | 1 + .../safeshare-frontend/src/pages/shareFile.js | 11 +++++-- safeshare/safeshare/settings.py | 1 + safeshare/safeshare_app/views/file.py | 10 +++---- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/safeshare/safeshare-frontend/package-lock.json b/safeshare/safeshare-frontend/package-lock.json index 842c2d1..9428f6e 100644 --- a/safeshare/safeshare-frontend/package-lock.json +++ b/safeshare/safeshare-frontend/package-lock.json @@ -11,6 +11,7 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "axios": "^1.5.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-drag-drop-files": "^2.3.10", @@ -5336,6 +5337,29 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.5.1.tgz", + "integrity": "sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/axobject-query": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.2.1.tgz", @@ -14508,6 +14532,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", diff --git a/safeshare/safeshare-frontend/package.json b/safeshare/safeshare-frontend/package.json index b4afa1b..dc708cf 100644 --- a/safeshare/safeshare-frontend/package.json +++ b/safeshare/safeshare-frontend/package.json @@ -6,6 +6,7 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "axios": "^1.5.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-drag-drop-files": "^2.3.10", diff --git a/safeshare/safeshare-frontend/src/pages/shareFile.js b/safeshare/safeshare-frontend/src/pages/shareFile.js index 168cca1..4706ba9 100644 --- a/safeshare/safeshare-frontend/src/pages/shareFile.js +++ b/safeshare/safeshare-frontend/src/pages/shareFile.js @@ -13,14 +13,19 @@ function ShareFile() { console.log(file); if (file) { const formData = new FormData(); - formData.append('files', file); + formData.append('file', file); formData.append('ttl', "2"); // Send POST request to the backend API using Axios axios.post('http://127.0.0.1:8000/api/files/', formData) .then(response => { - // Handle a successful response from the server - setPasscode(response.data.passcode); + // Handle a successful response from the server, set passcode to "key" in the response body + const data = response.data; + + // If data is an array, take the first item + if (Array.isArray(data)) { + setPasscode(data[0].key); + } }) .catch(error => { // Handle errors here diff --git a/safeshare/safeshare/settings.py b/safeshare/safeshare/settings.py index 80cac0c..33a6fb9 100644 --- a/safeshare/safeshare/settings.py +++ b/safeshare/safeshare/settings.py @@ -94,6 +94,7 @@ SIMPLE_JWT = { CORS_ALLOWED_ORIGINS = [ 'http://localhost:3000', ] + TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', diff --git a/safeshare/safeshare_app/views/file.py b/safeshare/safeshare_app/views/file.py index 3304bbd..b0bcad8 100644 --- a/safeshare/safeshare_app/views/file.py +++ b/safeshare/safeshare_app/views/file.py @@ -1,11 +1,9 @@ -import uuid import threading +import uuid -import redis -from django.conf import settings +from django.core.cache import cache from rest_framework.decorators import api_view from rest_framework.response import Response -from django.core.cache import cache @api_view(['POST']) @@ -16,9 +14,9 @@ def manage_items(request, *args, **kwargs): # Get the list of files and the TTL value from the request data files = request.FILES.getlist('file') ttl = request.data.get('ttl') - if not ttl: - return Response({'msg': 'TTL not provided'}, status=400) + # Set ttl to 3 days + ttl = 259200 # 3 * 24 * 60 * 60 try: # Convert the TTL to an integer