UNPKG

picgo-plugin-testnpm-telegraph

Version:
79 lines (72 loc) 1.83 kB
module.exports = (ctx) => { const register = () => { ctx.helper.uploader.register('cloudflare-telegraph', { handle, name: 'cloudflare-telegraph', config: config }) } const handle = async function (ctx) { let userConfig = ctx.getConfig('picBed.cloudflare-telegraph') if (!userConfig.url) { throw new Error('请配置上传地址'); } const url = userConfig.url+"/upload" try { for (let i in ctx.output) { const image = ctx.output[i].buffer || Buffer.from(ctx.output[i].base64Image, 'base64'); const fileName = ctx.output[i].fileName; const postConfig = postOptions(image, url, fileName) let body = await ctx.Request.request(postConfig) delete ctx.output[i].buffer if (body) { ctx.output[i]['imgUrl'] = userConfig.url + JSON.parse(body)[0].src } } } catch (err) { ctx.emit('notification', { title: '上传失败', body: JSON.stringify(err) }) } } // 配置上传请求参数 const postOptions = (image, url, fileName) => { return { method: 'POST', url: url, headers: { contentType: 'multipart/form-data', 'User-Agent': 'PicGo' }, formData: { file: { value: image, options: { filename: fileName } } } } } const config = ctx => { let userConfig = ctx.getConfig('picBed.cloudflare-telegraph') if (!userConfig) { userConfig = {} } return [ { name: 'url', type: 'input', default: userConfig.url, required: true, message: 'https://xxx.pages.dev', alias: 'API地址' } ] } return { uploader: 'cloudflare-telegraph', register } }