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 0000000..d855ab0 Binary files /dev/null and b/logo.png differ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..5b986f4 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,101 @@ +{ + "name": "picgo-plugin-bohe-uploader", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "picgo-plugin-bohe-uploader", + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "axios": "^1.6.2", + "form-data": "^4.0.0" + } + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmmirror.com/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.6.2", + "resolved": "https://registry.npmmirror.com/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 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 + }; +};