-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
demisto_sdk/commands/validate/validators/IM_validators/IM101_image_too_large.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterable, List | ||
|
||
from demisto_sdk.commands.content_graph.objects.integration import Integration | ||
from demisto_sdk.commands.content_graph.parsers.related_files import ( | ||
ImageRelatedFile, | ||
RelatedFileType, | ||
) | ||
from demisto_sdk.commands.validate.validators.base_validator import ( | ||
BaseValidator, | ||
ValidationResult, | ||
) | ||
|
||
IMAGE_MAX_SIZE = 10 * 1024 # 10kB | ||
|
||
ContentTypes = Integration | ||
|
||
|
||
class ImageTooLargeValidator(BaseValidator[ContentTypes]): | ||
error_code = "IM101" | ||
description = "Checks that the image file dimensions are matching the requirements." | ||
rationale = "Image needs to fit its place in the UI. For more information see: https://xsoar.pan.dev/docs/integrations/integration-logo" | ||
error_message = "You've created/modified a yml or package with a large sized image. Please make sure to change the image dimensions at: {0}." | ||
related_field = "image" | ||
related_file_type = [RelatedFileType.IMAGE] | ||
|
||
def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]: | ||
return [ | ||
ValidationResult( | ||
validator=self, | ||
message=self.error_message.format(content_item.name + "_image.png"), | ||
content_object=content_item, | ||
) | ||
for content_item in content_items | ||
if self.is_image_valid(content_item.image) | ||
] | ||
|
||
def is_image_valid(self, image: ImageRelatedFile): | ||
file_type = image.file_path.suffix | ||
if file_type == ".png": | ||
return image.get_file_size().st_size > IMAGE_MAX_SIZE | ||
|
||
elif file_type == ".svg": | ||
# No size validation done for SVG images | ||
return False | ||
|
||
image_size = int(((image.get_file_size().st_size - 22) / 4) * 3) | ||
return image_size > IMAGE_MAX_SIZE |