unattached-media: delete orphaned attachments after 15 minutes
This commit is contained in:
parent
46b9deffce
commit
0b867afd8e
26
src/cron.ts
26
src/cron.ts
|
@ -1,6 +1,9 @@
|
||||||
import * as eventsDB from '@/db/events.ts';
|
import * as eventsDB from '@/db/events.ts';
|
||||||
|
import { deleteUnattachedMediaByUrl, getUnattachedMedia } from '@/db/unattached-media.ts';
|
||||||
import { cron } from '@/deps.ts';
|
import { cron } from '@/deps.ts';
|
||||||
import { Time } from '@/utils/time.ts';
|
import { Time } from '@/utils/time.ts';
|
||||||
|
import { configUploader as uploader } from '@/uploaders/config.ts';
|
||||||
|
import { cidFromUrl } from '@/utils/ipfs.ts';
|
||||||
|
|
||||||
/** Clean up old remote events. */
|
/** Clean up old remote events. */
|
||||||
async function cleanupEvents() {
|
async function cleanupEvents() {
|
||||||
|
@ -14,6 +17,29 @@ async function cleanupEvents() {
|
||||||
console.log(`Cleaned up ${result?.numDeletedRows ?? 0} old remote events.`);
|
console.log(`Cleaned up ${result?.numDeletedRows ?? 0} old remote events.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Delete files that aren't attached to any events. */
|
||||||
|
async function cleanupMedia() {
|
||||||
|
console.log('Deleting orphaned media files...');
|
||||||
|
|
||||||
|
const until = new Date(Date.now() - Time.minutes(15));
|
||||||
|
const media = await getUnattachedMedia(until);
|
||||||
|
|
||||||
|
for (const { url } of media) {
|
||||||
|
const cid = cidFromUrl(new URL(url))!;
|
||||||
|
try {
|
||||||
|
await uploader.delete(cid);
|
||||||
|
await deleteUnattachedMediaByUrl(url);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Failed to delete file ${url}`);
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Removed ${media?.length ?? 0} orphaned media files.`);
|
||||||
|
}
|
||||||
|
|
||||||
await cleanupEvents();
|
await cleanupEvents();
|
||||||
|
await cleanupMedia();
|
||||||
|
|
||||||
cron.every15Minute(cleanupEvents);
|
cron.every15Minute(cleanupEvents);
|
||||||
|
cron.every15Minute(cleanupMedia);
|
||||||
|
|
|
@ -27,4 +27,25 @@ function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'uploaded_at'
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
export { insertUnattachedMedia };
|
/** Find attachments that exist but aren't attached to any events. */
|
||||||
|
function getUnattachedMedia(until: Date) {
|
||||||
|
return db.selectFrom('unattached_media')
|
||||||
|
.select([
|
||||||
|
'unattached_media.id',
|
||||||
|
'unattached_media.pukey',
|
||||||
|
'unattached_media.url',
|
||||||
|
'unattached_media.data',
|
||||||
|
'unattached_media.uploaded_at',
|
||||||
|
])
|
||||||
|
.leftJoin('tags', 'unattached_media.url', 'tags.value')
|
||||||
|
.where('uploaded_at', '<', until)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteUnattachedMediaByUrl(url: string) {
|
||||||
|
return db.deleteFrom('unattached_media')
|
||||||
|
.where('url', '=', url)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export { deleteUnattachedMediaByUrl, getUnattachedMedia, insertUnattachedMedia };
|
||||||
|
|
|
@ -24,4 +24,4 @@ function cidFromUrl({ protocol, hostname, pathname }: URL) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { cidFromUrl };
|
export { cidFromUrl };
|
||||||
|
|
Loading…
Reference in New Issue