Skip to content

Commit 01c2f4f

Browse files
Feat/#186 (#191)
* post * post * formatted * reviewed * get * completed api * completed api for couples table * done issue #149 * modified after review * get user by raspi_id * the main code couldn't be pushed at last commit * azure error * create and delete container on azure blob storage * lockファイル再作成 * modified after review * upload file * revision uploading file on azure blob storage * download file * modified after review * download file * マージ前の修正 * modified after review * make format * messages table * chatgpt thread * エラー解決 * chatgpt use created assistant * return voice file * change response name * これでマージできる --------- Co-authored-by: shun-harutaro <shun_harutaro@protonmail.com> Co-authored-by: NOZAKI Shuntaro <60352276+shun-harutaro@users.noreply.github.com>
1 parent 30788ee commit 01c2f4f

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

api/v2/azure/storage.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,19 @@ async def upload_blob_file(user_id: int, blob_service_client: BlobServiceClient,
4646
return {"id": user_id, "message": "メッセージをアップロードしました"}
4747

4848

49-
def download_blob_file(
50-
user_id: int, boddy_id: str, blob_service_client: BlobServiceClient
51-
):
49+
def is_downloaded_blob(boddy_id: str, blob_service_client: BlobServiceClient):
5250
container_name = "message"
5351
blob_client = blob_service_client.get_blob_client(
5452
container=container_name, blob=str(boddy_id)
5553
)
5654
download_file_path = os.path.join(DOWNLOAD_DIR, f"{boddy_id}.wav")
5755

5856
if not blob_client.exists():
59-
return {"id": user_id, "message": "相方のファイルは見つかりませんでした"}
57+
return False
6058

6159
with open(file=download_file_path, mode="wb") as download_file:
6260
download_data = blob_client.download_blob()
6361
download_file.write(download_data.readall())
6462

6563
blob_client.delete_blob()
66-
return {"id": user_id, "message": "ダウンロードが完了しました"}
64+
return True

api/v2/routers/raspi.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from v1.services.whisper import speech2text
1515
from v1.utils.logging import get_logger
1616
from v2.azure.storage import (
17-
download_blob_file,
1817
get_blob_storage_account,
18+
is_downloaded_blob,
1919
upload_blob_file,
2020
)
2121
from v2.services.gpt import generate_text
@@ -95,7 +95,7 @@ async def create_message(
9595
f.write(content)
9696

9797
with open(file_location, "rb") as data:
98-
response = await upload_blob_file(id, blob_service_client, db, data)
98+
response = await upload_blob_file(id, blob_service_client, data)
9999
os.remove(file_location)
100100
return response
101101

@@ -113,14 +113,22 @@ async def negotiate(id: int):
113113
"/{id}",
114114
tags=["raspi"],
115115
summary="同coupleのメッセージ取得",
116-
response_model=raspi_schema.RaspiMessageResponse,
116+
# response_model=Union[FileResponse, raspi_schema.RaspiMessageResponse],
117117
)
118118
async def get_message(id: int, db: AsyncSession = Depends(get_db)):
119119
# 同coupleのidを取得
120120
boddy_id = await get_user_id_same_couple(db, id)
121121
# 同coupleのファイルをダウンロード
122-
response = download_blob_file(id, str(boddy_id), blob_service_client)
123-
return response
122+
is_downloaded = is_downloaded_blob(str(boddy_id), blob_service_client)
123+
124+
if is_downloaded:
125+
current_dir = os.path.dirname(os.path.abspath(__file__))
126+
file_path = os.path.join(current_dir, f"../../downloads/{boddy_id}.wav")
127+
return FileResponse(
128+
path=file_path, media_type="audio/wav", filename=f"{boddy_id}.wav"
129+
)
130+
131+
return {"id": id, "message": "相方のファイルは見つかりませんでした"}
124132

125133

126134
@router.get(

0 commit comments

Comments
 (0)