Merge branch 'master' of https://github.com/Devoalda/Safe_Share
This commit is contained in:
commit
56b128d5d4
|
@ -3,15 +3,20 @@ import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
url = "http://127.0.0.1:8000/api/files/"
|
url = "http://127.0.0.1:8000/api/files/"
|
||||||
file = {'file': open('test.text', 'rb')}
|
|
||||||
|
# Send test.text and test2.text to server as a list
|
||||||
|
files = [('file', open('test.text', 'rb')), ('file', open('test2.text', 'rb'))]
|
||||||
data = {'ttl': 2}
|
data = {'ttl': 2}
|
||||||
|
|
||||||
r = requests.post(url, files=file, data=data)
|
r = requests.post(url, files=files, data=data)
|
||||||
|
|
||||||
# Print response message
|
# Print response message
|
||||||
print(json.dumps(r.json(), indent=4))
|
print(json.dumps(r.json(), indent=4))
|
||||||
|
|
||||||
key = r.json()['key']
|
for msg in r.json():
|
||||||
|
print(msg['key'])
|
||||||
|
print(msg['msg'])
|
||||||
|
key = msg['key']
|
||||||
|
|
||||||
# Wait for file to expire
|
# Wait for file to expire
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
|
|
@ -7,21 +7,15 @@ from rest_framework.decorators import api_view
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
|
||||||
# redis_instance = redis.StrictRedis(host=settings.REDIS_HOST,
|
|
||||||
# port=settings.REDIS_PORT, db=0)
|
|
||||||
|
|
||||||
|
@api_view(['POST'])
|
||||||
@api_view(['GET', 'POST'])
|
|
||||||
def manage_items(request, *args, **kwargs):
|
def manage_items(request, *args, **kwargs):
|
||||||
if request.method == 'GET':
|
|
||||||
# Not supposed to enumerate all items, so return 405
|
|
||||||
return Response({'msg': 'Method not allowed'}, status=405)
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
|
||||||
# Define a timeout value (in seconds)
|
# Define a timeout value (in seconds)
|
||||||
timeout = 5
|
timeout = 5
|
||||||
|
|
||||||
ttl = request.data['ttl']
|
# 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:
|
if not ttl:
|
||||||
return Response({'msg': 'TTL not provided'}, status=400)
|
return Response({'msg': 'TTL not provided'}, status=400)
|
||||||
|
@ -35,39 +29,39 @@ def manage_items(request, *args, **kwargs):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return Response({'msg': 'Invalid TTL format'}, status=400)
|
return Response({'msg': 'Invalid TTL format'}, status=400)
|
||||||
|
|
||||||
# Define a function to save the file in a thread
|
# Define a function to save a single file in a thread
|
||||||
def save_file_to_redis():
|
def save_file_to_redis(file):
|
||||||
key = uuid.uuid4().hex
|
key = uuid.uuid4().hex
|
||||||
|
|
||||||
file = request.FILES['file']
|
# Get the filename
|
||||||
if not file:
|
|
||||||
return Response({'msg': 'No file provided'}, status=400)
|
|
||||||
|
|
||||||
filename = file.name
|
filename = file.name
|
||||||
|
|
||||||
# Convert file to bytes
|
# Convert file to bytes
|
||||||
file = file.read()
|
file_content = file.read()
|
||||||
|
|
||||||
# Set with ttl if ttl is provided
|
# Set with the provided TTL
|
||||||
cache.set(key, file, timeout=ttl)
|
cache.set(key, file_content, timeout=ttl)
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
'key': key,
|
'key': key,
|
||||||
'msg': f"{key} successfully set to {filename}: {file}, with a ttl of {ttl} seconds"
|
'msg': f"{key} successfully set to {filename} with TTL {ttl} seconds"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Store the response in a shared variable
|
# Append the response to the shared responses list
|
||||||
nonlocal saved_response
|
responses.append(response)
|
||||||
saved_response = response
|
|
||||||
|
|
||||||
# Create a shared variable to store the response
|
# Create a list to store the responses for each file
|
||||||
saved_response = None
|
responses = []
|
||||||
|
|
||||||
# Create a new thread for the file-saving process
|
# Create a thread for each file
|
||||||
file_saving_thread = threading.Thread(target=save_file_to_redis)
|
file_threads = []
|
||||||
|
for file in files:
|
||||||
|
file_thread = threading.Thread(target=save_file_to_redis, args=(file,))
|
||||||
|
file_threads.append(file_thread)
|
||||||
|
|
||||||
# Start the file-saving thread
|
# Start all file-saving threads
|
||||||
file_saving_thread.start()
|
for file_thread in file_threads:
|
||||||
|
file_thread.start()
|
||||||
|
|
||||||
# Use a Timer to add a timeout
|
# Use a Timer to add a timeout
|
||||||
timeout_event = threading.Event()
|
timeout_event = threading.Event()
|
||||||
|
@ -77,19 +71,17 @@ def manage_items(request, *args, **kwargs):
|
||||||
# Start the timer
|
# Start the timer
|
||||||
timeout_timer.start()
|
timeout_timer.start()
|
||||||
|
|
||||||
# Wait for the file-saving thread to complete
|
# Wait for all file-saving threads to complete
|
||||||
file_saving_thread.join()
|
for file_thread in file_threads:
|
||||||
|
file_thread.join()
|
||||||
|
|
||||||
# Check if the thread completed without a timeout
|
# Check if the threads completed without a timeout
|
||||||
if not timeout_event.is_set():
|
if not timeout_event.is_set():
|
||||||
if saved_response:
|
return Response(responses, status=201)
|
||||||
return Response(saved_response, status=201)
|
|
||||||
else:
|
|
||||||
return Response({'msg': 'File saving failed'}, status=500)
|
|
||||||
else:
|
else:
|
||||||
return Response({'msg': 'File saving timed out'}, status=500)
|
return Response({'msg': 'File saving timed out'}, status=500)
|
||||||
finally:
|
finally:
|
||||||
# Always cancel the timer to prevent it from firing after the thread completes
|
# Always cancel the timer to prevent it from firing after the threads complete
|
||||||
timeout_timer.cancel()
|
timeout_timer.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue