diff --git a/backend/.env b/backend/.env index ddc953c..5c1abf4 100644 --- a/backend/.env +++ b/backend/.env @@ -1,2 +1,3 @@ SUPABASE_URL=https://tnpnlkosqqudoadfylss.supabase.co -SUPABASE_SERVICE_ROLE_KEY=sb_publishable_UqXeuY5gOjvGpoNO1ciZYw_g7nO2M1Q +SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InRucG5sa29zcXF1ZG9hZGZ5bHNzIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3MTAyNTQwOCwiZXhwIjoyMDg2NjAxNDA4fQ.v1xblErJBBv8p001vvTCUBfjP8ZqskfXNETB_yJOgvc +SUPABASE_BUCKET=archives diff --git a/backend/__pycache__/api_routes.cpython-311.pyc b/backend/__pycache__/api_routes.cpython-311.pyc index c6c5b4f..6251856 100644 Binary files a/backend/__pycache__/api_routes.cpython-311.pyc and b/backend/__pycache__/api_routes.cpython-311.pyc differ diff --git a/backend/__pycache__/db_queries.cpython-311.pyc b/backend/__pycache__/db_queries.cpython-311.pyc index 690ec4a..a64d1c4 100644 Binary files a/backend/__pycache__/db_queries.cpython-311.pyc and b/backend/__pycache__/db_queries.cpython-311.pyc differ diff --git a/backend/api_routes.py b/backend/api_routes.py index 5a75fd7..8cc83cb 100644 --- a/backend/api_routes.py +++ b/backend/api_routes.py @@ -34,6 +34,7 @@ from db_queries import ( list_user_history, search_rag_chunks, update_audio_post, + upload_storage_object, upsert_archive_metadata, upsert_archive_rights, ) @@ -49,6 +50,7 @@ ALLOWED_MEDIA_EXTENSIONS = {"mp4", "mov", "mkv", "webm", "m4a", "mp3", "wav", "o WHISPER_MODEL = os.getenv("WHISPER_MODEL", "base") WHISPER_DEVICE = os.getenv("WHISPER_DEVICE", "cpu") WHISPER_COMPUTE_TYPE = os.getenv("WHISPER_COMPUTE_TYPE", "int8") +ARCHIVE_BUCKET = os.getenv("SUPABASE_BUCKET", os.getenv("SUPABASE_ARCHIVE_BUCKET", "archives")) _whisper_model: WhisperModel | None = None @@ -207,6 +209,7 @@ def api_upload_post(): post_uuid = str(uuid.uuid4()) safe_name = secure_filename(media.filename) storage_prefix = f"archive/{user_id}/{post_uuid}" + storage_object_path = f"{user_id}/{post_uuid}/original/{safe_name}" saved_path = UPLOAD_DIR / f"{post_uuid}_{safe_name}" media.save(saved_path) @@ -226,12 +229,20 @@ def api_upload_post(): post_id = int(created_post["post_id"]) media_sha = _sha256(saved_path) + with saved_path.open("rb") as media_file: + upload_storage_object( + bucket=ARCHIVE_BUCKET, + object_path=storage_object_path, + content=media_file.read(), + content_type=media.mimetype or "application/octet-stream", + upsert=False, + ) add_archive_file( post_id, { "role": "original_audio", - "path": str(saved_path).replace("\\", "/"), + "path": f"{ARCHIVE_BUCKET}/{storage_object_path}", "content_type": media.mimetype, "size_bytes": saved_path.stat().st_size, "sha256": media_sha, @@ -300,7 +311,7 @@ def api_upload_post(): "post_id": post_id, "visibility": visibility, "status": "ready", - "audio_path": str(saved_path).replace("\\", "/"), + "audio_path": f"{ARCHIVE_BUCKET}/{storage_object_path}", "transcript_text": transcript_text, "prompt": prompt_text, "rag_chunk_count": len(rag_rows), diff --git a/backend/db_queries.py b/backend/db_queries.py index 204db1c..0aee46a 100644 --- a/backend/db_queries.py +++ b/backend/db_queries.py @@ -36,6 +36,26 @@ def _paginate(page: int, limit: int) -> tuple[int, int]: return start, end +def upload_storage_object( + bucket: str, + object_path: str, + content: bytes, + content_type: str = "application/octet-stream", + upsert: bool = False, +) -> Dict[str, Any]: + """ + Upload bytes to Supabase Storage and return upload response data. + """ + return ( + supabase.storage.from_(bucket) + .upload( + object_path, + content, + {"content-type": content_type, "upsert": str(upsert).lower()}, + ) + ) + + # ==================== Users ==================== def create_user(payload: Dict[str, Any]) -> Dict[str, Any]: diff --git a/backend/uploads/49fb853c-f424-440a-9414-935862655c7b_data.m4a b/backend/uploads/49fb853c-f424-440a-9414-935862655c7b_data.m4a new file mode 100644 index 0000000..38e5740 Binary files /dev/null and b/backend/uploads/49fb853c-f424-440a-9414-935862655c7b_data.m4a differ diff --git a/backend/uploads/502f38f7-18bb-4b43-b09b-d32a0cd4baeb_data.m4a b/backend/uploads/502f38f7-18bb-4b43-b09b-d32a0cd4baeb_data.m4a new file mode 100644 index 0000000..38e5740 Binary files /dev/null and b/backend/uploads/502f38f7-18bb-4b43-b09b-d32a0cd4baeb_data.m4a differ diff --git a/backend/uploads/61adb8cf-2b50-46d0-8cd8-8079b9f55b35_data.m4a b/backend/uploads/61adb8cf-2b50-46d0-8cd8-8079b9f55b35_data.m4a new file mode 100644 index 0000000..38e5740 Binary files /dev/null and b/backend/uploads/61adb8cf-2b50-46d0-8cd8-8079b9f55b35_data.m4a differ