Add persistent file storage and improve error handling

Refactor object storage initialization to use a dedicated function and add error handling for uploads, including a fallback to local storage.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: cd9a7d26-a4e5-4215-975c-c59f4ed1f06d
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 127466ff-4d39-4e2e-bc28-80d552851c25
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/d0a1d46d-d203-4308-bc6a-312ac7c0243b/cd9a7d26-a4e5-4215-975c-c59f4ed1f06d/05bPjFc
This commit is contained in:
abhiramtx
2025-12-13 21:34:03 +00:00
parent 34a7a59899
commit 6a8eb826a6
3 changed files with 40 additions and 27 deletions

18
app.py
View File

@@ -17,11 +17,16 @@ ADMIN_PASSWORD = 'techturb123'
try:
storage_service = ObjectStorageService()
USING_OBJECT_STORAGE = True
print(f"Object Storage initialized successfully - using bucket: {storage_service.bucket_name}")
except ValueError as e:
print(f"Warning: Object Storage not configured - {e}")
print("Files will be saved locally (ephemeral storage)")
storage_service = None
USING_OBJECT_STORAGE = False
except Exception as e:
print(f"Error initializing Object Storage: {e}")
storage_service = None
USING_OBJECT_STORAGE = False
def get_db_connection():
conn = psycopg2.connect(os.environ['DATABASE_URL'])
@@ -36,10 +41,17 @@ def upload_file_to_storage(file, folder='uploads'):
return None
if USING_OBJECT_STORAGE and storage_service:
# Upload to persistent object storage
return storage_service.upload_file(file, folder)
try:
result = storage_service.upload_file(file, folder)
print(f"File uploaded to object storage: {result}")
return result
except Exception as e:
print(f"Error uploading to object storage: {e}")
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
return f'images/{filename}'
else:
# Fallback to local storage (ephemeral)
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)