diff --git a/meta/files/zip_image.sh b/meta/files/zip_image.sh index 95ab50b7..5fe63011 100644 --- a/meta/files/zip_image.sh +++ b/meta/files/zip_image.sh @@ -1,34 +1,144 @@ #!/bin/bash -file=comp.flag -if [ -f "$file" ]; then - option="-newer $file" -fi +# Настройки +IMAGE_DIR="./images" +COMP_DIR="$IMAGE_DIR/comp" +WEBP_DIR="$IMAGE_DIR/webp" +THREADS=4 # Количество параллельных процессов -find ./images/ -type f -not -path "./images/comp/*" ! -name "*-no-comp.*" $option -iname "*.png" -exec sh -c ' - png_file="./images/comp/${1#*/images/}" - png_dir="$(dirname "$png_file")" - mkdir -p "$png_dir" - cp "$1" "$png_file" - optipng -o7 "$png_file" - advpng -z4 "$png_file" - pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow "$png_file" -' _ {} \; +# Экспортируем необходимые переменные и функции для использования в subshell +export IMAGE_DIR COMP_DIR WEBP_DIR -find ./images/ -type f -not -path "./images/comp/*" ! -name "*-no-comp.*" $option -iregex '.*\.\(jpg\|jpeg\)' -exec sh -c ' - jpg_file="./images/comp/${1#*/images/}" - jpg_dir="$(dirname "$jpg_file")" - mkdir -p "$jpg_dir" - cp "$1" "$jpg_file" - jpegoptim --all-progressive "$jpg_file" -' _ {} \; +# Функция для обработки PNG файлов +process_png() { + local input_file="$1" + local relative_path="${input_file#$IMAGE_DIR/}" + local output_file="$COMP_DIR/$relative_path" + local output_dir="$(dirname "$output_file")" -find ./images/comp -type f -iregex '.*\.\(jpg\|jpeg\|png\)' -not -iregex '.*no-comp\.\(jpg\|jpeg\|png\)' $option -exec sh -c ' - webp_file="./images/webp/${1#*/images/comp/}" - webp_dir="$(dirname "$webp_file")" - mkdir -p "$webp_dir" - cwebp -mt -af -progress -m 6 -q 75 -pass 10 "$1" -o "${webp_file%.*}.webp" -' _ {} \; + mkdir -p "$output_dir" -touch $file -echo "$(date)" > $file \ No newline at end of file + # Проверка хеша файла + local hash_file="${output_file}.md5" + local current_hash + current_hash="$(md5sum "$input_file" | awk '{print $1}')" + + if [ -f "$hash_file" ]; then + local previous_hash + previous_hash="$(cat "$hash_file")" + if [ "$current_hash" == "$previous_hash" ]; then + echo "PNG файл не изменился: $input_file" + return + fi + fi + + cp "$input_file" "$output_file" + + if ! optipng -o7 "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью optipng" >&2 + return 1 + fi + + if ! advpng -z4 "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью advpng" >&2 + return 1 + fi + + if ! pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью pngcrush" >&2 + return 1 + fi + + echo "$current_hash" > "$hash_file" +} + +# Функция для обработки JPEG файлов +process_jpeg() { + local input_file="$1" + local relative_path="${input_file#$IMAGE_DIR/}" + local output_file="$COMP_DIR/$relative_path" + local output_dir="$(dirname "$output_file")" + + mkdir -p "$output_dir" + + # Проверка хеша файла + local hash_file="${output_file}.md5" + local current_hash + current_hash="$(md5sum "$input_file" | awk '{print $1}')" + + if [ -f "$hash_file" ]; then + local previous_hash + previous_hash="$(cat "$hash_file")" + if [ "$current_hash" == "$previous_hash" ]; then + echo "JPEG файл не изменился: $input_file" + return + fi + fi + + cp "$input_file" "$output_file" + + if ! jpegoptim --all-progressive "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью jpegoptim" >&2 + return 1 + fi + + echo "$current_hash" > "$hash_file" +} + +# Функция для конвертации в WebP +process_webp() { + local input_file="$1" + local relative_path="${input_file#$COMP_DIR/}" + local output_file="$WEBP_DIR/$relative_path" + output_file="${output_file%.*}.webp" + local output_dir="$(dirname "$output_file")" + + mkdir -p "$output_dir" + + # Проверка хеша файла + local hash_file="${output_file}.md5" + local current_hash + current_hash="$(md5sum "$input_file" | awk '{print $1}')" + + if [ -f "$hash_file" ]; then + local previous_hash + previous_hash="$(cat "$hash_file")" + if [ "$current_hash" == "$previous_hash" ]; then + echo "WebP файл не изменился: $input_file" + return + fi + fi + + if ! cwebp -mt -af -progress -m 6 -q 75 -pass 10 "$input_file" -o "$output_file"; then + echo "Ошибка при конвертации $input_file в WebP" >&2 + return 1 + fi + + echo "$current_hash" > "$hash_file" +} + +# Экспорт функций для использования в subshell +export -f process_png +export -f process_jpeg +export -f process_webp + +# Обработка PNG файлов +find "$IMAGE_DIR" -type f \ + -not -path "$COMP_DIR/*" \ + ! -name "*-no-comp.*" \ + -iname "*.png" -print0 | \ +xargs -0 -P "$THREADS" -I {} bash -c 'process_png "$@"' _ {} + +# Обработка JPEG файлов +find "$IMAGE_DIR" -type f \ + -not -path "$COMP_DIR/*" \ + ! -name "*-no-comp.*" \ + -iregex '.*\.\(jpg\|jpeg\)' -print0 | \ +xargs -0 -P "$THREADS" -I {} bash -c 'process_jpeg "$@"' _ {} + +# Конвертация в WebP +find "$COMP_DIR" -type f \ + -iregex '.*\.\(jpg\|jpeg\|png\)' \ + -not -iregex '.*no-comp\.\(jpg\|jpeg\|png\)' \ + -print0 | \ +xargs -0 -P "$THREADS" -I {} bash -c 'process_webp "$@"' _ {} \ No newline at end of file