Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace afterUpload, so that the url returns corresponds the url… #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
yarn-error.log
temp.js
package-lock.json
.eslintrc
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "picgo-plugin-super-prefix",
"version": "1.2.2",
"version": "1.2.3",
"description": "A PicGo plugin for elegant file name prefix",
"main": "dist/index.js",
"publishConfig": {
Expand All @@ -23,17 +23,19 @@
"author": "gclove",
"license": "MIT",
"devDependencies": {
"@types/node": "^10.10.1",
"eslint": "^5.0.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"@types/node": "16.9.1",
"@typescript-eslint/eslint-plugin": "^5.40.0",
"@typescript-eslint/parser": "^5.40.0",
"eslint-config-standard-with-typescript": "^23.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-standard": "^3.1.0",
"picgo": "^1.2.0",
"tslint": "^5.10.0",
"tslint-config-standard": "^7.1.0",
"typescript": "^3.0.3"
"picgo": "^1.5.0-alpha.13",
"typescript": "^4.8.4",
"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"
},
"dependencies": {
"dayjs": "^1.8.17"
Expand Down
138 changes: 94 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,95 @@
import picgo from 'picgo'
import { PicGo } from 'picgo'
import dayjs from 'dayjs'

function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time))
}

async function beforeTransformHandle (ctx) {
const autoRename = ctx.getConfig('settings.autoRename');
if (autoRename) {
ctx.emit('notification', {
title: '❌ 警告',
body: '请关闭 PicGo 的 ​``【oaicite:0】``​ 功能,\nsuper-prefix 插件重命名方式会被覆盖',
});
await sleep(10000);
throw new Error('super-prefix conflict');
}

let userConfig = ctx.getConfig('picgo-plugin-super-prefix');
if (!userConfig) {
userConfig = {
prefix: '',
fileFormat: '',
};
}

for (let i = 0; i < ctx.output.length; i++) {
let fileName = ctx.output[i].fileName;
let prefix = '';
if (userConfig.prefixFormat !== undefined && userConfig.prefixFormat !== '') {
prefix = dayjs().format(userConfig.prefixFormat);
}

if (userConfig.fileFormat !== undefined && userConfig.fileFormat !== '') {
if (i > 0) {
fileName = prefix + dayjs().format(userConfig.fileFormat) + '-' + i + ctx.output[i].extname;
} else {
fileName = prefix + dayjs().format(userConfig.fileFormat) + ctx.output[i].extname;
}
} else {
fileName = prefix + fileName;
}

ctx.output[i].fileName = fileName;
}
}

async function afterUploadHandle (ctx) {
const autoRename = ctx.getConfig('settings.autoRename');
if (autoRename) {
ctx.emit('notification', {
title: '❌ 警告',
body: '请关闭 PicGo 的 &#8203;``【oaicite:0】``&#8203; 功能,\nsuper-prefix 插件重命名方式会被覆盖',
});
await sleep(10000);
throw new Error('super-prefix conflict');
}

let userConfig = ctx.getConfig('picgo-plugin-super-prefix');
if (!userConfig) {
userConfig = {
prefix: '',
fileFormat: '',
};
}

// prefix + fileformat + extname
for (let i = 0; i < ctx.output.length; i++) {
let url = ctx.output[i].imgUrl;
let fileName = '';
if (userConfig.prefixFormat !== undefined && userConfig.prefixFormat !== '') {
fileName = dayjs().format(userConfig.prefixFormat);
}

if (userConfig.fileFormat !== undefined && userConfig.fileFormat !== '') {
if (i > 0) {
fileName = fileName + dayjs().format(userConfig.fileFormat) + '-' + i + ctx.output[i].extname;
} else {
fileName = fileName + dayjs().format(userConfig.fileFormat) + ctx.output[i].extname;
}
}

let tmpArray = url.split("/");
if (tmpArray.length > 0) {
tmpArray[tmpArray.length - 1] = fileName;
}
url = tmpArray.join("/");

ctx.output[i].imgUrl = url;
}
}

const pluginConfig = ctx => {
let userConfig = ctx.getConfig('picgo-plugin-super-prefix')
if (!userConfig) {
Expand All @@ -30,49 +115,14 @@ const pluginConfig = ctx => {
]
}

export = (ctx: picgo) => {
const register = () => {
ctx.helper.beforeUploadPlugins.register('super-prefix', {
async handle (ctx) {
// console.log(ctx)
const autoRename = ctx.getConfig('settings.autoRename')
if (autoRename) {
ctx.emit('notification', {
title: '❌ 警告',
body: '请关闭 PicGo 的 【时间戳重命名】 功能,\nsuper-prefix 插件重命名方式会被覆盖'
})
await sleep(10000)
throw new Error('super-prefix conflict')
}

let userConfig = ctx.getConfig('picgo-plugin-super-prefix')
if (!userConfig) {
userConfig = {
prefix: '',
fileFormat: ''
}
}

for (let i = 0; i < ctx.output.length; i++) {
let fileName = ctx.output[i].fileName
let prefix = ''
if (userConfig.prefixFormat != undefined && userConfig.prefixFormat != '') {
prefix = dayjs().format(userConfig.prefixFormat)
}

if (userConfig.fileFormat != undefined && userConfig.fileFormat != '') {
if (i > 0) {
fileName = prefix + dayjs().format(userConfig.fileFormat) + '-' + i + ctx.output[i].extname
} else {
fileName = prefix + dayjs().format(userConfig.fileFormat) + ctx.output[i].extname
}
}else{
fileName = prefix + fileName
}

ctx.output[i].fileName = fileName
}
},
export = (ctx: PicGo) => {
const register = (): void => {
ctx.helper.beforeTransformPlugins.register('super-prefix', {
handle: beforeTransformHandle,
config: pluginConfig
})
ctx.helper.afterUploadPlugins.register('super-prefix', {
handle: afterUploadHandle,
config: pluginConfig
})
}
Expand Down