Feature: add interval for sync setting

This commit is contained in:
萌萌哒赫萝 2023-05-02 12:03:21 +08:00
parent ab4e31f62a
commit 7150bf381c
7 changed files with 53 additions and 19 deletions

View File

@ -215,6 +215,7 @@ SETTINGS_SYNC_CONFIG_GITEE_BRANCH: branch
SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: Token SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: Token
SETTINGS_SYNC_CONFIG_GITEE_TOKEN: Token SETTINGS_SYNC_CONFIG_GITEE_TOKEN: Token
SETTINGS_SYNC_CONFIG_PROXY: Proxy SETTINGS_SYNC_CONFIG_PROXY: Proxy
SETTINGS_SYNC_CONFIG_INTERVAL: Interval(min)
SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: Please enter GitHub username SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: Please enter GitHub username
SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: Please enter Gitee username SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: Please enter Gitee username
SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: Please enter GitHub repository name SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: Please enter GitHub repository name

View File

@ -217,6 +217,7 @@ SETTINGS_SYNC_CONFIG_GITEE_BRANCH: Gitee分支
SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: GitHub Token SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: GitHub Token
SETTINGS_SYNC_CONFIG_GITEE_TOKEN: Gitee Token SETTINGS_SYNC_CONFIG_GITEE_TOKEN: Gitee Token
SETTINGS_SYNC_CONFIG_PROXY: 代理 SETTINGS_SYNC_CONFIG_PROXY: 代理
SETTINGS_SYNC_CONFIG_INTERVAL: 间隔(分钟)
SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: 请输入GitHub用户名 SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: 请输入GitHub用户名
SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: 请输入Gitee用户名 SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: 请输入Gitee用户名
SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: 请输入GitHub仓库名 SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: 请输入GitHub仓库名

View File

@ -215,6 +215,7 @@ SETTINGS_SYNC_CONFIG_GITEE_BRANCH: Gitee 分支
SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: GitHub Token SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: GitHub Token
SETTINGS_SYNC_CONFIG_GITEE_TOKEN: Gitee Token SETTINGS_SYNC_CONFIG_GITEE_TOKEN: Gitee Token
SETTINGS_SYNC_CONFIG_PROXY: 代理 SETTINGS_SYNC_CONFIG_PROXY: 代理
SETTINGS_SYNC_CONFIG_INTERVAL: 間隔(分鐘)
SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: 請輸入 GitHub 用戶名 SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: 請輸入 GitHub 用戶名
SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: 請輸入 Gitee 用戶名 SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: 請輸入 Gitee 用戶名
SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: 請輸入 GitHub 儲存庫名稱 SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: 請輸入 GitHub 儲存庫名稱

View File

