update<基于官方模板重写>

main
赤月未咲 11 months ago
parent 202325f9ed
commit f14487c97b

@ -1,9 +0,0 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -4,10 +4,21 @@ PicGo plugin for Bohe uploader
## Install
进入`bohe-uploader`文件夹
安装依赖库
```bash
npm install
```
在 PicGo 插件管理中选择导入本地插件
选中本项目文件夹
选中项目的`bohe-uploader`文件夹
## Question
- 为什么需要安装依赖库
根据开发文档所示,本地开发需要导入依赖库
- 为什么云端下载时不需要npm install
不知道,目前本插件未发布到插件列表中

@ -0,0 +1,3 @@
{
"extends": "standard"
}

@ -0,0 +1,6 @@
node_modules/
dist/
.DS_Store
yarn-error.log
temp.js
package-lock.json

@ -0,0 +1,9 @@
node_modules/
yarn-error.log
temp.js
package-lock.json
tsconfig.json
tslint.json
.vscode/
src/
.travis.yml

@ -0,0 +1,31 @@
language: node_js
node_js: stable
# Travis-CI Caching
cache:
directories:
- node_modules
yarn: true
# S: Build Lifecycle
install:
- yarn
stages:
- name: deploy
jobs:
include:
- stage: deploy
script:
- npm run build
deploy:
provider: npm
email: ""
api_key: "${NPM_TOKEN}"
skip_cleanup: true
on:
branch: master
branches:
only:
- master

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 紗夕里
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -0,0 +1,3 @@
## picgo-plugin-bohe-uploader
PicGo plugin for Bohe uploader

@ -0,0 +1,132 @@
# ---> Node
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

@ -0,0 +1,30 @@
{
"name": "picgo-plugin-bohe-uploader",
"version": "1.0.0",
"description": "PicGo plugin for Bohe uploader",
"main": "src/index.js",
"publishConfig": {
"access": "public"
},
"homepage": "",
"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"
],
"author": "紗夕里",
"license": "MIT",
"devDependencies": {
"eslint": "^8.25.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.3.0",
"eslint-plugin-promise": "^6.1.0"
}
}

@ -0,0 +1,138 @@
const fs = require('fs');
const path = require('path');
module.exports = (ctx) => {
const register = () => {
ctx.helper.uploader.register('bohe-uploader', {
handle,
name: '薄荷图床',
config: config
});
};
const handle = async function (ctx) {
let userConfig = ctx.getConfig('picBed.bohe-uploader');
if (!userConfig) {
throw new Error('Can\'t find uploader config');
}
const url = userConfig.url || '';
const uploadPath = userConfig.uploadPath || '';
const apiToken = userConfig.apiToken || '';
const enableCompression = userConfig.enableCompression || false;
const compressionLevel = enableCompression ? userConfig.compressionLevel || 80 : undefined;
try {
let imgList = ctx.output;
for (let i in imgList) {
let image = imgList[i].buffer;
if (!image && imgList[i].base64Image) {
image = Buffer.from(imgList[i].base64Image, 'base64');
}
const postConfig = postOptions(image, url, uploadPath, apiToken, enableCompression, compressionLevel, imgList[i].fileName);
// ctx.log.info('发送的JSON', JSON.stringify(postConfig.formData));
let body = await ctx.Request.request(postConfig);
ctx.log.info('返回的JSON', body);
delete imgList[i].base64Image;
delete imgList[i].buffer;
// 修改这里,确保 body 是一个对象
body = typeof body === 'string' ? JSON.parse(body) : body;
// 修改这里,将 imgUrl 设置为正确的值
imgList[i]['imgUrl'] = body.url || body.data.url;
}
} catch (err) {
ctx.log.error('上传失败:', err);
ctx.emit('notification', {
title: '上传失败',
body: JSON.stringify(err)
});
}
};
const postOptions = (image, url, uploadPath, apiToken, enableCompression, compressionLevel, fileName) => {
let headers = {
'Content-Type': 'multipart/form-data',
'User-Agent': 'PicGo'
};
const formData = {
uploadedFile: {
value: image,
options: {
filename: fileName
}
},
api_token: apiToken,
upload_format: 'file',
uploadPath: uploadPath
};
if (enableCompression === true) {
formData.image_compress = '1';
formData.mode = enableCompression ? '1' : '0';
formData.image_compress_level = compressionLevel.toString();
}
return {
method: 'POST',
url: url,
headers: headers,
formData: formData
};
};
const config = ctx => {
let userConfig = ctx.getConfig('picBed.bohe-uploader') || {};
return [
{
name: 'url',
type: 'input',
default: userConfig.url,
required: true,
message: 'API地址',
alias: 'API地址'
},
{
name: 'uploadPath',
type: 'input',
default: userConfig.uploadPath,
required: true,
message: '上传路径',
alias: '上传路径'
},
{
name: 'apiToken',
type: 'input',
default: userConfig.apiToken,
required: true,
message: 'API Token',
alias: 'API Token'
},
{
name: 'enableCompression',
type: 'confirm',
default: userConfig.enableCompression || false,
message: '是否启用压缩',
alias: '是否启用压缩'
},
{
name: 'compressionLevel',
type: 'input',
default: userConfig.compressionLevel,
required: false,
message: '压缩比例 (0-100)',
alias: '压缩比例'
}
];
};
return {
uploader: 'bohe-uploader',
register
};
};

@ -0,0 +1,132 @@
# ---> Node
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

2845
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,29 +1,5 @@
{
"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"
}
"dependencies": {
"picgo": "^1.5.6"
}
}

@ -1,141 +0,0 @@
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
};
};
Loading…
Cancel
Save