From 0f66c6ab95582c40cbd7901a61845bb09ac9d61b Mon Sep 17 00:00:00 2001 From: Rahul Sunil Date: Tue, 1 Oct 2024 00:32:16 +0530 Subject: [PATCH 1/2] GHAction to check Image Size --- .github/workflows/check-image-upload-size | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/check-image-upload-size diff --git a/.github/workflows/check-image-upload-size b/.github/workflows/check-image-upload-size new file mode 100644 index 000000000..9bce015d0 --- /dev/null +++ b/.github/workflows/check-image-upload-size @@ -0,0 +1,39 @@ +name: Check Image Upload Size + +on: + pull_request: + paths: + - '**/*.jpg' + - '**/*.jpeg' + - '**/*.png' + - '**/*.gif' + +jobs: + check_image_size: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check image sizes + run: | + #!/bin/bash + set -e + + MAX_SIZE_BYTES=$((5 * 1024 * 1024)) # 5MB in bytes + OVERSIZED_IMAGES=() + + for img in $(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -E '\.(jpg|jpeg|png|gif)$'); do + size=$(wc -c < "$img") + if [ $size -gt $MAX_SIZE_BYTES ]; then + OVERSIZED_IMAGES+=("$img ($(numfmt --to=iec-i --suffix=B --format="%.2f" $size))") + fi + done + + if [ ${#OVERSIZED_IMAGES[@]} -gt 0 ]; then + echo "Error: The following images exceed the maximum allowed size of 5MB:" + printf '%s\n' "${OVERSIZED_IMAGES[@]}" + exit 1 + else + echo "All new or modified images are within the size limit." + fi From ab0e82c5e0b914a822670ce0ead752a5ce56ec7d Mon Sep 17 00:00:00 2001 From: Rahul Sunil Date: Tue, 1 Oct 2024 00:32:53 +0530 Subject: [PATCH 2/2] Refactored --- .github/workflows/check-image-upload-size | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-image-upload-size b/.github/workflows/check-image-upload-size index 9bce015d0..d9c9306e3 100644 --- a/.github/workflows/check-image-upload-size +++ b/.github/workflows/check-image-upload-size @@ -11,6 +11,8 @@ on: jobs: check_image_size: runs-on: ubuntu-latest + env: + MAX_IMAGE_SIZE_MB: 5 steps: - name: Checkout code uses: actions/checkout@v3 @@ -20,7 +22,7 @@ jobs: #!/bin/bash set -e - MAX_SIZE_BYTES=$((5 * 1024 * 1024)) # 5MB in bytes + MAX_SIZE_BYTES=$((${MAX_IMAGE_SIZE_MB} * 1024 * 1024)) OVERSIZED_IMAGES=() for img in $(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -E '\.(jpg|jpeg|png|gif)$'); do @@ -31,7 +33,7 @@ jobs: done if [ ${#OVERSIZED_IMAGES[@]} -gt 0 ]; then - echo "Error: The following images exceed the maximum allowed size of 5MB:" + echo "Error: The following images exceed the maximum allowed size of ${MAX_IMAGE_SIZE_MB}MB:" printf '%s\n' "${OVERSIZED_IMAGES[@]}" exit 1 else