fix: Rewrite the logic to better handle the cycle and properly regenerate images; clean with shfmt/shellcheck

This commit is contained in:
Carlos Solís 2025-02-05 15:03:28 +00:00
parent 39c4369f12
commit e001f8706c

View file

@ -8,79 +8,123 @@ folderperm=750
db=friendica db=friendica
folder=/var/www/friendica folder=/var/www/friendica
#Internal parameters: #Internal parameters:
#Amount of times the loop has run #Amount of times the internal loop has run
iteration=0 batch=0
#Number of invalid avatars found. Set to 1 initially so we can run the loop at least once #Number of invalid avatars found. Set to 1 initially so we can run the loop at least once
n=1 n=1
#Number of entries processed #Number of entries processed this loop
nx=0 nx=0
#Total number of entries processed
nt=0
#Last known ID to have been successfully processed #Last known ID to have been successfully processed
lastid=0 lastid=0
#Generate an index to make searches faster #Highest possible ID known
((indexlength=37+${#url})) maxid=$(mariadb "${db}" -B -N -q -e "select max(\`id\`) from contact")
echo "Generating photo index..." #Limit per batch
mariadb $db -e "alter table contact add index if not exists photo_index (photo($indexlength))" #limit=1000
limit=$maxid
if [[ -f /tmp/lastid ]]; then
rm /tmp/lastid && touch /tmp/lastid
else
touch /tmp/lastid
fi
#Go to the Friendica installation #Go to the Friendica installation
cd $folder || exit cd "${folder}" || exit
#Loop at least once, until no invalid avatars are found #Add to the loop, reset values
until [[ $n -eq 0 ]] n=0
do nt=0
#Add to the loop, reset values maxid=$(mariadb "${db}" -B -N -q -e "select max(\`id\`) from contact")
iteration=$(("$iteration" + 1)) dbcount=$(mariadb "${db}" -B -N -q -e "select count(\`id\`) from contact where photo like 'https:\/\/${url}/avatar/%' and (id in (select cid from \`user-contact\`) or id in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`))")
n=0 echo "${dbcount}"
nx=0 until [[ $((nt + limit)) -ge ${dbcount} ]]; do
dblist=$(mariadb $db -B -N -q -e "select id, photo, thumb, micro from contact where id > $lastid and photo like 'https:\/\/$url/avatar/%' order by id") nx=0
m=$(echo "$dblist" | wc -l) maxid=$(mariadb "${db}" -B -N -q -e "select max(\`id\`) from contact")
echo "$dblist" | while read -r id photo thumb micro batch=$(("${batch}" + 1))
do #Read lastid outside of the loop with a temporary file
nx=$(("$nx" + 1)) if [[ -f /tmp/lastid && -s /tmp/lastid ]]; then
folderescaped=${folder////\\/} while read -r n_i nt_i lastid_i; do
#Substitute the URL path with the folder path so we can search for it in the local file system if [[ -s "${n_i}" && -s "${nt_i}" && -n "${lastid_i}" ]]; then
#Photo is nominally 320px, actually 300px n="${n_i}"
k_photo=$(echo "$photo" | sed -e "s/https:\/\/$url/$folderescaped/g" -e "s/\?ts=.*//g") nt="${nt_i}"
#Thumb is 80px lastid="${lastid_i}"
k_thumb=$(echo "$thumb" | sed -e "s/https:\/\/$url/$folderescaped/g" -e "s/\?ts=.*//g") fi
#Micro is 48px done < <(cat /tmp/lastid)
k_micro=$(echo "$micro" | sed -e "s/https:\/\/$url/$folderescaped/g" -e "s/\?ts=.*//g") fi
#If any of the images is not found in the filesystem dboutput=$(mariadb "${db}" -B -N -q -e "select \`id\`, \`photo\`, \`thumb\`, \`micro\` from \`contact\` where \`id\` > ${lastid} and \`photo\` like \"https:\/\/${url}/avatar/%\" and (\`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`)) order by id limit ${limit}")
if [[ ! -e "$k_photo" || ! -e "$k_thumb" || ! -e "$k_micro" ]] echo "${dboutput}" | while read -r id photo thumb micro; do
then if [[ -n "${id}" && -n "${photo}" && -n "${thumb}" && -n "${micro}" ]]; then
#If the avatar uses the standard fallback picture or is local, we cannot use it as a base nx=$(("${nx}" + 1))
avatar=$(mariadb $db -B -N -q -e "select avatar from contact where id = \"$id\" and not avatar like \"%$url\" and not avatar like \"%images/person%\"") nt=$(("${nt}" + 1))
#If we have a remote avatar as a fallback, download it error_found=0
if [[ $! -eq 0 && -n $avatar ]] folderescaped=${folder////\\/}
then #Substitute the URL path with the folder path so we can search for it in the local file system
echo "$id $avatar" #Photo is nominally 320px, actually 300px
sudo -u $user curl "$avatar" -s -o "$k_photo" k_photo=$(echo "${photo}" | sed -e "s/https:\/\/${url}/${folderescaped}/g" -e "s/\?ts=.*//g")
#If the file is a valid picture (not empty, not text) #Thumb is 80px
if file "$k_photo" | grep -q -v -e "text" -e "empty" -e "symbolic link" -e "directory" k_thumb=$(echo "${thumb}" | sed -e "s/https:\/\/${url}/${folderescaped}/g" -e "s/\?ts=.*//g")
then #Micro is 48px
#Also fetch for thumb/micro and resize k_micro=$(echo "${micro}" | sed -e "s/https:\/\/${url}/${folderescaped}/g" -e "s/\?ts=.*//g")
#As the photo is the largest version we have, we will use it as the base, and leave it last to convert #If fetching any of the images causes an error
convert "$k_photo" -resize 80x80 -depth 16 "$k_thumb" && chmod "$fileperm" "$k_thumb" && chown "$user:$group" "$k_thumb" if curl -s "${photo}" | file - | grep -q -e "text" -e "empty" -e "symbolic link" -e "directory" ||
convert "$k_photo" -resize 48x48 -depth 16 "$k_micro" && chmod "$fileperm" "$k_micro" && chown "$user:$group" "$k_micro" curl -s "${thumb}" | file - | grep -q -e "text" -e "empty" -e "symbolic link" -e "directory" ||
convert "$k_photo" -resize 300x300 -depth 16 "$k_photo" && chmod "$fileperm" "$k_photo" && chown "$user:$group" "$k_photo" curl -s "${micro}" | file - | grep -q -e "text" -e "empty" -e "symbolic link" -e "directory"; then
else #Request the user data to be regenerated in the system through the database
#If the avatar is not valid, set it as blank in the database mariadb "${db}" -N -B -q -e "update contact set avatar= \"\", photo = \"\", thumb = \"\", micro = \"\" where id = \"${id}\""
mariadb $db -e "update contact set avatar= \"\", photo = \"\", thumb = \"\", micro = \"\" where id = \"$id\"" mariadb "${db}" -N -B -q -e "insert ignore into workerqueue (command, parameter, priority) values (\"UpdateContact\", \"[${id}]\", 20);"
rm -rf "$k_photo" echo "${id} ${photo}"
fi error_found=1
else fi
#If no remote avatar is found, then we blank the photo/thumb/micro and let the avatar cache process fix them later #If any of the images is not found in the filesystem
mariadb $db -e "update contact set photo = \"\", thumb = \"\", micro = \"\" where id = \"$id\"" if [[ ! -e "${k_photo}" || ! -e "${k_thumb}" || ! -e "${k_micro}" ]]; then
fi #If the avatar uses the standard fallback picture or is local, we cannot use it as a base
n=$(( n + 1 )) avatar=$(mariadb "${db}" -B -N -q -e "select avatar from contact where id = \"${id}\" and not avatar like \"%${url}\" and not avatar like \"%images/person%\"")
fi #If we have a remote avatar as a fallback, download it
lastid="${id}" if [[ $! -eq 0 && -n ${avatar} ]]; then
printf "\rIteration %s\tPhotos: %s\tEntry %s/%s " "$iteration" "$n" "$nx" "$m" echo "${id} ${avatar}"
done sudo -u "${user}" curl "${avatar}" -s -o "${k_photo}"
wait #If the file is a valid picture (not empty, not text)
printf "\nFixing folders and moving to avatar cache...\n" if file "${k_photo}" | grep -q -v -e "text" -e "empty" -e "symbolic link" -e "directory"; then
sudo -u $user bin/console movetoavatarcache #&> /dev/null #Also fetch for thumb/micro and resize
find ./avatar -depth -not -user "$user" -or -not -group "$group" -exec chown -v "$user:$group" {} \; #As the photo is the largest version we have, we will use it as the base, and leave it last to convert
find ./avatar -depth -type f -and -not -type d -and -not -perm "$fileperm" -exec chmod -v "$fileperm" {} \; convert "${k_photo}" -resize 80x80 -depth 16 "${k_thumb}" && chmod "${fileperm}" "${k_thumb}" && chown "${user}:${group}" "${k_thumb}"
find ./avatar -depth -type d -and -not -perm "$folderperm" -exec chmod -v "$folderperm" {} \; convert "${k_photo}" -resize 48x48 -depth 16 "${k_micro}" && chmod "${fileperm}" "${k_micro}" && chown "${user}:${group}" "${k_micro}"
#chown -R "$user:$group" ./avatar convert "${k_photo}" -resize 300x300 -depth 16 "${k_photo}" && chmod "${fileperm}" "${k_photo}" && chown "${user}:${group}" "${k_photo}"
else
#If the avatar is not valid, set it as blank in the database
mariadb "${db}" -N -B -q -e "update contact set avatar= \"\", photo = \"\", thumb = \"\", micro = \"\" where id = \"${id}\""
rm -rf "${k_photo}"
fi
#Request the user data to be regenerated in the system through the database
mariadb "${db}" -N -B -q -e "insert ignore into workerqueue (command, parameter, priority) values (\"UpdateContact\", \"[${id}]\", 20, CURTIME());"
else
echo "${id}"
#If no remote avatar is found, then we blank the photo/thumb/micro and let the avatar cache process fix them later
mariadb "${db}" -e "update contact set photo = \"\", thumb = \"\", micro = \"\" where id = \"${id}\""
#Request the user data to be regenerated in the system through the database
mariadb "${db}" -N -B -q -e "insert ignore into workerqueue (command, parameter, priority, created) values (\"UpdateContact\", \"[${id}]\", 20, CURTIME());"
fi
error_found=1
k_photo_delta=$(echo "${photo}" | sed -e "s/.*?ts=//g")
else
k_photo_original_time=$(echo "${photo}" | sed -e "s/.*?ts=//g")
k_photo_found_time=$(stat -c%W "${k_photo}")
k_photo_delta=$((k_photo_found_time - k_photo_original_time))
fi
if [[ "${error_found}" -gt 0 ]]; then
n=$((n + 1))
fi
fi
lastid="${id}"
touch /tmp/lastid
echo "${n} ${nt} ${lastid}" >/tmp/lastid
printf "\rB. %5d Fd. %8d E. %8d Ct. %4d/%4d To. %8d/%8d Dt. %d " "${batch}" "${n}" "${nt}" "${nx}" "${limit}" "${lastid}" "${maxid}" "${k_photo_delta}"
done
done done
#Drop index in the end to save storage printf "\nFixing folders and moving to avatar cache...\n"
mariadb $db -e "alter table contact drop index photo_index" #sudo -u "${user}" bin/console movetoavatarcache #&> /dev/null
"${folder}"/bin/console movetoavatarcache #&> /dev/null
find ./avatar -depth -not -user "${user}" -or -not -group "${group}" -exec chown -v "${user}:${group}" {} \;
find ./avatar -depth -type f -and -not -type d -and -not -perm "${fileperm}" -exec chmod -v "${fileperm}" {} \;
find ./avatar -depth -type d -and -not -perm "${folderperm}" -exec chmod -v "${folderperm}" {} \;
chown -R "${user}:${group}" ./avatar
rm /tmp/lastid