|
48 | 48 | from lib.core.enums import ProjectType |
49 | 49 | from lib.core.exceptions import AppException |
50 | 50 | from lib.core.exceptions import AppValidationException |
| 51 | +from lib.core.exceptions import ImageProcessingException |
51 | 52 | from lib.core.plugin import ImagePlugin |
52 | 53 | from lib.core.plugin import VideoPlugin |
53 | 54 | from lib.core.repositories import BaseManageableRepository |
54 | 55 | from lib.core.repositories import BaseReadOnlyRepository |
55 | 56 | from lib.core.response import Response |
56 | 57 | from lib.core.serviceproviders import SuerannotateServiceProvider |
| 58 | +from PIL import UnidentifiedImageError |
57 | 59 |
|
58 | 60 | logger = logging.getLogger() |
59 | 61 |
|
@@ -498,64 +500,77 @@ def max_resolution(self) -> int: |
498 | 500 |
|
499 | 501 | def execute(self): |
500 | 502 | image_name = Path(self._image_path).name |
501 | | - image_processor = ImagePlugin(self._image, self.max_resolution) |
502 | | - origin_width, origin_height = image_processor.get_size() |
503 | | - thumb_image, _, _ = image_processor.generate_thumb() |
504 | | - huge_image, huge_width, huge_height = image_processor.generate_huge() |
505 | | - quality = 60 |
506 | | - if not self._image_quality_in_editor: |
507 | | - for setting in self._project_settings.get_all(): |
508 | | - if setting.attribute == "ImageQuality": |
509 | | - quality = setting.value |
510 | | - else: |
511 | | - quality = ImageQuality.get_value(self._image_quality_in_editor) |
512 | | - if Path(image_name).suffix[1:].upper() in ("JPEG", "JPG"): |
513 | | - if quality == 100: |
514 | | - self._image.seek(0) |
515 | | - low_resolution_image = self._image |
| 503 | + try: |
| 504 | + image_processor = ImagePlugin(self._image, self.max_resolution) |
| 505 | + origin_width, origin_height = image_processor.get_size() |
| 506 | + thumb_image, _, _ = image_processor.generate_thumb() |
| 507 | + huge_image, huge_width, huge_height = image_processor.generate_huge() |
| 508 | + quality = 60 |
| 509 | + if not self._image_quality_in_editor: |
| 510 | + for setting in self._project_settings.get_all(): |
| 511 | + if setting.attribute == "ImageQuality": |
| 512 | + quality = setting.value |
516 | 513 | else: |
517 | | - low_resolution_image, _, _ = image_processor.generate_low_resolution( |
518 | | - quality=quality |
519 | | - ) |
520 | | - else: |
521 | | - if quality == 100: |
522 | | - low_resolution_image, _, _ = image_processor.generate_low_resolution( |
523 | | - quality=quality, subsampling=0 |
524 | | - ) |
| 514 | + quality = ImageQuality.get_value(self._image_quality_in_editor) |
| 515 | + if Path(image_name).suffix[1:].upper() in ("JPEG", "JPG"): |
| 516 | + if quality == 100: |
| 517 | + self._image.seek(0) |
| 518 | + low_resolution_image = self._image |
| 519 | + else: |
| 520 | + ( |
| 521 | + low_resolution_image, |
| 522 | + _, |
| 523 | + _, |
| 524 | + ) = image_processor.generate_low_resolution(quality=quality) |
525 | 525 | else: |
526 | | - low_resolution_image, _, _ = image_processor.generate_low_resolution( |
527 | | - quality=quality, subsampling=-1 |
528 | | - ) |
529 | | - image_key = ( |
530 | | - self._upload_path + str(uuid.uuid4()) + Path(self._image_path).suffix |
531 | | - ) |
| 526 | + if quality == 100: |
| 527 | + ( |
| 528 | + low_resolution_image, |
| 529 | + _, |
| 530 | + _, |
| 531 | + ) = image_processor.generate_low_resolution( |
| 532 | + quality=quality, subsampling=0 |
| 533 | + ) |
| 534 | + else: |
| 535 | + ( |
| 536 | + low_resolution_image, |
| 537 | + _, |
| 538 | + _, |
| 539 | + ) = image_processor.generate_low_resolution( |
| 540 | + quality=quality, subsampling=-1 |
| 541 | + ) |
| 542 | + image_key = ( |
| 543 | + self._upload_path + str(uuid.uuid4()) + Path(self._image_path).suffix |
| 544 | + ) |
532 | 545 |
|
533 | | - file_entity = S3FileEntity(uuid=image_key, data=self._image) |
| 546 | + file_entity = S3FileEntity(uuid=image_key, data=self._image) |
534 | 547 |
|
535 | | - thumb_image_name = image_key + "___thumb.jpg" |
536 | | - thumb_image_entity = S3FileEntity(uuid=thumb_image_name, data=thumb_image) |
537 | | - self._s3_repo.insert(thumb_image_entity) |
| 548 | + thumb_image_name = image_key + "___thumb.jpg" |
| 549 | + thumb_image_entity = S3FileEntity(uuid=thumb_image_name, data=thumb_image) |
| 550 | + self._s3_repo.insert(thumb_image_entity) |
538 | 551 |
|
539 | | - low_resolution_image_name = image_key + "___lores.jpg" |
540 | | - low_resolution_file_entity = S3FileEntity( |
541 | | - uuid=low_resolution_image_name, data=low_resolution_image |
542 | | - ) |
543 | | - self._s3_repo.insert(low_resolution_file_entity) |
| 552 | + low_resolution_image_name = image_key + "___lores.jpg" |
| 553 | + low_resolution_file_entity = S3FileEntity( |
| 554 | + uuid=low_resolution_image_name, data=low_resolution_image |
| 555 | + ) |
| 556 | + self._s3_repo.insert(low_resolution_file_entity) |
544 | 557 |
|
545 | | - huge_image_name = image_key + "___huge.jpg" |
546 | | - huge_file_entity = S3FileEntity( |
547 | | - uuid=huge_image_name, |
548 | | - data=huge_image, |
549 | | - metadata={"height": huge_width, "weight": huge_height}, |
550 | | - ) |
551 | | - self._s3_repo.insert(huge_file_entity) |
552 | | - file_entity.data.seek(0) |
553 | | - self._s3_repo.insert(file_entity) |
554 | | - self._response.data = ImageEntity( |
555 | | - name=image_name, |
556 | | - path=image_key, |
557 | | - meta=ImageInfoEntity(width=origin_width, height=origin_height), |
558 | | - ) |
| 558 | + huge_image_name = image_key + "___huge.jpg" |
| 559 | + huge_file_entity = S3FileEntity( |
| 560 | + uuid=huge_image_name, |
| 561 | + data=huge_image, |
| 562 | + metadata={"height": huge_width, "weight": huge_height}, |
| 563 | + ) |
| 564 | + self._s3_repo.insert(huge_file_entity) |
| 565 | + file_entity.data.seek(0) |
| 566 | + self._s3_repo.insert(file_entity) |
| 567 | + self._response.data = ImageEntity( |
| 568 | + name=image_name, |
| 569 | + path=image_key, |
| 570 | + meta=ImageInfoEntity(width=origin_width, height=origin_height), |
| 571 | + ) |
| 572 | + except (ImageProcessingException, UnidentifiedImageError) as e: |
| 573 | + self._response.errors = e |
559 | 574 | return self._response |
560 | 575 |
|
561 | 576 |
|
|
0 commit comments