@ -7,6 +7,17 @@ import { HttpsProxyAgent } from 'hpagent'
import { Octokit } from '@octokit/rest' import { Octokit } from '@octokit/rest'
import logger from 'apis/core/picgo/logger' import logger from 'apis/core/picgo/logger'
interface SyncConfig {
type: string
file: string
username: string
repo: string
branch: string
token: string
proxy?: string,
interval?: number
}
const STORE_PATH = app.getPath('userData') const STORE_PATH = app.getPath('userData')
const configFileNames = [ const configFileNames = [
@ -18,7 +29,7 @@ const configFileNames = [
'UpDownTaskQueue.json' 'UpDownTaskQueue.json'
] ]
function getOctokit (syncConfig: any) { function getOctokit (syncConfig: SyncConfig) {
const { token, proxy } = syncConfig const { token, proxy } = syncConfig
return new Octokit({ return new Octokit({
auth: token, auth: token,
@ -48,12 +59,12 @@ function getSyncConfig () {
return syncConfig return syncConfig
} }
function syncConfigValidator (syncConfig: any) { function syncConfigValidator (syncConfig: SyncConfig) {
const { type, file, username, repo, branch, token } = syncConfig const { type, file, username, repo, branch, token } = syncConfig
return type && file && username && repo && branch && token return type && file && username && repo && branch && token
} }
async function getModifiedTime (syncConfig: IStringKeyMap, filePath: string) { async function getModifiedTime (syncConfig: SyncConfig, filePath: string) {
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}/commits` const url = `https://gitee.com/api/v5/repos/${username}/${repo}/commits`
@ -100,7 +111,7 @@ async function getModifiedTimeOfLocal (filePath: string) {
return stat.mtime return stat.mtime
} }
async function compareNewerFile (syncConfig: IStringKeyMap, fileName: string): Promise<'upload' | 'download' | 'update' | undefined> { async function compareNewerFile (syncConfig: SyncConfig, fileName: string): Promise<'upload' | 'download' | 'update' | undefined> {
const localFilePath = path.join(STORE_PATH, fileName) const localFilePath = path.join(STORE_PATH, fileName)
const remoteModifiedTime = await getModifiedTime(syncConfig, fileName) const remoteModifiedTime = await getModifiedTime(syncConfig, fileName)
if (remoteModifiedTime === null) { if (remoteModifiedTime === null) {
@ -114,7 +125,7 @@ async function compareNewerFile (syncConfig: IStringKeyMap, fileName: string): P
} }
} }
async function uploadLocalToRemote (syncConfig: IStringKeyMap, fileName: string) { async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
const localFilePath = path.join(STORE_PATH, fileName) const localFilePath = path.join(STORE_PATH, fileName)
const { username, repo, branch, token, type } = syncConfig const { username, repo, branch, token, type } = syncConfig
if (type === 'gitee') { if (type === 'gitee') {
@ -145,7 +156,7 @@ async function uploadLocalToRemote (syncConfig: IStringKeyMap, fileName: string)
} }
} }
async function updateLocalToRemote (syncConfig: IStringKeyMap, fileName: string) { async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
const localFilePath = path.join(STORE_PATH, fileName) const localFilePath = path.join(STORE_PATH, fileName)
const { username, repo, branch, token, type } = syncConfig const { username, repo, branch, token, type } = syncConfig
if (type === 'gitee') { if (type === 'gitee') {
@ -205,7 +216,7 @@ async function updateLocalToRemote (syncConfig: IStringKeyMap, fileName: string)
} }
} }
async function downloadRemoteToLocal (syncConfig: IStringKeyMap, fileName: string) { async function downloadRemoteToLocal (syncConfig: SyncConfig, fileName: string) {
const localFilePath = path.join(STORE_PATH, fileName) const localFilePath = path.join(STORE_PATH, fileName)
const { username, repo, branch, token, proxy, type } = syncConfig const { username, repo, branch, token, proxy, type } = syncConfig
if (type === 'gitee') { if (type === 'gitee') {
@ -258,9 +269,8 @@ async function downloadRemoteToLocal (syncConfig: IStringKeyMap, fileName: strin
} }
} }
async function syncFile (syncConfig: IStringKeyMap, fileName: string) { async function syncFile (syncConfig: SyncConfig, fileName: string) {
const compareResult = await compareNewerFile(syncConfig, fileName) const compareResult = await compareNewerFile(syncConfig, fileName)
logger.info(`file ${fileName} compare result: ${compareResult}`)
let result = false let result = false
if (compareResult === 'upload') { if (compareResult === 'upload') {
result = await uploadLocalToRemote(syncConfig, fileName) result = await uploadLocalToRemote(syncConfig, fileName)
@ -272,7 +282,7 @@ async function syncFile (syncConfig: IStringKeyMap, fileName: string) {
return result return result
} }
async function syncAllFiles (syncConfig: IStringKeyMap) { async function syncAllFiles (syncConfig: SyncConfig) {
for (const file of configFileNames) { for (const file of configFileNames) {
try { try {
const result = await syncFile(syncConfig, file) const result = await syncFile(syncConfig, file)
@ -286,6 +296,15 @@ async function syncAllFiles (syncConfig: IStringKeyMap) {
} }
} }
async function syncFunc () {
const syncConfig = await getSyncConfig()
if (!syncConfigValidator(syncConfig)) {
return
}
await syncAllFiles(syncConfig)
logger.info(`sync all files at ${new Date().toLocaleString()}`)
}
async function syncInterval () { async function syncInterval () {
const syncConfig = await getSyncConfig() const syncConfig = await getSyncConfig()
if (!syncConfigValidator(syncConfig)) { if (!syncConfigValidator(syncConfig)) {
@ -296,11 +315,13 @@ async function syncInterval () {
logger.info(`sync all files at ${new Date().toLocaleString()}`) logger.info(`sync all files at ${new Date().toLocaleString()}`)
} }
await syncFunc() await syncFunc()
const interval = Number(syncConfig.interval) || 60
setInterval(async () => { setInterval(async () => {
syncFunc() syncFunc()
}, 1000 * 60 * 10) }, 1000 * 60 * interval)
} }
export { export {
syncFunc,
syncInterval syncInterval
} }

View File

@ -808,6 +808,15 @@
:placeholder="$T('SETTINGS_SYNC_CONFIG_PROXY_PLACEHOLDER')" :placeholder="$T('SETTINGS_SYNC_CONFIG_PROXY_PLACEHOLDER')"
/> />
</el-form-item> </el-form-item>
<el-form-item
:label="$T('SETTINGS_SYNC_CONFIG_INTERVAL')"
>
<el-input-number
v-model="sync.interval"
:min="10"
:step="1"
/>
</el-form-item>
</el-form> </el-form>
<template #footer> <template #footer>
<el-button <el-button
@ -1252,7 +1261,8 @@ const sync = ref({
repo: '', repo: '',
branch: '', branch: '',
token: '', token: '',
proxy: '' proxy: '',
interval: 60
}) })
const syncType = [ const syncType = [
@ -1278,7 +1288,8 @@ async function cancelSyncSetting () {
repo: '', repo: '',
branch: '', branch: '',
token: '', token: '',
proxy: '' proxy: '',
interval: 60
} }
} }
@ -1354,7 +1365,8 @@ async function initData () {
repo: '', repo: '',
branch: '', branch: '',
token: '', token: '',
proxy: '' proxy: '',
interval: 60
} }
form.logFileSizeLimit = enforceNumber(settings.logFileSizeLimit) || 10 form.logFileSizeLimit = enforceNumber(settings.logFileSizeLimit) || 10
} }

View File

@ -1,10 +1,7 @@
class LS { class LS {
get (name: string) { get (name: string) {
if (localStorage.getItem(name)) { const item = localStorage.getItem(name) as string
return JSON.parse(localStorage.getItem(name) as string) return item ? JSON.parse(item) : {}
} else {
return {}
}
} }
set (name: string, value: any) { set (name: string, value: any) {

View File

@ -211,6 +211,7 @@ interface ILocales {
SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: string SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: string
SETTINGS_SYNC_CONFIG_GITEE_TOKEN: string SETTINGS_SYNC_CONFIG_GITEE_TOKEN: string
SETTINGS_SYNC_CONFIG_PROXY: string SETTINGS_SYNC_CONFIG_PROXY: string
SETTINGS_SYNC_CONFIG_INTERVAL: string
SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: string SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: string
SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: string SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: string
SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: string SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: string