fix(File save update):

Updated file saved to use uuid instead of original filename
Updated Download to use the original filename
This commit is contained in:
Devoalda 2023-11-02 10:16:06 +08:00
parent a5e409c2fd
commit 372825d662
1 changed files with 27 additions and 18 deletions

View File

@ -65,9 +65,9 @@ class ManageItemsView(APIView):
timeout_timer.cancel() timeout_timer.cancel()
def _save_file(self, file, ttl, responses): def _save_file(self, file, ttl, responses):
key = uuid.uuid4().hex generated_uuid = uuid.uuid4().hex # Generate a UUID
filename = file.name filename = file.name # Get the original filename
save_path = os.path.join(settings.MEDIA_ROOT, filename) save_path = os.path.join(settings.MEDIA_ROOT, generated_uuid) # Use the UUID as the new filename
hasher = hashlib.sha256() hasher = hashlib.sha256()
with open(save_path, 'wb') as destination: with open(save_path, 'wb') as destination:
@ -103,21 +103,22 @@ class ManageItemsView(APIView):
logger.warning(f'Error detecting MIME type: {str(e)}') logger.warning(f'Error detecting MIME type: {str(e)}')
mime_type = 'application/octet-stream' mime_type = 'application/octet-stream'
# Store the file path, filename, MIME type, and other information in the cache # Store the file path, generated UUID, original filename, MIME type, and other information in the cache
cache.set(key, { cache.set(generated_uuid, {
'filename': filename, 'filename': filename, # Original filename
'generated_uuid': generated_uuid, # Generated UUID
'path': save_path, 'path': save_path,
'mime_type': mime_type, # Store the MIME type 'mime_type': mime_type,
}, timeout=ttl) }, timeout=ttl)
response = { response = {
'key': key, 'key': generated_uuid, # Return the generated UUID
'filename': filename, 'filename': filename, # Return the original filename
'mime_type': mime_type, # Include the MIME type in the response 'mime_type': mime_type,
'msg': f"{key} successfully set to {filename} with TTL {ttl} seconds", 'msg': f"{generated_uuid} successfully set to {filename} with TTL {ttl} seconds",
} }
responses.append(response) responses.append(response)
logger.info(f'File {filename} successfully saved to cache with key {key} and TTL {ttl} seconds') logger.info(f'File {filename} successfully saved to cache with key {generated_uuid} and TTL {ttl} seconds')
class ManageItemView(APIView): class ManageItemView(APIView):
@ -147,7 +148,8 @@ class ManageItemView(APIView):
response = HttpResponse(file_data, content_type=mime_type) response = HttpResponse(file_data, content_type=mime_type)
# Set the Content-Disposition with the original filename # Set the Content-Disposition with the original filename
response['Content-Disposition'] = f'attachment; filename="{quote(os.path.basename(file_path))}"' original_filename = value.get('filename', 'file') # Get the original filename from the cache
response['Content-Disposition'] = f'attachment; filename="{quote(original_filename)}"'
logger.info(f'File {file_path} successfully retrieved from cache with key {key}') logger.info(f'File {file_path} successfully retrieved from cache with key {key}')
return response return response
@ -174,7 +176,13 @@ PRIVATE_IPS_PREFIX = ('10.', '172.', '192.')
def get_client_ip(request): def get_client_ip(request):
""" """
Get Client's IP Get the client's IP address from the request.
This function extracts the client's IP address from various request headers,
taking into account possible proxy servers.
:param request: The HTTP request object.
:return: The client's IP address.
""" """
remote_address = request.META.get('HTTP_X_FORWARDED_FOR') or request.META.get('REMOTE_ADDR') remote_address = request.META.get('HTTP_X_FORWARDED_FOR') or request.META.get('REMOTE_ADDR')
ip = remote_address ip = remote_address
@ -183,8 +191,9 @@ def get_client_ip(request):
if x_forwarded_for: if x_forwarded_for:
proxies = x_forwarded_for.split(',') proxies = x_forwarded_for.split(',')
while len(proxies) > 0 and proxies[0].startswith(PRIVATE_IPS_PREFIX): while proxies and proxies[0].startswith(PRIVATE_IPS_PREFIX):
proxies.pop(0) proxies.pop(0)
if len(proxies) > 0: if proxies:
ip = proxies[0] ip = proxies[0]
return ip return ip