mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-01-23 14:48:13 -05:00
🐛 Fix: fix config file sync bug
This commit is contained in:
parent
d8eea9c556
commit
344088f391
@ -9,7 +9,7 @@ import logger from 'apis/core/picgo/logger'
|
|||||||
|
|
||||||
interface SyncConfig {
|
interface SyncConfig {
|
||||||
type: string
|
type: string
|
||||||
file: string
|
file?: string
|
||||||
username: string
|
username: string
|
||||||
repo: string
|
repo: string
|
||||||
branch: string
|
branch: string
|
||||||
@ -25,7 +25,6 @@ const configFileNames = [
|
|||||||
'data.bak.json',
|
'data.bak.json',
|
||||||
'manage.json',
|
'manage.json',
|
||||||
'manage.bak.json',
|
'manage.bak.json',
|
||||||
'piclist-remote-notice.json',
|
|
||||||
'UpDownTaskQueue.json'
|
'UpDownTaskQueue.json'
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -127,6 +126,9 @@ async function compareNewerFile (syncConfig: SyncConfig, fileName: string): Prom
|
|||||||
|
|
||||||
async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||||
const localFilePath = path.join(STORE_PATH, fileName)
|
const localFilePath = path.join(STORE_PATH, fileName)
|
||||||
|
if (!fs.existsSync(localFilePath)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
const { username, repo, branch, token, type } = syncConfig
|
const { username, repo, branch, token, type } = syncConfig
|
||||||
if (type === 'gitee') {
|
if (type === 'gitee') {
|
||||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
||||||
@ -148,7 +150,7 @@ async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
|||||||
content: fs.readFileSync(localFilePath, { encoding: 'base64' }),
|
content: fs.readFileSync(localFilePath, { encoding: 'base64' }),
|
||||||
branch
|
branch
|
||||||
})
|
})
|
||||||
return res.status === 200
|
return res.status >= 200 && res.status < 300
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
return false
|
return false
|
||||||
@ -158,6 +160,9 @@ async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
|||||||
|
|
||||||
async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||||
const localFilePath = path.join(STORE_PATH, fileName)
|
const localFilePath = path.join(STORE_PATH, fileName)
|
||||||
|
if (!fs.existsSync(localFilePath)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
const { username, repo, branch, token, type } = syncConfig
|
const { username, repo, branch, token, type } = syncConfig
|
||||||
if (type === 'gitee') {
|
if (type === 'gitee') {
|
||||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
||||||
@ -187,7 +192,7 @@ async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
|||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
const octokit = getOctokit(syncConfig)
|
const octokit = getOctokit(syncConfig)
|
||||||
try {
|
|
||||||
const shaRes = await octokit.rest.repos.getContent({
|
const shaRes = await octokit.rest.repos.getContent({
|
||||||
owner: username,
|
owner: username,
|
||||||
repo,
|
repo,
|
||||||
@ -195,7 +200,7 @@ async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
|||||||
ref: branch
|
ref: branch
|
||||||
})
|
})
|
||||||
if (shaRes.status !== 200) {
|
if (shaRes.status !== 200) {
|
||||||
return false
|
throw new Error('get sha failed')
|
||||||
}
|
}
|
||||||
const data = shaRes.data as any
|
const data = shaRes.data as any
|
||||||
const sha = data.sha
|
const sha = data.sha
|
||||||
@ -209,10 +214,6 @@ async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
|||||||
sha
|
sha
|
||||||
})
|
})
|
||||||
return res.status === 200
|
return res.status === 200
|
||||||
} catch (error: any) {
|
|
||||||
logger.error(error)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +276,11 @@ async function syncFile (syncConfig: SyncConfig, fileName: string) {
|
|||||||
if (compareResult === 'upload') {
|
if (compareResult === 'upload') {
|
||||||
result = await uploadLocalToRemote(syncConfig, fileName)
|
result = await uploadLocalToRemote(syncConfig, fileName)
|
||||||
} else if (compareResult === 'update') {
|
} else if (compareResult === 'update') {
|
||||||
|
try {
|
||||||
result = await updateLocalToRemote(syncConfig, fileName)
|
result = await updateLocalToRemote(syncConfig, fileName)
|
||||||
|
} catch (error: any) {
|
||||||
|
result = await uploadLocalToRemote(syncConfig, fileName)
|
||||||
|
}
|
||||||
} else if (compareResult === 'download') {
|
} else if (compareResult === 'download') {
|
||||||
result = await downloadRemoteToLocal(syncConfig, fileName)
|
result = await downloadRemoteToLocal(syncConfig, fileName)
|
||||||
}
|
}
|
||||||
@ -288,6 +293,8 @@ async function syncAllFiles (syncConfig: SyncConfig) {
|
|||||||
const result = await syncFile(syncConfig, file)
|
const result = await syncFile(syncConfig, file)
|
||||||
if (result) {
|
if (result) {
|
||||||
logger.info(`sync file ${file} success`)
|
logger.info(`sync file ${file} success`)
|
||||||
|
} else {
|
||||||
|
logger.error(`sync file ${file} failed`)
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error(`sync file ${file} failed`)
|
logger.error(`sync file ${file} failed`)
|
||||||
|
@ -838,7 +838,6 @@
|
|||||||
<el-button
|
<el-button
|
||||||
type="primary"
|
type="primary"
|
||||||
round
|
round
|
||||||
:disabled="!allSynFilled"
|
|
||||||
@click="confirmSyncSetting"
|
@click="confirmSyncSetting"
|
||||||
>
|
>
|
||||||
{{ $T('CONFIRM') }}
|
{{ $T('CONFIRM') }}
|
||||||
@ -1310,7 +1309,9 @@ function confirmSyncSetting () {
|
|||||||
'settings.sync': sync.value
|
'settings.sync': sync.value
|
||||||
})
|
})
|
||||||
syncVisible.value = false
|
syncVisible.value = false
|
||||||
|
if (allSynFilled.value) {
|
||||||
sendRPC(IRPCActionType.RELOAD_APP)
|
sendRPC(IRPCActionType.RELOAD_APP)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const version = pkg.version
|
const version = pkg.version
|
||||||
|
Loading…
Reference in New Issue
Block a user