Skip to content

Instantly share code, notes, and snippets.

@vinayakg
Last active June 9, 2023 06:18
Show Gist options
  • Save vinayakg/d1b8f757414d8a64ee64617a47a1b54d to your computer and use it in GitHub Desktop.
Save vinayakg/d1b8f757414d8a64ee64617a47a1b54d to your computer and use it in GitHub Desktop.
download-images-transform
#!/bin/bash
# Function to process each file
process_file() {
local file="$1"
local relative_full_path="$2" # Full path of the file
local remote_file_path="http://3.84.160.28/images/$relative_full_path"
# fill an image if its smaller on one of the dimension using center crop
local url="https://mlznzveokpms.i.optimole.com/q:85/rs:fit/$remote_file_path"
#local url="https://mlznzveokpms.i.optimole.com/rs:fill:719:260/g:ce/$remote_file_path"
# crop an image to smaller dimension so dont specify fit or anything
#local url="https://mlznzveokpms.i.optimole.com/w:794/h:30/$remote_file_path"
local output_filename="${file%.*}.webp" # Output filename with extension changed to .png
local relative_path_only="${relative_full_path%/*}"
# echo "Processing: fullpath: $relative_full_path, outputpath: $relative_path_only/$output_filename"
set -x
# Run curl request to download the file
# curl --silent --write-out "%{http_code}\n" --max-time 10 "$url" --output "images/$relative_path_only/$output_filename"
response=$(curl --silent --max-time 20 --write-out "%{http_code}" "$url" --output "images/$relative_path_only/$output_filename")
set +x
if [[ "$response" == "200" ]]; then
echo "Downloaded: $dir_path/$output_filename"
else
echo "Error downloading: response: $response, url: $url, file: images/$relative_path_only/$output_filename"
fi
sleep 2
#echo "Downloaded: $output_filename $url"
}
# Function to traverse directories recursively
traverse_directory() {
local dir_path="$1"
local base_dir="$2" # Base directory path
# Iterate over files and directories in the specified directory
for item in "$dir_path"/*; do
if [[ -d "$item" ]]; then
if [[ "$item" == */destinationlq || "$item" == */flags || "$item" == */pdfassets || "$item" == */cards || "$item" == */destinations || "$item" == */flights ]]; then
continue # Skip the directory completely
fi
# If it's a directory, recursively call traverse_directory function with updated directory path
traverse_directory "$item" "$base_dir"
elif [[ -f "$item" ]]; then
# If it's a file, check if it's .jpg or .png and process it
if [[ "$item" == *.jpg || "$item" == *.png ]]; then
local relative_path="${item#$base_dir/}" # Get the relative path of the file
# echo "relative path: $relative_path and item: $item\n"
process_file "${item##*/}" "$relative_path" # Extract file name and pass relative path
fi
fi
done
}
# Check if the directory argument is provided
if [[ $# -ne 1 ]]; then
echo "Please provide a directory as an argument."
exit 1
fi
# Validate if the provided directory exists
if [[ ! -d "$1" ]]; then
echo "Invalid directory: $1"
exit 1
fi
# Start traversal from the specified directory
base_directory=$(realpath "$1") # Get the absolute path of the base directory
traverse_directory "$base_directory" "$base_directory"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment