Skip to content

Commit

Permalink
🐛 fix(uploadService): 대문자 확장자에서 오류 나는 문제, is not in list 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kms0219kms committed Apr 9, 2024
1 parent 9281bf1 commit 159900c
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/services/uploadService.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ def __init__(self):
self.nasStation = filestation.FileStationPlugin().getFileStation()

async def upload(self, type: UploadType, message: Message) -> tuple[int, str]:
# 데이터 시트 Open
worksheet = self.google_util.get_worksheet_by_index(
key="1hfW3FTo9cjuMW9Kxvfnrbc6p_HyEnyYeA38mKM7nrOE", index=type.value
)

worksheet_data = worksheet.get_all_records(empty2zero=True)

# 짤 이름만 골라서 Get
resv_zzal_names = [x.get("이름") for x in worksheet_data]

uploaded = 0
errors = []

for file in message.attachments:
filename = file.filename
# 2024-04-09 FIX: 대문자 확장자에서 오류 나는 문제 수정
filename = file.filename.lower()

if not (
filename.endswith(".webp")
Expand All @@ -52,12 +56,25 @@ async def upload(self, type: UploadType, message: Message) -> tuple[int, str]:
errors.append(_error_msg)
continue

# 업로드 하는 짤 이름 GET (YAML 타입)
zzal_name = (
message.content.split("time: ")[1] if type == UploadType.TIME else message.content.split("name: ")[1]
)

# 2024-04-09 FIX: 시트에 등록되어 있는 짤인지 확인하기 (~~ is not in list 문제 수정)
if not zzal_name in resv_zzal_names:
_error_msg = f"`{zzal_name}` 시트에 등록되지 않은 짤 이름입니다. 등록 후 다시 시도해 주세요."

if len(message.attachments) == 1:
raise Exception(_error_msg)
else:
errors.append(_error_msg)
continue

# 등록되어 있다면 어느 row에 있는지 GET
row = resv_zzal_names.index(zzal_name)

# ~~시 - 인물 이름 Formatting
member_dict_name = (
(
worksheet_data[row]
Expand All @@ -75,15 +92,7 @@ async def upload(self, type: UploadType, message: Message) -> tuple[int, str]:
else None
)

if not zzal_name in resv_zzal_names:
_error_msg = f"`{zzal_name}` 시트에 등록되지 않은 짤 이름입니다. 등록 후 다시 시도해 주세요."

if len(message.attachments) == 1:
raise Exception(_error_msg)
else:
errors.append(_error_msg)
continue

# 짤 이미지 이름 Formatting
zzal_dict_name = (
zzal_name.replace(" ", "_")
.replace(":", "_")
Expand All @@ -95,15 +104,18 @@ async def upload(self, type: UploadType, message: Message) -> tuple[int, str]:
.replace('"', "_")
)

# 임시 폴더에 저장
await file.save(join("temp/", f"{message.author.id}_{filename}"))

if filename.endswith(".jpg") or filename.endswith(".png"):
# 파일이 jpg/jpeg/png라면
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
converted_image = Image.open(join("temp/", f"{message.author.id}_{filename}"))
converted_image.save(
join("temp/", f"{message.author.id}_{re.sub(r'\.(jpg|jpeg|png)$', '.webp', filename)}"), "webp"
)
converted_image.close()

# 업로드할 폴더 지정
upload_dest_path = (
f"/files/zzals/{zzal_dict_name}"
if type == UploadType.ZZAL
Expand All @@ -114,6 +126,7 @@ async def upload(self, type: UploadType, message: Message) -> tuple[int, str]:
)
)

# 업로드 처리
uploadRes = self.nasStation.upload_file(
dest_path=upload_dest_path,
file_path=join("temp/", f"{message.author.id}_{re.sub(r'\.(jpg|jpeg|png)$', '.webp', filename)}"),
Expand All @@ -131,6 +144,7 @@ async def upload(self, type: UploadType, message: Message) -> tuple[int, str]:
errors.append(_error_msg)
continue

# 임시 파일 정리
os.remove(join("temp/", f"{message.author.id}_{filename}"))

if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
Expand Down

0 comments on commit 159900c

Please sign in to comment.