2025-01-24 19:16:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
IFS="
|
|
|
|
"
|
|
|
|
#Set your parameters here
|
2025-01-28 03:00:57 +00:00
|
|
|
site=friendica.example.net
|
2025-01-24 19:16:21 +00:00
|
|
|
user=friendica
|
2025-02-06 14:36:48 +00:00
|
|
|
group=www-data
|
2025-02-09 02:29:19 +00:00
|
|
|
fileperm=660
|
2025-02-07 14:51:18 +00:00
|
|
|
folderperm=770
|
2025-01-24 19:16:21 +00:00
|
|
|
folder=/var/www/friendica
|
|
|
|
folderescaped=${folder////\\/}
|
|
|
|
tmpfile=/tmp/friendica-fix-avatar-permissions.txt
|
|
|
|
avatarfolder=avatar
|
2025-01-26 04:31:05 +00:00
|
|
|
|
2025-02-05 14:59:57 +00:00
|
|
|
loop_1() {
|
|
|
|
if [[ "${p}" =~ .jpeg || "${p}" =~ .jpg ]]; then
|
2025-01-28 03:00:57 +00:00
|
|
|
nice -n 10 jpegoptim -m 76 "${p}" #&> /dev/null
|
2025-02-05 14:59:57 +00:00
|
|
|
elif [[ "${p}" =~ .gif ]]; then
|
2025-01-28 03:00:57 +00:00
|
|
|
nice -n 10 gifsicle --batch -O3 --lossy=80 --colors=255 "${p}" #&> /dev/null
|
|
|
|
#Specific compression for large GIF files
|
2025-02-25 16:36:50 -06:00
|
|
|
while [[ $(stat -c%s "${p}" 2>/dev/null || echo 0) -ge 512000 ]]; do
|
2025-02-09 02:29:19 +00:00
|
|
|
frameamount=$(($(exiftool -b -FrameCount "${p}" || 1) - 1))
|
2025-02-03 03:48:12 +00:00
|
|
|
nice -n 10 gifsicle "${p}" $(seq -f "#%g" 0 2 "${frameamount}") -O3 --lossy=80 --colors=255 -o "${p}" #&> /dev/null
|
2025-01-28 03:00:57 +00:00
|
|
|
done
|
2025-02-05 14:59:57 +00:00
|
|
|
elif [[ "${p}" =~ .png ]]; then
|
2025-01-28 03:00:57 +00:00
|
|
|
nice -n 10 oxipng -o max "${p}" #&> /dev/null
|
2025-02-05 14:59:57 +00:00
|
|
|
elif [[ "${p}" =~ .webp ]]; then
|
2025-01-29 15:55:19 +00:00
|
|
|
#If file is not animated
|
2025-02-10 15:15:46 +00:00
|
|
|
if [[ -f "${p}" ]]; then
|
|
|
|
if grep -q -v -e "ANIM" -e "ANMF" "${p}"; then
|
2025-02-11 14:35:48 +00:00
|
|
|
tmppic="/tmp/temp_$(date +%s).webp"
|
|
|
|
nice -n 10 cwebp -mt -af -quiet "${p}" -o "${tmppic}" #&> /dev/null
|
|
|
|
if [[ -f "${tmppic}" ]]; then
|
2025-02-24 11:00:49 -06:00
|
|
|
size_new=$(stat -c%s "${tmppic}" 2>/dev/null || echo 0)
|
|
|
|
size_original=$(stat -c%s "${p}" 2>/dev/null || echo 0)
|
2025-02-10 15:15:46 +00:00
|
|
|
if [[ "${size_original}" -gt "${size_new}" ]]; then
|
2025-02-11 14:35:48 +00:00
|
|
|
mv "${tmppic}" "${p}" #&> /dev/null
|
2025-02-10 15:15:46 +00:00
|
|
|
else
|
2025-02-11 14:35:48 +00:00
|
|
|
rm "${tmppic}" #&> /dev/null
|
2025-02-10 15:15:46 +00:00
|
|
|
fi
|
2025-01-29 15:55:19 +00:00
|
|
|
fi
|
2025-01-28 03:00:57 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2025-01-26 04:31:05 +00:00
|
|
|
}
|
|
|
|
|
2025-01-24 19:34:48 +00:00
|
|
|
cd "${folder}" || exit
|
2025-02-05 14:59:57 +00:00
|
|
|
if [[ ! -f "${tmpfile}" ]]; then
|
2025-02-09 02:29:19 +00:00
|
|
|
sudo bin/console movetoavatarcache | sudo tee "${tmpfile}" #&> /dev/null
|
2025-01-24 19:16:21 +00:00
|
|
|
fi
|
2025-01-28 03:00:57 +00:00
|
|
|
grep -e "https://${site}/${avatarfolder}/" "${tmpfile}" | sed -e "s/.*${site}/${folderescaped}/g" -e "s/?ts=.*//g" | (
|
2025-02-05 14:59:57 +00:00
|
|
|
while read -r i; do
|
|
|
|
for p in "${i}" "${i//-320/-80}" "${i//-320/-48}"; do
|
2025-01-28 03:10:46 +00:00
|
|
|
loop_1 "${p}" &
|
2025-02-28 08:52:49 -06:00
|
|
|
if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) / 2)) ]]; then
|
2025-01-28 03:10:46 +00:00
|
|
|
wait -n
|
|
|
|
fi
|
|
|
|
done
|
2025-01-24 19:16:21 +00:00
|
|
|
done
|
|
|
|
)
|
|
|
|
wait
|
2025-01-24 19:34:48 +00:00
|
|
|
rm "${tmpfile}"
|
|
|
|
/usr/bin/find "${folder}"/avatar -type d -empty -delete
|
|
|
|
/usr/bin/chmod "${folderperm}" "${folder}"/avatar
|
|
|
|
/usr/bin/chown -R "${user}":"${group}" "${folder}"/avatar
|
2025-02-05 14:59:57 +00:00
|
|
|
/usr/bin/find "${folder}"/avatar -depth -not -user "${user}" -or -not -group "${group}" -print0 | xargs -0 -r chown -v "${user}":"${group}" #&> /dev/null
|
2025-01-28 03:00:57 +00:00
|
|
|
/usr/bin/find "${folder}"/avatar -depth -type d -and -not -type f -and -not -perm "${folderperm}" -print0 | xargs -0 -r chmod -v "${folderperm}" #&> /dev/null
|
2025-02-05 14:59:57 +00:00
|
|
|
/usr/bin/find "${folder}"/avatar -depth -type f -and -not -type d -and -not -perm "${fileperm}" -print0 | xargs -0 -r chmod -v "${fileperm}" #&> /dev/null
|