Skip to content

Commit

Permalink
fix: remove uuid prefix in attachment creation
Browse files Browse the repository at this point in the history
  • Loading branch information
gary-Shen committed Nov 18, 2024
1 parent 3589d22 commit f0820ff
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
91 changes: 46 additions & 45 deletions labelu/internal/application/service/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,57 @@ async def create(
status_code=status.HTTP_404_NOT_FOUND,
)

# save file
try:
# file relative path
path_filename = cmd.file.filename.split("/")
filename = str(uuid.uuid4())[0:8] + "-" + path_filename[-1]
path = "/".join(path_filename[:-1])
attachment_relative_base_dir = Path(settings.UPLOAD_DIR).joinpath(
str(task_id), path
)
attachment_relative_path = str(attachment_relative_base_dir.joinpath(filename))
path_filename = cmd.file.filename.split("/")
# filename = str(uuid.uuid4())[0:8] + "-" + path_filename[-1]
filename = path_filename[-1]
path = "/".join(path_filename[:-1])
attachment_relative_base_dir = Path(settings.UPLOAD_DIR).joinpath(
str(task_id), path
)
attachment_relative_path = str(attachment_relative_base_dir.joinpath(filename))

# file full path
attachment_full_base_dir = Path(settings.MEDIA_ROOT).joinpath(
attachment_relative_base_dir
)
attachment_full_path = Path(settings.MEDIA_ROOT).joinpath(
attachment_relative_path
)
# file full path
attachment_full_base_dir = Path(settings.MEDIA_ROOT).joinpath(
attachment_relative_base_dir
)
attachment_full_path = Path(settings.MEDIA_ROOT).joinpath(
attachment_relative_path
)

# create dicreatory
attachment_full_base_dir.mkdir(parents=True, exist_ok=True)

# save image
logger.info(attachment_full_path)
async with aiofiles.open(attachment_full_path, "wb") as out_file:
content = await cmd.file.read() # async read
await out_file.write(content) # async write

# create thumbnail for image
if cmd.file.content_type.startswith("image/"):
tumbnail_full_path = Path(
f"{attachment_full_path.parent}/{attachment_full_path.stem}-thumbnail{attachment_full_path.suffix}"
)
logger.info(tumbnail_full_path)
image = Image.open(attachment_full_path)
image.thumbnail(
(
round(image.width / image.height * settings.THUMBNAIL_HEIGH_PIXEL),
settings.THUMBNAIL_HEIGH_PIXEL,
),
)
if image.mode != "RGB":
image = image.convert("RGB")
image.save(tumbnail_full_path)
except Exception as e:
logger.error(e)
# create dicreatory
attachment_full_base_dir.mkdir(parents=True, exist_ok=True)

# check file exist
if attachment_full_path.exists():
logger.error("file already exists:{}", attachment_full_path)
raise LabelUException(
code=ErrorCode.CODE_51000_CREATE_ATTACHMENT_ERROR,
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
code=ErrorCode.CODE_51002_TASK_ATTACHMENT_ALREADY_EXISTS,
status_code=status.HTTP_400_BAD_REQUEST,
)

# save image
logger.info(attachment_full_path)
async with aiofiles.open(attachment_full_path, "wb") as out_file:
content = await cmd.file.read() # async read
await out_file.write(content) # async write

# create thumbnail for image
if cmd.file.content_type.startswith("image/"):
tumbnail_full_path = Path(
f"{attachment_full_path.parent}/{attachment_full_path.stem}-thumbnail{attachment_full_path.suffix}"
)
logger.info(tumbnail_full_path)
image = Image.open(attachment_full_path)
image.thumbnail(
(
round(image.width / image.height * settings.THUMBNAIL_HEIGH_PIXEL),
settings.THUMBNAIL_HEIGH_PIXEL,
),
)
if image.mode != "RGB":
image = image.convert("RGB")
image.save(tumbnail_full_path)

# check file already saved
if not attachment_full_path.exists() or (
Expand Down
4 changes: 4 additions & 0 deletions labelu/internal/common/error_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class ErrorCode(Enum):
"Attachment file not found",
)

CODE_51002_TASK_ATTACHMENT_ALREADY_EXISTS =(
TASK_INIT_CODE + 1002,
"Attachment file already exists",
)
# task sample error code
CODE_55000_SAMPLE_LIST_PARAMETERS_ERROR = (
TASK_INIT_CODE + 5000,
Expand Down

0 comments on commit f0820ff

Please sign in to comment.