feature(File Virus Check):

Backend virus check update
This commit is contained in:
Devoalda 2023-10-26 16:28:10 +08:00
parent f4646cd658
commit eddb7a7bc9
2 changed files with 26 additions and 2 deletions

View File

@ -28,7 +28,10 @@ function ShareFile() {
// If data is an array, take the first item // If data is an array, take the first item
if (Array.isArray(data)) { if (Array.isArray(data)) {
const passcode = data[0].key; const passcode = data[0].key;
const baseUrl = 'http://localhost:8000/api/files/';
setPasscode(passcode); setPasscode(passcode);
setShareableLink(baseUrl + passcode);
// Copy the passcode to the clipboard // Copy the passcode to the clipboard
navigator.clipboard.writeText(passcode).then(() => { navigator.clipboard.writeText(passcode).then(() => {
// Show a notification // Show a notification
@ -40,8 +43,6 @@ function ShareFile() {
// Handle errors here // Handle errors here
console.error('File upload failed', error); console.error('File upload failed', error);
}); });
const baseUrl = 'http://localhost:3000/download/';
setShareableLink(baseUrl);
} }
}; };

View File

@ -1,7 +1,9 @@
import threading import threading
import uuid import uuid
import os import os
import hashlib
from safeshare.safeshare_vdb.client import Client
from django.core.cache import cache from django.core.cache import cache
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
@ -43,11 +45,32 @@ def manage_items(request):
# Define the path to save the file locally # Define the path to save the file locally
save_path = os.path.join(settings.MEDIA_ROOT, filename) save_path = os.path.join(settings.MEDIA_ROOT, filename)
# Hash the file
hasher = hashlib.sha256()
# Save the file locally # Save the file locally
with open(save_path, 'wb') as destination: with open(save_path, 'wb') as destination:
for chunk in file.chunks(): for chunk in file.chunks():
hasher.update(chunk)
destination.write(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 # Store the file path in the cache with the provided TTL
cache.set(key, cache.set(key,
{ {