跳至正文

处理插件 ​

处理插件(kind: "processor")运行在处理链步骤中,可以在三个不同阶段介入图片处理。一个插件可以同时支持多个阶段,每个阶段各自声明配置和实现。

选择阶段 ​

阶段运行时机典型场景详细教程
upload保存到存储之前(同步)压缩尺寸、转格式、加水印、自动打标签上传处理
access访问公开图片且未命中缓存时(按需)动态缩略图、按需裁剪或固定水印,不改原图访问处理
post_save图片发布后的后台任务(异步)内容审核(屏蔽/删除)、Webhook 通知、异步替换保存后处理

stages 声明结构 ​

在 definePlugin({...}) 中设置 kind: "processor",把配置和实现放到 stages 对应的阶段里(至少声明一个阶段):

ts
export default definePlugin({
  id: "example.my-processor",
  version: "1.0.0",
  kind: "processor",
  defaultLocale: "en-US",
  messages: { "en-US": { "package.name": "My processor" } },
  stages: {
    upload: {
      configSchemaVersion: 1,
      configSchema: { /* ... */ },
      paramsSchema: { /* 可选:请求参数 */ },
      async validateConfig(config) { return []; },        // 可选:按阶段补充校验
      async migrateConfig({ from, config }) { return config; }, // 可选:按阶段升级迁移
      async createInstance({ config, services }) {
        return { async process(input, context) { /* ... */ } };
      },
    },
    access: {
      configSchemaVersion: 1,
      cacheable: true, // 可选,默认 true;设为 false 禁用派生缓存
      async createInstance({ config, services }) {
        return { async process(input, context) { /* ... */ } };
      },
    },
    post_save: {
      configSchemaVersion: 1, // 保存后阶段不支持 paramsSchema
      async createInstance({ config, services }) {
        return { async process(input, context) { /* ... */ } };
      },
    },
  },
});

各阶段共用顶层的 defaultLocale 和 messages,不需要在阶段内重复声明字典。配置与宿主能力见配置、密钥与宿主能力。