feature(Trsh Collection):
Added Trash Collection Util Cleaned up API Views
This commit is contained in:
parent
31257a51e4
commit
04cc4fd4a5
|
@ -17,10 +17,11 @@ REDIS_URL=redis://localhost:6379/0
|
||||||
# SQLITE_URL=sqlite:///my-local-sqlite.db
|
# 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_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
|
||||||
CACHE=TRUE
|
CACHE=TRUE
|
||||||
REDIS_URL=redis://127.0.0.1:6379/1
|
|
||||||
|
|
||||||
REDIS_HOST=192.168.56.102
|
REDIS_HOST=192.168.56.102
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
|
REDIS_DB=0
|
||||||
|
TRASH_TIMEOUT=30 # in seconds
|
||||||
|
|
||||||
AWS_ACCESS_KEY_ID='AKIA5VQLLGQ24RLS3KWK'
|
AWS_ACCESS_KEY_ID='AKIA5VQLLGQ24RLS3KWK'
|
||||||
AWS_SECRET_ACCESS_KEY='vxDQL+rt0D2cSJ1pgT1xDCFQyiVz14SmMeIPQi/f'
|
AWS_SECRET_ACCESS_KEY='vxDQL+rt0D2cSJ1pgT1xDCFQyiVz14SmMeIPQi/f'
|
|
@ -2,7 +2,7 @@ annotated-types==0.5.0
|
||||||
argon2-cffi==23.1.0
|
argon2-cffi==23.1.0
|
||||||
argon2-cffi-bindings==21.2.0
|
argon2-cffi-bindings==21.2.0
|
||||||
asgiref==3.7.2
|
asgiref==3.7.2
|
||||||
boto3==1.28.68
|
boto==2.49.0
|
||||||
certifi==2023.7.22
|
certifi==2023.7.22
|
||||||
cffi==1.16.0
|
cffi==1.16.0
|
||||||
charset-normalizer==3.2.0
|
charset-normalizer==3.2.0
|
||||||
|
@ -16,7 +16,6 @@ dr-scaffold==2.1.2
|
||||||
drf-nested-routers==0.93.4
|
drf-nested-routers==0.93.4
|
||||||
drf-yasg==1.21.7
|
drf-yasg==1.21.7
|
||||||
grpcio==1.59.0
|
grpcio==1.59.0
|
||||||
grpcio-tools==1.59.0
|
|
||||||
idna==3.4
|
idna==3.4
|
||||||
inflect==7.0.0
|
inflect==7.0.0
|
||||||
inflection==0.5.1
|
inflection==0.5.1
|
||||||
|
|
|
@ -38,6 +38,7 @@ ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=['*'])
|
||||||
|
|
||||||
REDIS_HOST = env.str('REDIS_HOST', default='localhost')
|
REDIS_HOST = env.str('REDIS_HOST', default='localhost')
|
||||||
REDIS_PORT = env.str('REDIS_PORT', default='6379')
|
REDIS_PORT = env.str('REDIS_PORT', default='6379')
|
||||||
|
REDIS_DB = env.str('REDIS_DB', default='0')
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
|
@ -150,7 +151,7 @@ if env.bool('CACHE', default=False):
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
"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}",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,10 @@ Including another URLconf
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
from safeshare_app.utils.TrashCollector import TrashCollector
|
||||||
|
|
||||||
|
trash_collector = TrashCollector()
|
||||||
|
trash_collector.start()
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from rest_framework import routers
|
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 = [
|
urlpatterns = [
|
||||||
path('files/', manage_items, name="manage_items"),
|
path('files/', ManageItemsView.as_view(), name="manage_items"),
|
||||||
path('files/<str:key>/', manage_item, name="manage_item"),
|
path('files/<str:key>/', ManageItemView.as_view(), name="manage_item"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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))
|
|
@ -3,18 +3,17 @@ import uuid
|
||||||
import os
|
import os
|
||||||
import hashlib
|
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
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from rest_framework.views import APIView
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
class ManageItemsView(APIView):
|
||||||
def manage_items(request):
|
def post(self, request):
|
||||||
if request.method == 'POST':
|
|
||||||
# Define a timeout value (in seconds)
|
# Define a timeout value (in seconds)
|
||||||
timeout = 5
|
timeout = 5
|
||||||
|
|
||||||
|
@ -56,11 +55,15 @@ def manage_items(request):
|
||||||
|
|
||||||
# Get the hash signature
|
# Get the hash signature
|
||||||
hash_signature = hasher.hexdigest()
|
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
|
# Call RPC For virus scan
|
||||||
client = Client()
|
try:
|
||||||
result = client.CheckFile(hash_signature)
|
client = Client()
|
||||||
|
result = client.CheckFile(hash_signature)
|
||||||
|
except Exception as e:
|
||||||
|
result = False
|
||||||
|
|
||||||
# If infected, delete the file and return an error
|
# If infected, delete the file and return an error
|
||||||
if result:
|
if result:
|
||||||
|
@ -123,99 +126,35 @@ def manage_items(request):
|
||||||
timeout_timer.cancel()
|
timeout_timer.cancel()
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'PUT', 'DELETE'])
|
class ManageItemView(APIView):
|
||||||
def manage_item(request, *args, **kwargs):
|
def get(self, request, key):
|
||||||
if request.method == 'GET':
|
value = cache.get(key)
|
||||||
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)
|
|
||||||
|
|
||||||
elif request.method == 'DELETE':
|
if not value:
|
||||||
if 'key' in kwargs:
|
return Response({'msg': 'Not found'}, status=404)
|
||||||
value = cache.get(kwargs['key'])
|
|
||||||
if value:
|
|
||||||
if 'path' in value:
|
|
||||||
file_path = value['path']
|
|
||||||
|
|
||||||
# Check if the file exists
|
if 'path' not in value:
|
||||||
if os.path.exists(file_path):
|
return Response({'msg': 'File not found'}, status=404)
|
||||||
# Delete the file
|
|
||||||
os.remove(file_path)
|
|
||||||
|
|
||||||
# Delete the cache entry
|
file_path = value['path']
|
||||||
cache.delete(kwargs['key'])
|
|
||||||
|
|
||||||
response = {
|
if not os.path.exists(file_path):
|
||||||
'msg': f"{kwargs['key']} successfully deleted"
|
return Response({'msg': 'File not found'}, status=404)
|
||||||
}
|
|
||||||
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)
|
|
||||||
|
|
||||||
# elif request.method == 'PUT':
|
with open(file_path, 'rb') as f:
|
||||||
# if kwargs['key']:
|
response = HttpResponse(f.read(), content_type='application/octet-stream')
|
||||||
# request_data = json.loads(request.body)
|
response['Content-Disposition'] = f'attachment; filename="{quote(value["filename"])}"'
|
||||||
# new_value = request_data['value']
|
return response
|
||||||
# value = redis_instance.get(kwargs['key'])
|
|
||||||
# if value:
|
def delete(self, request, key):
|
||||||
# redis_instance.set(kwargs['key'], new_value)
|
value = cache.get(key)
|
||||||
# response = {
|
|
||||||
# 'key': kwargs['key'],
|
if not value:
|
||||||
# 'file': value,
|
return Response({'msg': 'Not found'}, status=404)
|
||||||
# 'msg': f"Successfully updated {kwargs['key']}"
|
|
||||||
# }
|
if 'path' in value and os.path.exists(value['path']):
|
||||||
# return Response(response, status=200)
|
os.remove(value['path'])
|
||||||
# else:
|
cache.delete(key)
|
||||||
# response = {
|
return Response({'msg': f"{key} successfully deleted"}, status=200)
|
||||||
# 'key': kwargs['key'],
|
|
||||||
# 'value': None,
|
return Response({'msg': 'File not found'}, status=404)
|
||||||
# '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()
|
|
||||||
|
|
Loading…
Reference in New Issue