From 84e37d122036094a4fb03d030b1e103d4797c353 Mon Sep 17 00:00:00 2001 From: Akatsuki-Misaki Date: Sat, 2 Dec 2023 21:48:10 +0800 Subject: [PATCH] update --- README.md | 6 +- logo.png | Bin 0 -> 2776 bytes package-lock.json | 101 +++++++++++++++++++++++++++++++++ package.json | 29 ++++++++++ src/index.js | 141 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 logo.png create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.js diff --git a/README.md b/README.md index b2eadac..2af5635 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # picgo-plugin-bohe-uploader -PicGo plugin for Bohe uploader \ No newline at end of file +PicGo plugin for Bohe uploader + +## Install + +选中本项目文件夹,在 PicGo 插件管理中选择导入本地插件 \ No newline at end of file diff --git a/logo.png b/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..d855ab07d8a120a0843d03fb1e9fa0646256a0bd GIT binary patch literal 2776 zcmV;}3Mci6P)S{c+7L)eEXz=|X>lJ>QHDOCl4HlI%;76Zii!dFEk>s$8z0D1Z^; zMNvc~5&;8n`~6)j0kqC)0Djv0!5u&9+q;b+#1&G-N>Bh~;vl7z!teJ308~)4M}6*D znF0sm1MY|-+W!&neXLhbZl-x2EFO!&GpfdSF%n@LK4kYhQBU zalazmN+@B{RLT3hs-oa@cQ;g39Zd@YppLi3TCcgM`n*knGnp8h(q!xocW!h0Iz!5uLf{@#v@=yU+d=f zOz2seYHTTs3rZ=xzWkzEp%h6eyLQG?;P>87F6zuiUy?y}N7NGDAypep<9mZ}7?LFA zS=CV_6)`fS0DK%B;64;QFA7w#=+!ZYt&nbWv?vL6tf7)5AruM~q-(CZs&7UCcqMvj zTR;_8>ixc*;iMhz)uGebn5cs0R(0_Ce4vyTRN2q9*b9mP-68wC{zFcYs+YendVf5@ zZ8SaqQGjO*%EJw7*?ObZc`WnaMDEv<$s`0pD2y*a(H0rl0pOXk1JEXiU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eP$ zmTF}wU-N(gL>t%h>)h*2-LVl|RoDFT&Xw1%ziIx;P$_lmUXTB<@$$bnakg<=*ibkO zS(Z!YdmGD1H#D|9`1lt#-_mS%NhPL~;y7x5w9oZS=xCpyDLgRrLPK|VchMaqKw)H9 zEVI|2_)=Z-ZQohEwhd*HqF$1^q{0HWb?mxp@w%65%vKRI zAVmStBgVJV9O5G&U<$D1h(sbV8;pso>YKmu#ARFF1b{YwpCu+| z+#N9=#~IifQI*+hj^|f6=7tE+wV=wCFteP7Pf>J7z|)>XH^ia1B}qaw8U;%jZLM?O z?IXqmz!&S9(g3<}9*gd4@Qf<_0F)vS2!K+G71i!vKE3*;*D=dP7XZ)ujxG= 0.8" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.3", + "resolved": "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-1.15.3.tgz", + "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmmirror.com/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmmirror.com/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmmirror.com/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmmirror.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..8a864bc --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "picgo-plugin-bohe-uploader", + "version": "0.0.1", + "description": "PicGo plugin for Bohe uploader", + "main": "src/index.js", + "publishConfig": { + "access": "public" + }, + "homepage": "https://lolicon.team", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "patch": "npm version patch && git push origin master && git push origin --tags", + "minor": "npm version minor && git push origin master && git push origin --tags", + "major": "npm version major && git push origin master && git push origin --tags" + }, + "keywords": [ + "picgo", + "picgo-gui-plugin", + "picgo-plugin", + "bohe", + "bohe-uploader" + ], + "author": "紗夕里", + "license": "MIT", + "dependencies": { + "axios": "^1.6.2", + "form-data": "^4.0.0" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a8db230 --- /dev/null +++ b/src/index.js @@ -0,0 +1,141 @@ +const axios = require('axios'); +const FormData = require('form-data'); + +module.exports = function (ctx) { + const register = () => { + ctx.helper.uploader.register('bohe-uploader', { + handle, + name: '薄荷图床', + config: config + }); + }; + + const handle = async function (ctx) { + const userConfig = ctx.getConfig('picBed.bohe-uploader'); + if (!userConfig) { + throw new Error('Can\'t find uploader config'); + } + + const apiEndpoint = userConfig.apiEndpoint || ''; + const uploadPath = userConfig.uploadPath || ''; + const apiToken = userConfig.apiToken || ''; + const enableCompression = userConfig.enableCompression || false; + const compressionLevel = enableCompression ? userConfig.compressionLevel || 80 : undefined; + + const imgList = ctx.output; + for (const img of imgList) { + try { + const imageUrl = await uploadToBohe(img, apiEndpoint, uploadPath, apiToken, enableCompression, compressionLevel); + img.imgUrl = imageUrl; + + // 判断上传是否成功 + if (imageUrl) { + ctx.emit('notification', { + title: '上传成功', + body: '图片上传成功' + }); + } else { + ctx.emit('notification', { + title: '上传失败', + body: '薄荷图床返回的URL为空,上传可能未成功' + }); + } + } catch (err) { + ctx.emit('notification', { + title: '上传失败', + body: JSON.stringify(err) + }); + } + } + }; + + const uploadToBohe = async (img, apiEndpoint, uploadPath, apiToken, enableCompression, compressionLevel) => { + const formData = new FormData(); + const modes = enableCompression ? '1' : '0'; + formData.append('uploadedFile', img.buffer, { filename: img.fileName }); + formData.append('api_token', apiToken); + formData.append('upload_format', 'file'); + formData.append('uploadPath', uploadPath); + if (enableCompression === true) { + formData.append('mode', modes); + formData.append('image_compress', '1'); + formData.append('image_compress_level', compressionLevel.toString()); + } + + try { + // 记录上传请求 debug启用 + // ctx.log.info('上传请求 URL:', apiEndpoint); + // ctx.log.info('mode', modes); + // ctx.log.info('薄荷图床上传请求 JSON:', formData); + + const response = await axios.post(apiEndpoint, formData, { + headers: { + ...formData.getHeaders(), + } + }); + + + // 记录上传响应 + // ctx.log.info('薄荷图床上传响应 JSON:', response.data); + + if (response.data.status === 'success' && response.data.url) { + // 根据具体需求,可能还需要处理其他返回信息 + return response.data.url; + } else { + throw new Error('薄荷图床上传失败: ' + JSON.stringify(response.data)); + } + } catch (err) { + throw new Error('薄荷图床上传失败: ' + JSON.stringify(err)); + } + }; + + const config = ctx => { + return [ + { + name: 'apiToken', + type: 'input', + default: '', + required: true, + message: 'API Token', + alias: 'API Token' + }, + { + name: 'apiEndpoint', + type: 'input', + default: 'https://example.com//api/upload/', + required: true, + message: 'API 接口地址', + alias: 'API 接口地址' + }, + { + name: 'uploadPath', + type: 'input', + default: '', + required: true, + message: '文件位置', + alias: '文件位置' + }, + { + name: 'enableCompression', + type: 'confirm', + default: true, + message: '是否压缩', + alias: '是否压缩' + }, + { + name: 'compressionLevel', + type: 'input', + default: 80, + required: true, + message: '压缩比例', + alias: '压缩比例' + }, + // 添加所需的配置选项 + ]; + }; + + return { + uploader: 'bohe-uploader', + register + }; +};