Skip to content

Commit

Permalink
🐛 Fix: some bugs && refactor migrater
Browse files Browse the repository at this point in the history
  • Loading branch information
wayjam committed Feb 22, 2021
1 parent 8e4f26e commit 532ccbc
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 103 deletions.
74 changes: 43 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import picgo from 'picgo'
import FileHandler from './lib/FileHandler'
import Migrater from './lib/Migrater'
import fs from 'fs'
import globby from 'globby'
import path from 'path'
import picgo from 'picgo'
import { PluginConfig } from 'picgo/dist/utils/interfaces'
import fs from 'fs'
import FileHandler from './lib/FileHandler'
import Migrater from './lib/Migrater'

const replaceAll = (content: string, originText: string, replaceText: string): string => {
if (originText === replaceText) {
return content
}
let index = content.indexOf(originText)
while (index !== -1) {
content = content.replace(originText, replaceText)
Expand All @@ -16,15 +19,18 @@ const replaceAll = (content: string, originText: string, replaceText: string): s
}

const handleFiles = async (ctx: picgo, files: string[], guiApi: any = undefined) => {
const newFileSuffix = ctx.getConfig('picgo-plugin-pic-migrater.newFileSuffix')
if (guiApi) {
guiApi.showNotification({
title: `迁移进行中...`,
body: '请耐心等待'
})
}
ctx.log.info('Migrating...')

let total = 0
let success = 0

for (let file of files) {
const fileHandler = new FileHandler(ctx)
// read File
Expand All @@ -35,27 +41,34 @@ const handleFiles = async (ctx: picgo, files: string[], guiApi: any = undefined)

// migrate pics
const result = await migrater.migrate()
total += result.result.total
success += result.result.success
if (result.result.success === 0 && (result.result.total !== 0)) {
ctx.log.warn(`Please check your configuration, since no images migrated successfully in ${file}`)

if (result.total === 0) {
// early next
continue
}

total += result.total
success += result.success
if (result.success === 0 && result.total !== 0) {
ctx.log.warn(
`Please check your configuration, since no images migrated successfully in ${file}`
)
if (guiApi) {
guiApi.showNotification({
title: `${file}迁移失败!`,
body: '迁移图片0成功,请检查是否是URL不存在或者图床配置问题'
title: `${file} 迁移失败!`,
body: '无成功迁移的图片,请检查 URL 是否存在或者图床配置问题'
})
}
continue
} else {
let content = fileHandler.getFileContent(file)
// replace content
for (let originUrl in result.urlList) {
content = replaceAll(content, originUrl, result.urlList[originUrl])
}
const newFileSuffix = ctx.getConfig('picgo-plugin-pic-migrater.newFileSuffix')
result.urls.forEach((item) => {
content = replaceAll(content, item.original, item.new)
})
fileHandler.write(file, content, newFileSuffix)
}
}

ctx.log.info(`Success: ${success} pics, Fail: ${total - success} pics`)
if (guiApi) {
guiApi.showNotification({
Expand All @@ -66,11 +79,11 @@ const handleFiles = async (ctx: picgo, files: string[], guiApi: any = undefined)
}

const guiMenu = (ctx: picgo) => {
const userConfig = ctx.getConfig('picgo-plugin-pic-migrater')
return [
{
label: '选择文件',
async handle (ctx: picgo, guiApi: any) {
let userConfig = ctx.getConfig('picgo-plugin-pic-migrater')
if (!userConfig) {
return guiApi.showNotification({
title: '请先进行配置',
Expand Down Expand Up @@ -100,7 +113,6 @@ const guiMenu = (ctx: picgo) => {
{
label: '选择文件夹',
async handle (ctx: picgo, guiApi: any) {
let userConfig = ctx.getConfig('picgo-plugin-pic-migrater')
if (!userConfig) {
return guiApi.showNotification({
title: '请先进行配置',
Expand Down Expand Up @@ -172,7 +184,7 @@ export = (ctx: picgo) => {
ctx.log.info('picgo set plugin pic-migrater')
return
}
files = files.map(item => path.resolve(item))
files = files.map((item) => path.resolve(item))
let inputFiles = []
for (let filePath of files) {
// make sure filePath exists
Expand All @@ -192,19 +204,19 @@ export = (ctx: picgo) => {
}
})
.on('--help', () => {
console.log()
console.log('Note:')
console.log('You should configurate this plugin first!')
console.log('picgo set plugin pic-migrater')
console.log()
console.log('Examples:')
console.log()
console.log(' # migrate file or files')
console.log(' $ picgo migrate ./test.md ./test1.md')
console.log()
console.log(' # migrate markdown files in folder')
console.log(' $ picgo migrate ./test/')
console.log()
console.log(`
Note:
You should configurate this plugin first!
picgo set plugin pic-migrater
Examples:
# migrate file or files
$ picgo migrate ./test.md ./test1.md
# migrate markdown files in folder
$ picgo migrate ./test/
`.replace(/ +/g, '')
)
})
}
})
Expand Down
5 changes: 3 additions & 2 deletions src/lib/FileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import picgo from 'picgo'
import fs from 'fs'
import path from 'path'
import picgo from 'picgo'

class FileHandler {
ctx: picgo
fileList: {
[propName: string]: string
[propName: string]: string;
}
urlList: any
constructor (ctx: picgo) {
Expand All @@ -22,6 +22,7 @@ class FileHandler {
this.fileList[file] = content
this.getFileUrlContent(file)
}

getFileUrlContent (file: string) {
let urls = this.fileList[file].match(/\!\[.*\]\(.*\)/g)
if (urls === null) {
Expand Down
114 changes: 55 additions & 59 deletions src/lib/Migrater.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import picgo from 'picgo'
import fs from 'fs'
import path from 'path'
import probe from 'probe-image-size'
import { MigrateResult } from './interface'
import picgo from 'picgo'
import { ImgInfo } from 'picgo/dist/utils/interfaces'
import probe from 'probe-image-size'
import { IMigrateResult } from './interface'

class Migrater {
ctx: picgo
guiApi: any
urlList: any
urlArray: any[]
baseDir: string
constructor (ctx: picgo, guiApi: any, filePath: string) {
Expand All @@ -18,12 +17,10 @@ class Migrater {
}

init (urlList: any) {
this.urlList = urlList
this.urlArray = Object.keys(urlList)
}

async migrate (): Promise<MigrateResult> {
let input = []
async migrate (): Promise<IMigrateResult> {
const originTransformer = this.ctx.getConfig('picBed.transformer')
this.ctx.setConfig({
'picBed.transformer': 'base64'
Expand All @@ -33,54 +30,63 @@ class Migrater {
const exclude: string | null = this.ctx.getConfig('picgo-plugin-pic-migrater.exclude') || null
const includesReg = new RegExp(include)
const excludesReg = new RegExp(exclude)
let uploadCount: number = 0
for (let i in this.urlArray) {
try {
if (!include || includesReg.test(this.urlArray[i])) {
if (!exclude || !excludesReg.test(this.urlArray[i])) {
uploadCount++
let uploadData: ImgInfo | Boolean
let picPath = this.getLocalPath(this.urlArray[i])
if (!picPath) {
uploadData = await this.handlePicFromURL(this.urlArray[i])
} else {
uploadData = await this.handlePicFromLocal(picPath, this.urlArray[i])
}
if (uploadData) {
input.push(uploadData)
}

const result: IMigrateResult = {
urls: [],
success: 0,
total: 0
}

if (!this.urlArray || this.urlArray.length === 0) {
return result
}

const toUploadURLs = this.urlArray.filter(url => (!include || includesReg.test(url) && !exclude || !excludesReg.test(url))).map(url => {
return new Promise<ImgInfo>(async (resolve, reject) => {
result.total += 1

try {
let imgInfo: ImgInfo
const picPath = this.getLocalPath(url)
if (!picPath) {
imgInfo = await this.handlePicFromURL(url)
} else {
imgInfo = await this.handlePicFromLocal(picPath, url)
}
resolve(imgInfo)
} catch (err) {
// dont reject
resolve(undefined)
this.ctx.log.error(err)
}
} catch (e) {
console.log(e)
}
}
if (input.length > 0) { // ensure there are available pics
let result = []
})
})

const toUploadImgs = await Promise.all(toUploadURLs).then(imgs => imgs.filter(img => img !== undefined))

// upload
let output = []
if (toUploadImgs && toUploadImgs.length > 0) {
if (this.guiApi) {
result = await this.guiApi.upload(input)
output = await this.guiApi.upload(toUploadImgs)
} else {
await this.ctx.upload(input)
result = this.ctx.output
}
for (let item of result) {
if (this.urlList[item.origin]) {
if (item.imgUrl) {
this.urlList[item.origin] = item.imgUrl
}
}
await this.ctx.upload(toUploadImgs)
output = this.ctx.output
}
}
let result = {
urlList: Object.assign({}, this.urlList),
result: {
success: this.calcSuccessLength(),
total: uploadCount

result.urls = output.filter(item => item.imgUrl && item.imgUrl !== item.origin).map(item => {
return {
original: item.origin,
new: item.imgUrl
}
}
})
result.success = result.urls.length

this.ctx.setConfig({
'picBed.transformer': originTransformer // for GUI reset config
})

return result
}

Expand All @@ -102,7 +108,7 @@ class Migrater {
})
}

async handlePicFromLocal (picPath: string, origin: string) {
async handlePicFromLocal (picPath: string, origin: string): Promise<ImgInfo | undefined> {
if (fs.existsSync(picPath)) {
let fileName = path.basename(picPath)
let buffer = fs.readFileSync(picPath)
Expand All @@ -116,11 +122,11 @@ class Migrater {
origin
}
} else {
return false
return undefined
}
}

async handlePicFromURL (url: string) {
async handlePicFromURL (url: string): Promise<ImgInfo | undefined> {
try {
let buffer = await this.getPicFromURL(url)
let fileName = path.basename(url).split('?')[0].split('#')[0]
Expand All @@ -134,18 +140,8 @@ class Migrater {
origin: url
}
} catch (e) {
return false
}
}

calcSuccessLength () {
let count = 0
for (let i in this.urlList) {
if (this.urlList[i] !== i) {
count += 1
}
return undefined
}
return count
}
}

Expand Down
19 changes: 8 additions & 11 deletions src/lib/interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
interface MigrateResult {
urlList: {
[picPath: string]: string
}
result: {
success: number
total: number
}
interface IMigrateResult {
urls: Array<{
original: string;
new: string;
}>
success: number // count of which was migrated
total: number // total count of which should migrate
}

export {
MigrateResult
}
export { IMigrateResult }

0 comments on commit 532ccbc

Please sign in to comment.