Skip to content

Commit c22492b

Browse files
committed
🐛 Fix: fileName urlencode bug & github duplicate error
ISSUES CLOSED: Molunerfinn/PicGo#1105
1 parent 063006c commit c22492b

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

src/plugins/commander/setting.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ const handleConfig = async (ctx: IPicGo, prompts: IPluginConfig[], module: strin
99
ctx.saveConfig({
1010
[configName]: answer
1111
})
12+
// auto set current uploader or transformer
13+
if (module === 'uploader') {
14+
ctx.saveConfig({
15+
'picBed.current': name,
16+
'picBed.uploader': name
17+
})
18+
} else if (module === 'transformer') {
19+
ctx.saveConfig({
20+
'picBed.transformer': name
21+
})
22+
}
1223
}
1324

1425
const setting = {
@@ -84,9 +95,10 @@ const setting = {
8495
ctx.log.warn(`No module named ${module}`)
8596
return ctx.log.warn('Available modules are uploader|transformer|plugin')
8697
}
87-
const useModuleName = module === 'plugin' ? 'plugins' : module
8898
ctx.log.success('Configure config successfully!')
89-
ctx.log.info(`If you want to use this config, please run 'picgo use ${useModuleName}'`)
99+
if (module === 'plugin') {
100+
ctx.log.info('If you want to use this config, please run \'picgo use plugins\'')
101+
}
90102
} catch (e: any) {
91103
ctx.log.error(e)
92104
if (process.argv.includes('--debug')) {

src/plugins/uploader/aliyun.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const generateSignature = (options: IAliyunConfig, fileName: string): string =>
1919
const postOptions = (options: IAliyunConfig, fileName: string, signature: string, image: Buffer): IOldReqOptionsWithFullResponse => {
2020
return {
2121
method: 'PUT',
22-
url: `https://${options.bucket}.${options.area}.aliyuncs.com/${encodeURI(options.path)}${encodeURI(fileName)}`,
22+
url: `https://${options.bucket}.${options.area}.aliyuncs.com/${encodeURI(options.path)}${encodeURIComponent(fileName)}`,
2323
headers: {
2424
Host: `${options.bucket}.${options.area}.aliyuncs.com`,
2525
Authorization: signature,
@@ -54,9 +54,9 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
5454
delete img.buffer
5555
const optionUrl = aliYunOptions.options || ''
5656
if (customUrl) {
57-
img.imgUrl = `${customUrl}/${path}${img.fileName}${optionUrl}`
57+
img.imgUrl = `${customUrl}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
5858
} else {
59-
img.imgUrl = `https://${aliYunOptions.bucket}.${aliYunOptions.area}.aliyuncs.com/${path}${img.fileName}${optionUrl}`
59+
img.imgUrl = `https://${aliYunOptions.bucket}.${aliYunOptions.area}.aliyuncs.com/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
6060
}
6161
} else {
6262
throw new Error('Upload failed')

src/plugins/uploader/github.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const postOptions = (fileName: string, options: IGithubConfig, data: any): IOldR
88
const { token, repo } = options
99
return {
1010
method: 'PUT',
11-
url: `https://api.github.com/repos/${repo}/contents/${encodeURI(path)}${encodeURI(fileName)}`,
11+
url: `https://api.github.com/repos/${repo}/contents/${encodeURI(path)}${encodeURIComponent(fileName)}`,
1212
headers: {
1313
Authorization: `token ${token}`,
1414
'User-Agent': 'PicGo',
@@ -36,21 +36,36 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
3636
path: githubOptions.path + encodeURI(img.fileName)
3737
}
3838
const postConfig = postOptions(img.fileName, githubOptions, data)
39-
const body: {
40-
content: {
41-
download_url: string
39+
try {
40+
const body: {
41+
content: {
42+
download_url: string
43+
}
44+
} = await ctx.request(postConfig)
45+
if (body) {
46+
delete img.base64Image
47+
delete img.buffer
48+
if (githubOptions.customUrl) {
49+
img.imgUrl = `${githubOptions.customUrl}/${encodeURI(githubOptions.path)}${encodeURIComponent(img.fileName)}`
50+
} else {
51+
img.imgUrl = body.content.download_url
52+
}
53+
} else {
54+
throw new Error('Server error, please try again')
4255
}
43-
} = await ctx.request(postConfig)
44-
if (body) {
45-
delete img.base64Image
46-
delete img.buffer
47-
if (githubOptions.customUrl) {
48-
img.imgUrl = `${githubOptions.customUrl}/${githubOptions.path}${img.fileName}`
56+
} catch (e: any) {
57+
// handle duplicate images
58+
if (e.statusCode === 422) {
59+
delete img.base64Image
60+
delete img.buffer
61+
if (githubOptions.customUrl) {
62+
img.imgUrl = `${githubOptions.customUrl}/${encodeURI(githubOptions.path)}${encodeURIComponent(img.fileName)}`
63+
} else {
64+
img.imgUrl = `https://raw.githubusercontent.com/${githubOptions.repo}/${githubOptions.branch}/${encodeURI(githubOptions.path)}${encodeURIComponent(img.fileName)}`
65+
}
4966
} else {
50-
img.imgUrl = body.content.download_url
67+
throw e
5168
}
52-
} else {
53-
throw new Error('Server error, please try again')
5469
}
5570
}
5671
}

src/plugins/uploader/tcyun.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ const handle = async (ctx: IPicGo): Promise<IPicGo | boolean> => {
142142
delete img.base64Image
143143
delete img.buffer
144144
if (customUrl) {
145-
img.imgUrl = `${customUrl}/${encodeURI(path)}${encodeURI(img.fileName)}${optionUrl}`
145+
img.imgUrl = `${customUrl}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
146146
} else {
147-
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${encodeURI(path)}${encodeURI(img.fileName)}${optionUrl}`
147+
img.imgUrl = `https://${tcYunOptions.bucket}.cos.${tcYunOptions.area}.myqcloud.com/${encodeURI(path)}${encodeURIComponent(img.fileName)}${optionUrl}`
148148
}
149149
} else {
150150
throw new Error(res.body.msg)

src/plugins/uploader/upyun.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const generateSignature = (options: IUpyunConfig, fileName: string): string => {
1313
const password = options.password
1414
const md5Password = MD5(password)
1515
const date = new Date().toUTCString()
16-
const uri = `/${options.bucket}/${encodeURI(path)}${encodeURI(fileName)}`
16+
const uri = `/${options.bucket}/${encodeURI(path)}${encodeURIComponent(fileName)}`
1717
const value = `PUT&${uri}&${date}`
1818
const sign = crypto.createHmac('sha1', md5Password).update(value).digest('base64')
1919
return `UPYUN ${operator}:${sign}`
@@ -24,7 +24,7 @@ const postOptions = (options: IUpyunConfig, fileName: string, signature: string,
2424
const path = options.path || ''
2525
return {
2626
method: 'PUT',
27-
url: `https://v0.api.upyun.com/${bucket}/${encodeURI(path)}${encodeURI(fileName)}`,
27+
url: `https://v0.api.upyun.com/${bucket}/${encodeURI(path)}${encodeURIComponent(fileName)}`,
2828
headers: {
2929
Authorization: signature,
3030
Date: new Date().toUTCString(),
@@ -55,7 +55,7 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
5555
if (body.statusCode === 200) {
5656
delete img.base64Image
5757
delete img.buffer
58-
img.imgUrl = `${upyunOptions.url}/${path}${img.fileName}${upyunOptions.options}`
58+
img.imgUrl = `${upyunOptions.url}/${encodeURI(path)}${encodeURIComponent(img.fileName)}${upyunOptions.options}`
5959
} else {
6060
throw new Error('Upload failed')
6161
}

0 commit comments

Comments
 (0)