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
|
|
|
|
group=friendica
|
|
|
|
fileperm=644
|
|
|
|
folderperm=755
|
|
|
|
folder=/var/www/friendica
|
|
|
|
folderescaped=${folder////\\/}
|
|
|
|
tmpfile=/tmp/friendica-fix-avatar-permissions.txt
|
|
|
|
avatarfolder=avatar
|
2025-01-26 04:31:05 +00:00
|
|
|
|
|
|
|
loop_1(){
|
2025-01-28 03:00:57 +00:00
|
|
|
if [[ "${p}" =~ .jpeg || "${p}" =~ .jpg ]]
|
|
|
|
then
|
|
|
|
nice -n 10 jpegoptim -m 76 "${p}" #&> /dev/null
|
|
|
|
elif [[ "${p}" =~ .gif ]]
|
|
|
|
then
|
|
|
|
nice -n 10 gifsicle --batch -O3 --lossy=80 --colors=255 "${p}" #&> /dev/null
|
|
|
|
#Specific compression for large GIF files
|
2025-01-29 15:06:50 +00:00
|
|
|
while [[ $(stat -c%s "${p}" || 0) -ge 512000 ]]
|
2025-01-28 03:00:57 +00:00
|
|
|
do
|
|
|
|
nice -n 10 gifsicle "${p}" $(seq -f "#%g" 0 2 99) -O3 --lossy=80 --colors=255 -o "${p}" #&> /dev/null
|
|
|
|
done
|
|
|
|
elif [[ "${p}" =~ .png ]]
|
|
|
|
then
|
|
|
|
nice -n 10 oxipng -o max "${p}" #&> /dev/null
|
|
|
|
elif [[ "${p}" =~ .webp ]]
|
|
|
|
then
|
|
|
|
nice -n 10 cwebp -mt -af -quiet "${p}" -o /tmp/temp.webp #&> /dev/null
|
|
|
|
if [[ -f /tmp/temp.webp ]]
|
|
|
|
then
|
2025-01-29 15:06:50 +00:00
|
|
|
size_new=$(stat -c%s "/tmp/temp.webp" || 0 )
|
2025-01-28 03:00:57 +00:00
|
|
|
size_original=$(stat -c%s "${p}")
|
|
|
|
if [[ "${size_original}" -gt "${size_new}" ]]
|
|
|
|
then
|
2025-01-29 15:06:50 +00:00
|
|
|
mv /tmp/temp.webp "${p}" #&> /dev/null
|
2025-01-28 03:00:57 +00:00
|
|
|
else
|
2025-01-29 15:06:50 +00:00
|
|
|
rm /tmp/temp.webp #&> /dev/null
|
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
|
|
|
|
if [[ ! -f "${tmpfile}" ]]
|
2025-01-24 19:16:21 +00:00
|
|
|
then
|
2025-01-24 19:34:48 +00:00
|
|
|
sudo -u "${user}" 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-01-28 03:10:46 +00:00
|
|
|
while read -r i
|
2025-01-24 19:16:21 +00:00
|
|
|
do
|
2025-01-28 03:10:46 +00:00
|
|
|
for p in "${i}" "${i//-320/-80}" "${i//-320/-48}"
|
|
|
|
do
|
|
|
|
loop_1 "${p}" &
|
|
|
|
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]
|
|
|
|
then
|
|
|
|
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-01-28 03:00: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
|
|
|
|
/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
|
|
|
|
/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
|