directus-extension-generate.../src/hook/index.ts

89 lines
2.7 KiB
TypeScript
Raw Normal View History

import { defineHook } from '@directus/extensions-sdk';
import { runBackfill } from '../lib/backfill';
import { recordOp } from '../lib/diagnostics';
import { buildRegistry, isImageType, loadCustomPresets } from '../lib/presets';
import { readSettings } from '../lib/settings';
import { readRun } from '../lib/state';
import { warmFile } from '../lib/warmer';
import { isInWindow } from '../lib/window';
interface UploadMeta {
key?: string;
collection?: string;
payload?: { type?: string | null; collection?: string };
}
export default defineHook(({ action, schedule }, { services, database, getSchema, env, logger }) => {
action('files.upload', (meta) => {
void warmUpload(meta as UploadMeta).catch((error) =>
logger.error(error, 'image-previews: upload warm failed'),
);
});
schedule('*/15 * * * *', () => {
void scheduledBackfill().catch((error) =>
logger.error(error, 'image-previews: scheduled run failed'),
);
});
async function warmUpload(meta: UploadMeta): Promise<void> {
const settings = await readSettings(env);
if (!settings.enabled || !settings.onUpload) return;
const fileId = meta?.key;
const payload = meta?.payload;
if (!fileId || !payload) return;
if (payload.collection && payload.collection !== 'directus_files') return;
if (!isImageType(payload.type)) return;
const schema = await getSchema();
const custom = await loadCustomPresets(new services['SettingsService']({ schema }));
const presets = buildRegistry(custom, settings.presetKeys);
if (presets.length === 0) return;
const assets = new services['AssetsService']({ schema });
const summary = await warmFile(assets, fileId, presets, {
concurrency: settings.concurrency,
formats: settings.formats,
onResult: (result) =>
recordOp({
ts: Date.now(),
fileId,
key: result.key,
format: result.format,
ok: result.ok,
ms: result.ms,
error: result.error,
}),
});
if (settings.debug) logger.info({ fileId, summary }, 'image-previews: warmed upload');
}
async function scheduledBackfill(): Promise<void> {
const settings = await readSettings(env);
if (!settings.enabled) return;
if (!isInWindow(settings.windowStart, settings.windowEnd)) return;
const run = await readRun(env);
if (run?.running) return;
const finishedToday = run?.finishedAt
? new Date(run.finishedAt).toDateString() === new Date().toDateString()
: false;
if (finishedToday && run && run.done >= run.total) return;
logger.info('image-previews: scheduled backfill starting');
void runBackfill({ services, database, getSchema, env, logger }, {}).catch((error) =>
logger.error(error, 'image-previews: scheduled backfill failed'),
);
}
});