This repository has been archived on 2023-08-03. You can view files and clone it, but cannot push or open issues or pull requests.
scripts/prod/cleanup-media.zsh

23 lines
1.5 KiB
Bash
Executable file

#!/bin/zsh
date=$(date -d "-30 days" "+%F %T.000")
# Get all files so we can delete them later
rows=$(sudo -iu postgres psql -d e2net -c "SELECT \"url\", \"thumbnailUrl\" FROM \"public\".\"drive_file\" WHERE \"isLink\" = 'FALSE' AND \"storedInternal\" = 'FALSE' AND \"userHost\" IS NOT NULL AND \"userHost\" <> 'estrogen.network' AND \"createdAt\" < '$date' AND NOT EXISTS (SELECT 1 FROM \"user\" WHERE \"user\".\"avatarId\" = \"drive_file\".\"id\") AND NOT EXISTS (SELECT 1 FROM \"user\" WHERE \"user\".\"bannerId\" = \"drive_file\".\"id\")" -t -A --csv)
files=
for row (${(f)rows}) do
parts=("${(@s:,:)row}")
files+="${parts[1]}\n"
if [[ -n ${parts[2]} ]]; then
files+="${parts[2]}\n"
fi
done
# Replace file URLs with the remote URI
cleanup_query="UPDATE \"public\".\"drive_file\" SET \"isLink\" = 'TRUE', \"thumbnailUrl\" = NULL, \"url\" = \"uri\", \"accessKey\" = replace(\"accessKey\", 'drive/', ''), \"thumbnailAccessKey\" = replace(\"thumbnailAccessKey\", 'drive/', '') WHERE \"isLink\" = 'FALSE' AND \"storedInternal\" = 'FALSE' AND \"userHost\" IS NOT NULL AND \"userHost\" <> 'estrogen.network' AND \"createdAt\" < '$date' AND NOT EXISTS (SELECT 1 FROM \"user\" WHERE \"user\".\"avatarId\" = \"drive_file\".\"id\") AND NOT EXISTS (SELECT 1 FROM \"user\" WHERE \"user\".\"bannerId\" = \"drive_file\".\"id\")"
echo "$cleanup_query" | sudo -iu postgres psql -d e2net
# Cleanup object storage
echo "$files" | awk NF | sed 's/https:\/\/e2net.cdn.ztn.sh/garage\/e2net/g' | grep -P '\/e2net\/drive\/.{16,}' | mcli rm --stdin --force