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
|
||||
|
||||
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}
|
||||
|
||||
r = requests.post(url, files=file, data=data)
|
||||
r = requests.post(url, files=files, data=data)
|
||||
|
||||
# Print response message
|
||||
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
|
||||
time.sleep(3)
|
||||
|
|
|
@ -7,21 +7,15 @@ from rest_framework.decorators import api_view
|
|||
from rest_framework.response import Response
|
||||
from django.core.cache import cache
|
||||
|
||||
# redis_instance = redis.StrictRedis(host=settings.REDIS_HOST,
|
||||
# port=settings.REDIS_PORT, db=0)
|
||||
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
@api_view(['POST'])
|
||||
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)
|
||||
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:
|
||||
return Response({'msg': 'TTL not provided'}, status=400)
|
||||
|
@ -35,39 +29,39 @@ def manage_items(request, *args, **kwargs):
|
|||
except ValueError:
|
||||
return Response({'msg': 'Invalid TTL format'}, status=400)
|
||||
|
||||
# Define a function to save the file in a thread
|
||||
def save_file_to_redis():
|
||||
# Define a function to save a single file in a thread
|
||||
def save_file_to_redis(file):
|
||||
key = uuid.uuid4().hex
|
||||
|
||||
file = request.FILES['file']
|
||||
if not file:
|
||||
return Response({'msg': 'No file provided'}, status=400)
|
||||
|
||||
# Get the filename
|
||||
filename = file.name
|
||||
|
||||
# Convert file to bytes
|
||||
file = file.read()
|
||||
file_content = file.read()
|
||||
|
||||
# Set with ttl if ttl is provided
|
||||
cache.set(key, file, timeout=ttl)
|
||||
# Set with the provided TTL
|
||||
cache.set(key, file_content, timeout=ttl)
|
||||
|
||||
response = {
|
||||
'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
|
||||
nonlocal saved_response
|
||||
saved_response = response
|
||||
# Append the response to the shared responses list
|
||||
responses.append(response)
|
||||
|
||||
# Create a shared variable to store the response
|
||||
saved_response = None
|
||||
# Create a list to store the responses for each file
|
||||
responses = []
|
||||
|
||||
# Create a new thread for the file-saving process
|
||||
file_saving_thread = threading.Thread(target=save_file_to_redis)
|
||||
# Create a thread for each file
|
||||
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
|
||||
file_saving_thread.start()
|
||||
# Start all file-saving threads
|
||||
for file_thread in file_threads:
|
||||
file_thread.start()
|
||||
|
||||
# Use a Timer to add a timeout
|
||||
timeout_event = threading.Event()
|
||||
|
@ -77,19 +71,17 @@ def manage_items(request, *args, **kwargs):
|
|||
# Start the timer
|
||||
timeout_timer.start()
|
||||
|
||||
# Wait for the file-saving thread to complete
|
||||
file_saving_thread.join()
|
||||
# Wait for all file-saving threads to complete
|
||||
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 saved_response:
|
||||
return Response(saved_response, status=201)
|
||||
else:
|
||||
return Response({'msg': 'File saving failed'}, status=500)
|
||||
return Response(responses, status=201)
|
||||
else:
|
||||
return Response({'msg': 'File saving timed out'}, status=500)
|
||||
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()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue