fixing file counting issue

This commit is contained in:
2025-04-14 22:47:50 -04:00
parent 68ed19766d
commit f5e4016816

View File

@@ -86,8 +86,23 @@ def download_file(code):
error_message='This file has expired and is no longer available for download.' error_message='This file has expired and is no longer available for download.'
), 404 ), 404
# Check download limit # Check if file exists
if file_info['downloads'] >= file_info['max_downloads']: if not os.path.exists(file_path):
del metadata[code]
save_metadata(metadata)
return render_template('error.html',
error_title='File Not Found',
error_message='The requested file could not be found on the server.'
), 404
# Increment download counter before checking limit
file_info['downloads'] += 1
# Check if this download puts us over the limit
if file_info['downloads'] > file_info['max_downloads']:
# Clean up the file and metadata
if os.path.exists(file_path):
os.remove(file_path)
del metadata[code] del metadata[code]
save_metadata(metadata) save_metadata(metadata)
return render_template('error.html', return render_template('error.html',
@@ -95,13 +110,10 @@ def download_file(code):
error_message='This file has reached its maximum number of downloads and is no longer available.' error_message='This file has reached its maximum number of downloads and is no longer available.'
), 404 ), 404
# Serve the file # Save the updated download count
if os.path.exists(file_path):
file_info['downloads'] += 1
if file_info['downloads'] >= file_info['max_downloads']:
os.remove(file_path)
del metadata[code]
save_metadata(metadata) save_metadata(metadata)
# Serve the file
directory = os.path.dirname(file_path) directory = os.path.dirname(file_path)
filename = os.path.basename(file_path) filename = os.path.basename(file_path)
return send_from_directory( return send_from_directory(
@@ -110,13 +122,6 @@ def download_file(code):
as_attachment=True, as_attachment=True,
download_name=file_info['filename'] download_name=file_info['filename']
) )
else:
del metadata[code]
save_metadata(metadata)
return render_template('error.html',
error_title='File Not Found',
error_message='The requested file could not be found on the server.'
), 404
def cleanup_expired_files(): def cleanup_expired_files():
metadata = load_metadata() metadata = load_metadata()