From eddb7a7bc908f5f93267c5b1f717b8fa12a76892 Mon Sep 17 00:00:00 2001 From: Devoalda Date: Thu, 26 Oct 2023 16:28:10 +0800 Subject: [PATCH] feature(File Virus Check): Backend virus check update --- .../safeshare-frontend/src/pages/shareFile.js | 5 ++-- safeshare/safeshare_app/views/file.py | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/safeshare/safeshare-frontend/src/pages/shareFile.js b/safeshare/safeshare-frontend/src/pages/shareFile.js index a18f5d1..2cc6118 100644 --- a/safeshare/safeshare-frontend/src/pages/shareFile.js +++ b/safeshare/safeshare-frontend/src/pages/shareFile.js @@ -28,7 +28,10 @@ function ShareFile() { // If data is an array, take the first item if (Array.isArray(data)) { const passcode = data[0].key; + const baseUrl = 'http://localhost:8000/api/files/'; + setPasscode(passcode); + setShareableLink(baseUrl + passcode); // Copy the passcode to the clipboard navigator.clipboard.writeText(passcode).then(() => { // Show a notification @@ -40,8 +43,6 @@ function ShareFile() { // Handle errors here console.error('File upload failed', error); }); - const baseUrl = 'http://localhost:3000/download/'; - setShareableLink(baseUrl); } }; diff --git a/safeshare/safeshare_app/views/file.py b/safeshare/safeshare_app/views/file.py index 8530e11..0873cfa 100644 --- a/safeshare/safeshare_app/views/file.py +++ b/safeshare/safeshare_app/views/file.py @@ -1,7 +1,9 @@ import threading import uuid import os +import hashlib +from safeshare.safeshare_vdb.client import Client from django.core.cache import cache from rest_framework.decorators import api_view from rest_framework.response import Response @@ -43,11 +45,32 @@ def manage_items(request): # Define the path to save the file locally save_path = os.path.join(settings.MEDIA_ROOT, filename) + # Hash the file + hasher = hashlib.sha256() + # Save the file locally with open(save_path, 'wb') as destination: for chunk in file.chunks(): + hasher.update(chunk) destination.write(chunk) + # Get the hash signature + hash_signature = hasher.hexdigest() + print(f"Hash signature: {hash_signature}") + + # Call RPC For virus scan + client = Client() + result = client.CheckFile(hash_signature) + + # If infected, delete the file and return an error + if result: + response = { + 'msg': f"File {filename} is infected with a virus" + } + os.remove(save_path) + responses.append(response) + return Response(responses, status=400) + # Store the file path in the cache with the provided TTL cache.set(key, {