Skip to content

Commit

Permalink
add check for image mime types that aren't supported
Browse files Browse the repository at this point in the history
  • Loading branch information
kx1t committed Jan 16, 2025
1 parent eb3acf6 commit e485b34
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions rootfs/scripts/post2bsky.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,36 @@ for image in "${IMAGES[@]}"; do
# figure out what type the image is: jpeg, png, gif, and reduce size if necessary/possible.
mimetype_local="$(file --mime-type -b "$image")"
imgsize_org="$(stat -c%s "$image")"
if (( imgsize_org >= 950000 )); then
if [[ "$mimetype_local" == "image/jpeg" ]]; then
jpegoptim -q -S950 -s "$image" # if it's JPG and > 1 MB, we can optimize for it
# try again if still too big
if (( $(stat -c%s "$image") >= 950000 )); then
jpegoptim -q -S850 -s "$image"
fi
elif [[ "$mimetype_local" == "image/png" ]]; then
pngquant -f -o "${image}.tmp" 64 "$image" # if it's PNG and > 1 MB, we can optimize for it
mv -f "${image}.tmp" "$image"
else
"${s6wrap[@]}" echo "Omitting image $image as it is too big ($imgsize)"
continue # skip if it's not JPG or PNG
fi
"${s6wrap[@]}" echo "Image size of $image reduced from $imgsize_org to $(stat -c%s "$image")"
fi
if (( $(stat -c%s "$image") >= 950000 )); then
"${s6wrap[@]}" echo "Omitting image $image as the size reduction was insufficient: before: $imgsize_org; now: $(stat -c%s "$image")"
continue;
fi # skip if it's still > 1MB

if (( imgsize_org < 10000 )); then
"${s6wrap[@]}" echo "Image size for $image is less than 10 KB ($imgsize_org). Is it really an image? Skipping..."
continue
fi
# shellcheck disable=SC2076
if [[ ! " jpeg jpg gif png " =~ " ${mimetype_local##*/} " ]]; then
"${s6wrap[@]}" echo "Omitting image $image as it is not jpg, gif, or png. (Reported mimetype is ${mimetype_local##*/})"
continue
fi
if (( imgsize_org >= 950000 )); then
if [[ "$mimetype_local" == "image/jpeg" ]]; then
jpegoptim -q -S950 -s "$image" # if it's JPG and > 1 MB, we can optimize for it
# try again if still too big
if (( $(stat -c%s "$image") >= 950000 )); then
jpegoptim -q -S850 -s "$image"
fi
elif [[ "$mimetype_local" == "image/png" ]]; then
pngquant -f -o "${image}.tmp" 64 "$image" # if it's PNG and > 1 MB, we can optimize for it
mv -f "${image}.tmp" "$image"
else
"${s6wrap[@]}" echo "Omitting image $image as it is too big ($imgsize)"
continue # skip if it's not JPG or PNG
fi
"${s6wrap[@]}" echo "Image size of $image reduced from $imgsize_org to $(stat -c%s "$image")"
fi
if (( $(stat -c%s "$image") >= 950000 )); then
"${s6wrap[@]}" echo "Omitting image $image as the size reduction was insufficient: before: $imgsize_org; now: $(stat -c%s "$image")"
continue;
fi # skip if it's still > 1MB

#Send the image to Bluesky
response="$(curl -v -sL -X POST "$BLUESKY_API/com.atproto.repo.uploadBlob" \
Expand Down

0 comments on commit e485b34

Please sign in to comment.