Skip to content

Commit

Permalink
✨ Feature(custom): optimize second uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Sep 11, 2024
1 parent 0afc8e9 commit 58ae7ea
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export class Lifecycle extends EventEmitter {
this.ctx = ctx
this.ttfPath = path.join(ctx.baseDir, 'assets', 'simhei.ttf')
ensureDirSync(path.join(ctx.baseDir, 'imgTemp'))
emptyDirSync(path.join(ctx.baseDir, 'piclistTemp'))
const enableSecondUploader = ctx.getConfig<Undefinable<boolean>>('settings.enableSecondUploader') || false
if (!enableSecondUploader) {
emptyDirSync(path.join(ctx.baseDir, 'piclistTemp'))
}
}

private async downloadTTF(): Promise<boolean> {
Expand Down Expand Up @@ -84,21 +87,30 @@ export class Lifecycle extends EventEmitter {
}
}

async start(input: any[]): Promise<IPicGo> {
async start(input: any[], skipProcess = false): Promise<IPicGo> {
// ensure every upload process has an unique context
const ctx = createContext(this.ctx)
try {
if (!Array.isArray(input)) throw new Error('Input must be an array.')
ctx.input = input
ctx.output = [] as IImgInfo[]
ctx.rawInputPath = [] as string[]
ctx.rawInput = cloneDeep(input)
ctx.output = [] as IImgInfo[]
if (skipProcess) {
ctx.log.info('Skip process.')
ctx.output = input
await this.doUpload(ctx)
ctx.input = ctx.rawInput
await this.afterUpload(ctx)
return ctx
}
// lifecycle main
await this.preprocess(ctx)
await this.beforeTransform(ctx)
await this.doTransform(ctx)
await this.buildInRename(ctx)
await this.beforeUpload(ctx)
ctx.processedInput = cloneDeep(ctx.output)
await this.doUpload(ctx)
ctx.input = ctx.rawInput
await this.afterUpload(ctx)
Expand Down
34 changes: 34 additions & 0 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class PicGo extends EventEmitter implements IPicGo {
output: IImgInfo[]
input: any[]
rawInput: any[]
processedInput: any[]
pluginHandler: PluginHandler
/**
* @deprecated will be removed in v1.5.0+
Expand All @@ -71,6 +72,7 @@ export class PicGo extends EventEmitter implements IPicGo {
this.configPath = configPath
this.input = []
this.rawInput = []
this.processedInput = []
this.output = []
this.helper = {
transformer: new LifecyclePlugins('transformer'),
Expand Down Expand Up @@ -237,4 +239,36 @@ export class PicGo extends EventEmitter implements IPicGo {
return output
}
}

async uploadReturnCtx(input?: any[], skipProcess = false): Promise<IPicGo> {
if (this.configPath === '') {
this.log.error('No config file found, please check your config file path')
return this
}

if (input === undefined || input.length === 0) {
try {
const { imgPath, shouldKeepAfterUploading } = await getClipboardImage(this)
const cleanup = (): void => {
if (!shouldKeepAfterUploading) {
remove(imgPath).catch(e => {
this.log.error(e)
})
}
}
if (imgPath === 'no image') {
throw new Error('image not found in clipboard')
} else {
this.once(IBuildInEvent.FAILED, cleanup)
this.once(IBuildInEvent.FINISHED, cleanup)
return await this.lifecycle.start([imgPath], skipProcess)
}
} catch (e) {
this.emit(IBuildInEvent.FAILED, e)
throw e
}
} else {
return await this.lifecycle.start(input, skipProcess)
}
}
}
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface IPicGo extends NodeJS.EventEmitter {
* the origin input
*/
rawInput: any[]
/**
* processed input path
*/
processedInput: any[]
/**
* register\unregister\get picgo's plugin
*/
Expand Down Expand Up @@ -99,6 +103,10 @@ export interface IPicGo extends NodeJS.EventEmitter {
* upload gogogo
*/
upload: (input?: any[]) => Promise<IImgInfo[] | Error>
/**
* transform gogogo
*/
uploadReturnCtx: (input?: any[]) => Promise<IPicGo>
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/utils/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createContext = (ctx: IPicGo): IPicGo => {
output: [],
input: [],
rawInput: [],
processedInput: [],
pluginLoader: ctx.pluginLoader,
pluginHandler: ctx.pluginHandler,
Request: ctx.Request,
Expand All @@ -27,6 +28,7 @@ export const createContext = (ctx: IPicGo): IPicGo => {
setConfig: ctx.setConfig.bind(ctx),
unsetConfig: ctx.unsetConfig.bind(ctx),
upload: ctx.upload.bind(ctx),
uploadReturnCtx: ctx.uploadReturnCtx.bind(ctx),
addListener: ctx.addListener.bind(ctx),
on: ctx.on.bind(ctx),
once: ctx.once.bind(ctx),
Expand Down

0 comments on commit 58ae7ea

Please sign in to comment.