Fix finalize_upload to always return JSON responses

- Improved FileLike class with proper file handling and close() method
- Added better error handling and validation for JSON input
- Added try/finally block to ensure file handles are closed
- Added detailed error logging with traceback for debugging
- Ensures server always returns JSON instead of HTML error pages
parent f1ea5952
...@@ -950,6 +950,9 @@ def finalize_upload(): ...@@ -950,6 +950,9 @@ def finalize_upload():
"""Finalize chunked upload""" """Finalize chunked upload"""
try: try:
data = request.get_json() data = request.get_json()
if not data:
return jsonify({'success': False, 'error': 'Invalid JSON data'}), 400
upload_id = data.get('uploadId') upload_id = data.get('uploadId')
file_name = data.get('fileName') file_name = data.get('fileName')
match_id = data.get('matchId') match_id = data.get('matchId')
...@@ -990,43 +993,46 @@ def finalize_upload(): ...@@ -990,43 +993,46 @@ def finalize_upload():
self.filename = filename self.filename = filename
self.name = filename self.name = filename
self.content_type = 'application/zip' # Default for ZIP files self.content_type = 'application/zip' # Default for ZIP files
self._file = None self._closed = False
self._pos = 0
def __enter__(self):
self._file = open(self.path, 'rb')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self._file:
self._file.close()
def read(self, size=-1): def read(self, size=-1):
if self._file is None: if self._closed:
raise ValueError("I/O operation on closed file")
if not hasattr(self, '_file'):
self._file = open(self.path, 'rb') self._file = open(self.path, 'rb')
if size == -1: if size == -1:
data = self._file.read() data = self._file.read()
else: else:
data = self._file.read(size) data = self._file.read(size)
self._pos += len(data)
return data return data
def seek(self, pos, whence=0): def seek(self, pos, whence=0):
if self._file is None: if self._closed:
raise ValueError("I/O operation on closed file")
if not hasattr(self, '_file'):
self._file = open(self.path, 'rb') self._file = open(self.path, 'rb')
self._file.seek(pos, whence) self._file.seek(pos, whence)
self._pos = self._file.tell()
def tell(self): def tell(self):
if self._file is None: if self._closed:
raise ValueError("I/O operation on closed file")
if not hasattr(self, '_file'):
self._file = open(self.path, 'rb') self._file = open(self.path, 'rb')
return self._file.tell() return self._file.tell()
def close(self):
if hasattr(self, '_file') and self._file:
self._file.close()
self._closed = True
def save(self, dst): def save(self, dst):
if self._closed:
raise ValueError("I/O operation on closed file")
shutil.move(self.path, dst) shutil.move(self.path, dst)
mock_file = FileLike(final_path, file_name) mock_file = FileLike(final_path, file_name)
try:
if match_id: if match_id:
# ZIP upload for match # ZIP upload for match
from app.models import Match from app.models import Match
...@@ -1099,6 +1105,12 @@ def finalize_upload(): ...@@ -1099,6 +1105,12 @@ def finalize_upload():
'redirect': url_for('main.matches') 'redirect': url_for('main.matches')
}), 200 }), 200
finally:
# Ensure file is closed
mock_file.close()
except Exception as e: except Exception as e:
logger.error(f"Finalize upload error: {str(e)}") logger.error(f"Finalize upload error: {str(e)}")
return jsonify({'success': False, 'error': str(e)}), 500 import traceback
\ No newline at end of file logger.error(f"Traceback: {traceback.format_exc()}")
return jsonify({'success': False, 'error': f'Upload finalization failed: {str(e)}'}), 500
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment