diff --git a/safeshare/env b/safeshare/env index 4f14184..ebd7206 100644 --- a/safeshare/env +++ b/safeshare/env @@ -17,10 +17,11 @@ REDIS_URL=redis://localhost:6379/0 # SQLITE_URL=sqlite:///my-local-sqlite.db # CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213 CACHE=TRUE -REDIS_URL=redis://127.0.0.1:6379/1 REDIS_HOST=192.168.56.102 REDIS_PORT=6379 +REDIS_DB=0 +TRASH_TIMEOUT=30 # in seconds AWS_ACCESS_KEY_ID='AKIA5VQLLGQ24RLS3KWK' AWS_SECRET_ACCESS_KEY='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' \ No newline at end of file diff --git a/safeshare/requirements.txt b/safeshare/requirements.txt index 6f28c30..7e93011 100644 --- a/safeshare/requirements.txt +++ b/safeshare/requirements.txt @@ -2,7 +2,7 @@ annotated-types==0.5.0 argon2-cffi==23.1.0 argon2-cffi-bindings==21.2.0 asgiref==3.7.2 -boto3==1.28.68 +boto==2.49.0 certifi==2023.7.22 cffi==1.16.0 charset-normalizer==3.2.0 @@ -16,7 +16,6 @@ dr-scaffold==2.1.2 drf-nested-routers==0.93.4 drf-yasg==1.21.7 grpcio==1.59.0 -grpcio-tools==1.59.0 idna==3.4 inflect==7.0.0 inflection==0.5.1 diff --git a/safeshare/safeshare/settings.py b/safeshare/safeshare/settings.py index be54820..daef073 100644 --- a/safeshare/safeshare/settings.py +++ b/safeshare/safeshare/settings.py @@ -38,6 +38,7 @@ ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=['*']) REDIS_HOST = env.str('REDIS_HOST', default='localhost') REDIS_PORT = env.str('REDIS_PORT', default='6379') +REDIS_DB = env.str('REDIS_DB', default='0') # Application definition @@ -150,7 +151,7 @@ if env.bool('CACHE', default=False): CACHES = { "default": { "BACKEND": "django.core.cache.backends.redis.RedisCache", - "LOCATION": env.str('REDIS_URL', 'redis://localhost:6379/1'), + "LOCATION": f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}", } } diff --git a/safeshare/safeshare/urls.py b/safeshare/safeshare/urls.py index 199c753..d9720e5 100644 --- a/safeshare/safeshare/urls.py +++ b/safeshare/safeshare/urls.py @@ -16,6 +16,10 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include +from safeshare_app.utils.TrashCollector import TrashCollector + +trash_collector = TrashCollector() +trash_collector.start() urlpatterns = [ path('admin/', admin.site.urls), diff --git a/safeshare/safeshare_app/urls/__init__.py b/safeshare/safeshare_app/urls/__init__.py index 243ec81..16218f3 100644 --- a/safeshare/safeshare_app/urls/__init__.py +++ b/safeshare/safeshare_app/urls/__init__.py @@ -1,11 +1,9 @@ from django.urls import path, include from rest_framework import routers -from safeshare_app.views import manage_items, manage_item +from safeshare_app.views import ManageItemsView, ManageItemView -# router = routers.SimpleRouter() -# router.register(r'files', FileView, basename='file') urlpatterns = [ - path('files/', manage_items, name="manage_items"), - path('files//', manage_item, name="manage_item"), + path('files/', ManageItemsView.as_view(), name="manage_items"), + path('files//', ManageItemView.as_view(), name="manage_item"), ] diff --git a/safeshare/safeshare_app/utils/TrashCollector.py b/safeshare/safeshare_app/utils/TrashCollector.py new file mode 100644 index 0000000..28e4eff --- /dev/null +++ b/safeshare/safeshare_app/utils/TrashCollector.py @@ -0,0 +1,70 @@ +import threading +import os +import redis +import environ +from django.conf import settings + + +class TrashCollector: + def __init__(self): + self.stop_event = threading.Event() + self.thread = threading.Thread(target=self.run) + self.media_root = settings.MEDIA_ROOT + + environ.Env.read_env(os.path.join('safeshare', 'env')) + self.env = environ.Env() + # Connect to Redis + self.redis = redis.StrictRedis( + host=self.env.str('REDIS_HOST', default='localhost'), + port=self.env.str('REDIS_PORT', default=6379), + db=self.env.str('REDIS_DB', default=0), + ) + + def start(self): + self.thread.start() + + def stop(self): + self.stop_event.set() + self.thread.join() + + def run(self): + while not self.stop_event.is_set(): + # Get all keys from the Redis database + all_keys = self.redis.keys("*") + + # Search through redis keys for expired keys + # Delete the files of expired keys or keys that have been deleted (dont exist) + if not all_keys: + # Delete all files + for file in os.listdir(self.media_root): + file_path = os.path.join(self.media_root, file) + try: + if os.path.isfile(file_path): + print(f"Deleting file {file_path}") + os.unlink(file_path) + except Exception as e: + print(e) + + else: + # Delete files(value) of expired keys + for key in all_keys: + if not self.redis.exists(key): + file_path = os.path.join(self.media_root, key.decode("utf-8")) + try: + if os.path.isfile(file_path): + os.unlink(file_path) + except Exception as e: + print(e) + + else: + if self.redis.ttl(key) == -1: + file_path = os.path.join(self.media_root, key.decode("utf-8")) + try: + if os.path.isfile(file_path): + print(f"Deleting file {file_path}") + os.unlink(file_path) + except Exception as e: + print(e) + + # Sleep for a specific interval in seconds + self.stop_event.wait(timeout=self.env.int('TRASH_TIMEOUT', default=60)) diff --git a/safeshare/safeshare_app/views/file.py b/safeshare/safeshare_app/views/file.py index 0873cfa..21c2324 100644 --- a/safeshare/safeshare_app/views/file.py +++ b/safeshare/safeshare_app/views/file.py @@ -3,18 +3,17 @@ 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 from django.http import HttpResponse from django.conf import settings +from rest_framework.views import APIView from urllib.parse import quote -@api_view(['POST']) -def manage_items(request): - if request.method == 'POST': +class ManageItemsView(APIView): + def post(self, request): # Define a timeout value (in seconds) timeout = 5 @@ -56,11 +55,15 @@ def manage_items(request): # Get the hash signature hash_signature = hasher.hexdigest() - print(f"Hash signature: {hash_signature}") + # print(f"Hash signature: {hash_signature}") + # If RPC client import fails, skip virus scan # Call RPC For virus scan - client = Client() - result = client.CheckFile(hash_signature) + try: + client = Client() + result = client.CheckFile(hash_signature) + except Exception as e: + result = False # If infected, delete the file and return an error if result: @@ -123,99 +126,35 @@ def manage_items(request): timeout_timer.cancel() -@api_view(['GET', 'PUT', 'DELETE']) -def manage_item(request, *args, **kwargs): - if request.method == 'GET': - if 'key' in kwargs: - value = cache.get(kwargs['key']) - if value: - # Check if the 'path' key is in the stored value - if 'path' in value: - file_path = value['path'] - if os.path.exists(file_path): - with open(file_path, 'rb') as f: - response = HttpResponse(f.read(), content_type='application/octet-stream') - response['Content-Disposition'] = f'attachment; filename="{quote(value["filename"])}"' - return response - else: - response = { - 'msg': 'File not found' - } - return Response(response, status=404) - else: - response = { - 'msg': 'Not found' - } - return Response(response, status=404) +class ManageItemView(APIView): + def get(self, request, key): + value = cache.get(key) - elif request.method == 'DELETE': - if 'key' in kwargs: - value = cache.get(kwargs['key']) - if value: - if 'path' in value: - file_path = value['path'] + if not value: + return Response({'msg': 'Not found'}, status=404) - # Check if the file exists - if os.path.exists(file_path): - # Delete the file - os.remove(file_path) + if 'path' not in value: + return Response({'msg': 'File not found'}, status=404) - # Delete the cache entry - cache.delete(kwargs['key']) + file_path = value['path'] - response = { - 'msg': f"{kwargs['key']} successfully deleted" - } - return Response(response, status=200) - else: - response = { - 'msg': 'File not found' - } - return Response(response, status=404) - else: - response = { - 'key': kwargs['key'], - 'msg': 'Not found' - } - return Response(response, status=404) + if not os.path.exists(file_path): + return Response({'msg': 'File not found'}, status=404) -# elif request.method == 'PUT': -# if kwargs['key']: -# request_data = json.loads(request.body) -# new_value = request_data['value'] -# value = redis_instance.get(kwargs['key']) -# if value: -# redis_instance.set(kwargs['key'], new_value) -# response = { -# 'key': kwargs['key'], -# 'file': value, -# 'msg': f"Successfully updated {kwargs['key']}" -# } -# return Response(response, status=200) -# else: -# response = { -# 'key': kwargs['key'], -# 'value': None, -# 'msg': 'Not found' -# } -# return Response(response, status=404) -# class FileView(viewsets.ModelViewSet): -# queryset = File.objects.all() -# serializer_class = FileSerializer -# permission_classes = () -# -# def get_queryset(self): -# # Only allow GET with a key -# key = self.request.query_params.get('key', None) -# if key is not None: -# # Remove / from end of key -# if key[-1] == '/': -# key = key[:-1] -# -# print(key) -# data = self.queryset.filter(key=key) -# return data -# -# else: -# # Return nothing if no key is provided -# return File.objects.none() + with open(file_path, 'rb') as f: + response = HttpResponse(f.read(), content_type='application/octet-stream') + response['Content-Disposition'] = f'attachment; filename="{quote(value["filename"])}"' + return response + + def delete(self, request, key): + value = cache.get(key) + + if not value: + return Response({'msg': 'Not found'}, status=404) + + if 'path' in value and os.path.exists(value['path']): + os.remove(value['path']) + cache.delete(key) + return Response({'msg': f"{key} successfully deleted"}, status=200) + + return Response({'msg': 'File not found'}, status=404)