mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-08-28 19:47:17 +08:00
🔨 Refactor: change config sync to manual
This commit is contained in:
@@ -44,6 +44,7 @@ import { buildMainPageMenu, buildMiniPageMenu, buildPluginPageMenu, buildPicBedL
|
||||
import path from 'path'
|
||||
import { T } from '~/main/i18n'
|
||||
import { generateShortUrl } from '~/universal/utils/common'
|
||||
import { uploadFile, downloadFile } from '../utils/syncSettings'
|
||||
|
||||
const STORE_PATH = app.getPath('userData')
|
||||
|
||||
@@ -164,6 +165,46 @@ export default {
|
||||
return shortUrl
|
||||
})
|
||||
|
||||
ipcMain.handle('uploadCommonConfig', async () => {
|
||||
const dataResult = await uploadFile('data.json')
|
||||
const bakResult = await uploadFile('data.bak.json')
|
||||
return dataResult + bakResult
|
||||
})
|
||||
|
||||
ipcMain.handle('downloadCommonConfig', async () => {
|
||||
const dataResult = await downloadFile('data.json')
|
||||
const bakResult = await downloadFile('data.bak.json')
|
||||
return dataResult + bakResult
|
||||
})
|
||||
|
||||
ipcMain.handle('uploadManageConfig', async () => {
|
||||
const manageResult = await uploadFile('manage.json')
|
||||
const bakResult = await uploadFile('manage.bak.json')
|
||||
return manageResult + bakResult
|
||||
})
|
||||
|
||||
ipcMain.handle('downloadManageConfig', async () => {
|
||||
const manageResult = await downloadFile('manage.json')
|
||||
const bakResult = await downloadFile('manage.bak.json')
|
||||
return manageResult + bakResult
|
||||
})
|
||||
|
||||
ipcMain.handle('uploadAllConfig', async () => {
|
||||
const dataResult = await uploadFile('data.json')
|
||||
const bakResult = await uploadFile('data.bak.json')
|
||||
const manageResult = await uploadFile('manage.json')
|
||||
const manageBakResult = await uploadFile('manage.bak.json')
|
||||
return dataResult + bakResult + manageResult + manageBakResult
|
||||
})
|
||||
|
||||
ipcMain.handle('downloadAllConfig', async () => {
|
||||
const dataResult = await downloadFile('data.json')
|
||||
const bakResult = await downloadFile('data.bak.json')
|
||||
const manageResult = await downloadFile('manage.json')
|
||||
const manageBakResult = await downloadFile('manage.bak.json')
|
||||
return dataResult + bakResult + manageResult + manageBakResult
|
||||
})
|
||||
|
||||
ipcMain.on('openSettingWindow', () => {
|
||||
windowManager.get(IWindowList.SETTING_WINDOW)!.show()
|
||||
if (windowManager.has(IWindowList.MINI_WINDOW)) {
|
||||
|
||||
@@ -48,7 +48,6 @@ import clipboardPoll from '../utils/clipboardPoll'
|
||||
import path from 'path'
|
||||
import { CLIPBOARD_IMAGE_FOLDER } from '~/universal/utils/static'
|
||||
import fs from 'fs-extra'
|
||||
import { syncInterval } from '../utils/syncSettings'
|
||||
const isDevelopment = process.env.NODE_ENV !== 'production'
|
||||
|
||||
const handleStartUpFiles = (argv: string[], cwd: string) => {
|
||||
@@ -202,7 +201,6 @@ class LifeCycle {
|
||||
}
|
||||
const clipboardDir = path.join(picgo.baseDir, CLIPBOARD_IMAGE_FOLDER)
|
||||
fs.ensureDir(clipboardDir)
|
||||
syncInterval()
|
||||
}
|
||||
app.whenReady().then(readyFunction)
|
||||
}
|
||||
|
||||
+72
-116
@@ -46,7 +46,7 @@ function getOctokit (syncConfig: SyncConfig) {
|
||||
}
|
||||
|
||||
function getSyncConfig () {
|
||||
const syncConfig = db.get('settings.sync') || {
|
||||
return db.get('settings.sync') || {
|
||||
type: 'github',
|
||||
username: '',
|
||||
repo: '',
|
||||
@@ -54,7 +54,6 @@ function getSyncConfig () {
|
||||
token: '',
|
||||
proxy: ''
|
||||
}
|
||||
return syncConfig
|
||||
}
|
||||
|
||||
function syncConfigValidator (syncConfig: SyncConfig) {
|
||||
@@ -62,67 +61,6 @@ function syncConfigValidator (syncConfig: SyncConfig) {
|
||||
return type && username && repo && branch && token
|
||||
}
|
||||
|
||||
async function getModifiedTime (syncConfig: SyncConfig, filePath: string) {
|
||||
const { username, repo, branch, token, type } = syncConfig
|
||||
if (type === 'gitee') {
|
||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/commits`
|
||||
const res = await axios.get(url, {
|
||||
params: {
|
||||
access_token: token,
|
||||
ref: branch,
|
||||
path: filePath
|
||||
}
|
||||
})
|
||||
const data = res.data
|
||||
if (data.length > 0) {
|
||||
return data[0].commit.committer.date
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
} else {
|
||||
const octokit = getOctokit(syncConfig)
|
||||
try {
|
||||
const res = await octokit.rest.repos.listCommits({
|
||||
owner: username,
|
||||
repo,
|
||||
ref: branch,
|
||||
path: filePath,
|
||||
per_page: 1
|
||||
})
|
||||
if (res.status === 200) {
|
||||
return res.data.length > 0 ? res.data[0].commit.committer?.date : null
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error(error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getModifiedTimeOfLocal (filePath: string) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return new Date(0)
|
||||
}
|
||||
const stat = await fs.stat(filePath)
|
||||
return stat.mtime
|
||||
}
|
||||
|
||||
async function compareNewerFile (syncConfig: SyncConfig, fileName: string): Promise<'upload' | 'download' | 'update' | undefined> {
|
||||
const localFilePath = path.join(STORE_PATH, fileName)
|
||||
const remoteModifiedTime = await getModifiedTime(syncConfig, fileName)
|
||||
if (remoteModifiedTime === null) {
|
||||
return 'upload'
|
||||
}
|
||||
const localModifiedTime = await getModifiedTimeOfLocal(localFilePath)
|
||||
if (remoteModifiedTime && localModifiedTime) {
|
||||
return Date.parse(remoteModifiedTime) > localModifiedTime.getTime() ? 'download' : 'update'
|
||||
} else {
|
||||
throw new Error('get modified time failed')
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||
const localFilePath = path.join(STORE_PATH, fileName)
|
||||
if (!fs.existsSync(localFilePath)) {
|
||||
@@ -130,14 +68,19 @@ async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||
}
|
||||
const { username, repo, branch, token, type } = syncConfig
|
||||
if (type === 'gitee') {
|
||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
||||
const res = await axios.post(url, {
|
||||
access_token: token,
|
||||
branch,
|
||||
content: fs.readFileSync(localFilePath, { encoding: 'base64' }),
|
||||
message: `upload ${fileName} from PicList`
|
||||
})
|
||||
return res.status >= 200 && res.status < 300
|
||||
try {
|
||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
||||
const res = await axios.post(url, {
|
||||
access_token: token,
|
||||
branch,
|
||||
content: fs.readFileSync(localFilePath, { encoding: 'base64' }),
|
||||
message: `upload ${fileName} from PicList`
|
||||
})
|
||||
return res.status >= 200 && res.status < 300
|
||||
} catch (error: any) {
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
const octokit = getOctokit(syncConfig)
|
||||
try {
|
||||
@@ -269,65 +212,78 @@ async function downloadRemoteToLocal (syncConfig: SyncConfig, fileName: string)
|
||||
}
|
||||
}
|
||||
|
||||
async function syncFile (syncConfig: SyncConfig, fileName: string) {
|
||||
const compareResult = await compareNewerFile(syncConfig, fileName)
|
||||
let result = false
|
||||
if (compareResult === 'upload') {
|
||||
result = await uploadLocalToRemote(syncConfig, fileName)
|
||||
} else if (compareResult === 'update') {
|
||||
try {
|
||||
result = await updateLocalToRemote(syncConfig, fileName)
|
||||
} catch (error: any) {
|
||||
result = await uploadLocalToRemote(syncConfig, fileName)
|
||||
}
|
||||
} else if (compareResult === 'download') {
|
||||
result = await downloadRemoteToLocal(syncConfig, fileName)
|
||||
async function uploadFile (fileName: string, all = false) {
|
||||
const syncConfig = getSyncConfig()
|
||||
if (!syncConfigValidator(syncConfig)) {
|
||||
logger.error('sync config is invalid')
|
||||
return 0
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
async function syncAllFiles (syncConfig: SyncConfig) {
|
||||
for (const file of configFileNames) {
|
||||
try {
|
||||
const result = await syncFile(syncConfig, file)
|
||||
let successCount = 0
|
||||
if (all) {
|
||||
for (const file of configFileNames) {
|
||||
const result = await uploadFunc(syncConfig, file)
|
||||
if (result) {
|
||||
logger.info(`sync file ${file} success`)
|
||||
} else {
|
||||
logger.error(`sync file ${file} failed`)
|
||||
successCount++
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error(`sync file ${file} failed`)
|
||||
logger.error(error)
|
||||
}
|
||||
logger.info(`upload all files at ${new Date().toLocaleString()}`)
|
||||
return successCount
|
||||
} else {
|
||||
const ressult = await uploadFunc(syncConfig, fileName)
|
||||
return ressult ? 1 : 0
|
||||
}
|
||||
}
|
||||
|
||||
async function syncFunc () {
|
||||
const syncConfig = await getSyncConfig()
|
||||
if (!syncConfigValidator(syncConfig)) {
|
||||
return
|
||||
async function uploadFunc (syncConfig: SyncConfig, fileName: string) {
|
||||
let result = false
|
||||
try {
|
||||
result = await updateLocalToRemote(syncConfig, fileName)
|
||||
} catch (error: any) {
|
||||
result = await uploadLocalToRemote(syncConfig, fileName)
|
||||
}
|
||||
if (!result) {
|
||||
logger.error(`upload ${fileName} failed`)
|
||||
return false
|
||||
} else {
|
||||
logger.info(`upload ${fileName} success`)
|
||||
return true
|
||||
}
|
||||
await syncAllFiles(syncConfig)
|
||||
logger.info(`sync all files at ${new Date().toLocaleString()}`)
|
||||
}
|
||||
|
||||
async function syncInterval () {
|
||||
const syncConfig = await getSyncConfig()
|
||||
async function downloadFile (fileName: string, all = false) {
|
||||
const syncConfig = getSyncConfig()
|
||||
if (!syncConfigValidator(syncConfig)) {
|
||||
return
|
||||
logger.error('sync config is invalid')
|
||||
return 0
|
||||
}
|
||||
const syncFunc = async () => {
|
||||
await syncAllFiles(syncConfig)
|
||||
logger.info(`sync all files at ${new Date().toLocaleString()}`)
|
||||
if (all) {
|
||||
let successCount = 0
|
||||
for (const file of configFileNames) {
|
||||
const result = await downloadFunc(syncConfig, file)
|
||||
if (result) {
|
||||
successCount++
|
||||
}
|
||||
}
|
||||
logger.info(`download all files at ${new Date().toLocaleString()}`)
|
||||
return successCount
|
||||
} else {
|
||||
const result = await downloadFunc(syncConfig, fileName)
|
||||
return result ? 1 : 0
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadFunc (syncConfig: SyncConfig, fileName: string) {
|
||||
const result = await downloadRemoteToLocal(syncConfig, fileName)
|
||||
if (!result) {
|
||||
logger.error(`download ${fileName} failed`)
|
||||
return false
|
||||
} else {
|
||||
logger.info(`download ${fileName} success`)
|
||||
return true
|
||||
}
|
||||
await syncFunc()
|
||||
const interval = Number(syncConfig.interval) || 60
|
||||
setInterval(async () => {
|
||||
syncFunc()
|
||||
}, 1000 * 60 * interval)
|
||||
}
|
||||
|
||||
export {
|
||||
syncFunc,
|
||||
syncInterval
|
||||
uploadFile,
|
||||
downloadFile
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user