2021-08-24 07:32:55 -07:00
|
|
|
const dbConnection = require('./db');
|
|
|
|
require('dotenv').config({ path:__dirname + '/../.env' });
|
|
|
|
const awsConfig = require('./aws');
|
|
|
|
const S3 = require('aws-sdk/clients/s3');
|
|
|
|
|
|
|
|
const execute = process.env.EXECUTE === '1';
|
|
|
|
|
|
|
|
console.log(execute ? 'WILL REMOVE FILES!' : 'Dry run');
|
|
|
|
|
|
|
|
async function cleanup() {
|
|
|
|
console.log('--- Fetching ids expected to stay ---');
|
|
|
|
const db = await dbConnection();
|
|
|
|
|
2021-08-24 08:26:01 -07:00
|
|
|
const avatars = {}
|
|
|
|
for (let row of await db.all(`
|
2021-08-24 07:32:55 -07:00
|
|
|
SELECT avatarSource
|
|
|
|
FROM users
|
|
|
|
WHERE avatarSource LIKE 'https://pronouns-page.s3-eu-west-1.amazonaws.com/images/%'`
|
2021-08-24 08:26:01 -07:00
|
|
|
)) {
|
|
|
|
avatars[row.avatarSource.match('https://pronouns-page.s3-eu-west-1.amazonaws.com/images/(.*)-thumb.png')[1]] = true;
|
|
|
|
}
|
2021-08-24 07:32:55 -07:00
|
|
|
|
2021-08-24 08:26:01 -07:00
|
|
|
const flags = {};
|
2021-08-24 07:32:55 -07:00
|
|
|
for (let row of await db.all(`
|
|
|
|
SELECT customFlags
|
|
|
|
FROM profiles
|
|
|
|
WHERE customFlags != '{}'
|
|
|
|
`)) {
|
|
|
|
for (let key of Object.keys(JSON.parse(row.customFlags))) {
|
2021-08-24 08:26:01 -07:00
|
|
|
flags[key] = true;
|
2021-08-24 07:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 08:26:01 -07:00
|
|
|
const sources = {};
|
2021-08-24 07:32:55 -07:00
|
|
|
for (let row of await db.all(`
|
|
|
|
SELECT images
|
|
|
|
FROM sources
|
|
|
|
WHERE images is not null AND images != ''
|
|
|
|
`)) {
|
|
|
|
for (let key of row.images.split(',')) {
|
2021-08-24 08:26:01 -07:00
|
|
|
sources[key] = true;
|
2021-08-24 07:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 08:26:01 -07:00
|
|
|
const terms = {};
|
2021-08-24 07:32:55 -07:00
|
|
|
for (let row of await db.all(`
|
|
|
|
SELECT images
|
|
|
|
FROM terms
|
|
|
|
WHERE images is not null AND images != ''
|
|
|
|
`)) {
|
|
|
|
for (let key of row.images.split(',')) {
|
2021-08-24 08:26:01 -07:00
|
|
|
terms[key] = true;
|
2021-08-24 07:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 08:26:01 -07:00
|
|
|
console.log('Avatars: ' + Object.keys(avatars).length);
|
|
|
|
console.log('Flags: ' + Object.keys(flags).length);
|
|
|
|
console.log('Sources: ' + Object.keys(sources).length);
|
|
|
|
console.log('Terms: ' + Object.keys(terms).length);
|
2021-08-24 07:32:55 -07:00
|
|
|
|
|
|
|
await db.close();
|
|
|
|
|
|
|
|
console.log('--- Cleaning up S3 ---');
|
|
|
|
const s3 = new S3(awsConfig);
|
|
|
|
let overall = 0;
|
|
|
|
let fresh = 0;
|
|
|
|
let removed = 0;
|
|
|
|
let removedSize = 0;
|
|
|
|
|
|
|
|
const chunkSize = 1000;
|
|
|
|
let marker = undefined;
|
|
|
|
while (true) {
|
|
|
|
console.log('Making a request');
|
|
|
|
const objects = await s3.listObjects({
|
|
|
|
Prefix: `images/`,
|
|
|
|
MaxKeys: chunkSize,
|
|
|
|
Marker: marker,
|
|
|
|
}).promise();
|
|
|
|
|
|
|
|
const toRemove = [];
|
|
|
|
const remove = async (object, reason) => {
|
|
|
|
console.log(`REMOVING: ${object.Key} (${reason})`);
|
2021-08-24 08:26:01 -07:00
|
|
|
toRemove.push({Key: object.Key});
|
2021-08-24 07:32:55 -07:00
|
|
|
removed += 1;
|
|
|
|
removedSize += object.Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let object of objects.Contents) {
|
|
|
|
overall++;
|
|
|
|
marker = object.Key;
|
|
|
|
|
|
|
|
if (object.LastModified > new Date() - 60*60*1000) {
|
|
|
|
fresh++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [, id, size] = object.Key.match('images/(.*)-(.*).png');
|
|
|
|
|
2021-08-24 08:26:01 -07:00
|
|
|
if (avatars[id]) {
|
2021-08-24 07:32:55 -07:00
|
|
|
if (size !== 'thumb') {
|
|
|
|
await remove(object, 'avatar');
|
|
|
|
}
|
2021-08-24 08:26:01 -07:00
|
|
|
} else if (flags[id]) {
|
2021-08-24 07:32:55 -07:00
|
|
|
if (size !== 'flag') {
|
|
|
|
await remove(object, 'flag');
|
|
|
|
}
|
2021-08-24 08:26:01 -07:00
|
|
|
} else if (sources[id]) {
|
2021-08-24 07:32:55 -07:00
|
|
|
if (size !== 'big' && size !== 'thumb') {
|
|
|
|
await remove(object, 'source');
|
|
|
|
}
|
2021-08-24 08:26:01 -07:00
|
|
|
} else if (terms[id]) {
|
2021-08-24 07:32:55 -07:00
|
|
|
if (size !== 'big' && size !== 'thumb') {
|
|
|
|
await remove(object, 'term');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await remove(object, 'not used');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 07:33:53 -07:00
|
|
|
if (execute && toRemove.length) {
|
2021-08-24 07:32:55 -07:00
|
|
|
console.log('--- Removal request ---');
|
|
|
|
await s3.deleteObjects({
|
|
|
|
Delete: {
|
|
|
|
Objects: toRemove,
|
|
|
|
}
|
2021-08-26 00:42:37 -07:00
|
|
|
}).promise();
|
2021-08-24 07:32:55 -07:00
|
|
|
}
|
2021-08-24 08:30:40 -07:00
|
|
|
|
2021-08-24 07:32:55 -07:00
|
|
|
if (objects.Contents.length < chunkSize) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('--- Summary ---');
|
|
|
|
console.log('Overall: ' + overall);
|
|
|
|
console.log('Fresh: ' + fresh);
|
|
|
|
console.log('Removed: ' + removed);
|
|
|
|
console.log(`Removed size: ${Math.round(removedSize / 1024 / 1024)} MB`);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|