picgo-plugin-imageX火山引擎ImageX插件for picGo

picGo 火山引擎imageX插件

imageX支持图片模板操作,设置模板可以给图片添加水印和压缩等操作;见下图,增加了水印并压缩了图片
在这里插入图片描述

插件依赖了'@volcengine/openapi
源码地址:gitee

const { imagex } = require('@volcengine/openapi')
const imagexService = new imagex.ImagexService()

const config = (ctx) => {
  let userConfig = ctx.getConfig('picBed.imageX-uploader')
  if (!userConfig) {
    userConfig = {}
  }
  const config = [
    {
      name: 'accessKey',
      type: 'input',
      default: userConfig.accessKey || '',
      message: 'AccessKey不能为空',
      required: true
    },
    {
      name: 'secretKey',
      type: 'input',
      default: userConfig.secretKey || '',
      message: 'SecretKey不能为空',
      required: true
    },
    {
      name: 'serviceId',
      type: 'input',
      alias: 'serviceId',
      default: userConfig.serviceId || '',
      message: '服务Id',
      required: true
    },
    {
      name: 'region',
      type: 'input',
      alias: '地区',
      default: userConfig.region || 'cn-north-1',
      message: '例如:cn-north-1 (默认)',
      required: true
    },
    {
      name: 'host',
      type: 'input',
      alias: '地区host',
      default: userConfig.host || 'imagex.volcengineapi.com',
      message: '例如:imagex.volcengineapi.com (默认)',
      required: true
    },
    {
      name: 'imgTemp',
      type: 'input',
      alias: '图片模板',
      default: userConfig.imgTemp || '',
      message: '例如:tplv-jafkehkiym-1.png, 可以不配置',
      required: false
    },
    {
      name: 'customUrl',
      type: 'input',
      alias: '自定义域名',
      default: userConfig.customUrl || '',
      message: '例如:http://bucket.xxx.com',
      required: true
    }
  ]
  return config
}

/**
 * @description: 设置aksk
 * @param {string} AccessKeyId
 * @param {string} SecretKey
 * @return {*}
 */
const setAkSk = (ctx, AccessKeyId, SecretKey, region, host) => {
  // 设置aksk
  imagexService.setAccessKeyId(AccessKeyId)
  imagexService.setSecretKey(SecretKey)
  // 设置地域
  imagexService.setRegion(region)
  imagexService.setHost(host)
}

const uploadImage = async (ctx, serviceId, img) => {
  const options = {
    serviceId: serviceId,
    files: [img]
  }
  ctx.log.info('上传')
  const res = await imagexService.UploadImages(options)
  ctx.log.info(res)
  return res
}

const handle = async (ctx) => {
  const userConfig = ctx.getConfig('picBed.imageX-uploader')
  if (!userConfig) {
    throw new Error('未配置参数,请配置imageX上传参数')
  }
  const accessKey = userConfig.accessKey
  const secretKey = userConfig.secretKey
  const serviceId = userConfig.serviceId
  const region = userConfig.region
  const host = userConfig.host
  const imgTemp = userConfig.imgTemp
  ctx.log.warn('开始上传')
  // 设置配置信息
  setAkSk(ctx, accessKey, secretKey, region, host)
  const customUrl = userConfig.customUrl
  try {
    // 上次图片
    const imgList = ctx.output
    for (let i in imgList) {
      let img = imgList[i].buffer
      if (!img && imgList[i].base64Image) {
        img = Buffer.from(imgList[i].base64Image, 'base64')
      }
      const res = await uploadImage(ctx, serviceId, img)
      if (res.Result && res.Result.Results) {
        let url = res.Result.PluginResult[0].ImageUri
        if (imgTemp) {
          url += `~${imgTemp}`
        }
        ctx.log.info(url)
        delete imgList[i].base64Image
        delete imgList[i].buffer
        imgList[i]['imgUrl'] = `${customUrl}/${url}`
      } else {
        throw new Error('Upload failed')
      }
    }
    return ctx
  } catch (err) {
    if (err.error === 'Upload failed') {
      ctx.emit('notification', {
        title: '上传失败!',
        body: '请检查你的配置项是否正确'
      })
    } else {
      ctx.emit('notification', {
        title: '上传失败!',
        body: '请检查你的配置项是否正确'
      })
    }
    throw err
  }
}

module.exports = (ctx) => {
  const register = () => {
    ctx.log.success('imageX加载成功')
    ctx.helper.uploader.register('imageX-uploader', {
      handle: handle,
      config: config,
      name: 'imageX'
    })
  }
  return {
    register,
    uploader: 'imageX-uploader'
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值