Skip to content

Commit

Permalink
feat: 自动上传配置到备份服务器
Browse files Browse the repository at this point in the history
* 需要在通用配置中启用
* 只会在自动刷新后触发
* 因为自动刷新会重试失败的, 所以每天可能会上传多次
* 不支持自动下载, 需手动覆盖更新本地配置
* 因为是全量上传, 不考虑配置合并
  • Loading branch information
IITII committed Sep 14, 2023
1 parent 5197991 commit 68a553f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
4 changes: 4 additions & 0 deletions resource/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@
"autoRefreshUserDataTip3": "Retry",
"autoRefreshUserDataTip4": "times after failure,",
"autoRefreshUserDataTip5": "minute apart",
"autoBackupDataTip1": "Automatically backup user data when the browser is open(Beta)",
"autoBackupDataTip2": "Auto upload after ",
"autoBackupDataTip3": "automatically refreshes",
"autoBackupDataTip4": "Backup to",
"searchResultOrderBySitePriority": "When the search results are clicked on the site header, they are sorted by site priority (effective after refreshing the page after saving)",
"saveSearchKey": "Save historical search keyword records",
"showMoiveInfoCardOnSearch": "Show movie and rating information when searching by IMDb number",
Expand Down
4 changes: 4 additions & 0 deletions resource/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@
"autoRefreshUserDataTip3": "失败后重试",
"autoRefreshUserDataTip4": "次,每次间隔",
"autoRefreshUserDataTip5": "分钟",
"autoBackupDataTip1": "在浏览器打开的情况下自动上传用户数据(Beta)",
"autoBackupDataTip2": "自动刷新数据后间隔",
"autoBackupDataTip3": "分钟, 上传数据到",
"autoBackupDataTip4": "上传数据到",
"searchResultOrderBySitePriority": "搜索结果点击站点表头时,按站点优先级别排序(保存后需刷新页面后生效)",
"saveSearchKey": "保存搜索关键字",
"showMoiveInfoCardOnSearch": "当以 IMDb 编号搜索时显示电影及评分信息",
Expand Down
6 changes: 3 additions & 3 deletions src/background/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,14 @@ class Config {
console.log("upgradeSites.site", site, newHost);
site.host = newHost;
site.url = systemSite.url;

// 设置默认图标
if (!systemSite.icon && !site.icon)
site.icon = site.url + "/favicon.ico"
else
site.icon = systemSite.icon;
}

// 更新搜索方案
if (this.options.searchSolutions) {
this.options.searchSolutions.forEach(
Expand Down Expand Up @@ -1049,7 +1049,7 @@ class Config {
* @param server
*/
public backupToServer(server: IBackupServer): Promise<any> {
console.log("backupToServer", server);
console.log("backupToServer", server, this.options.backupServers);
return new Promise<any>((resolve?: any, reject?: any) => {
const time = dayjs().valueOf();
const fileName = this.getNewBackupFileName();
Expand Down
22 changes: 22 additions & 0 deletions src/background/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export default class PTPlugin {
public keepUploadTask: KeepUploadTask = new KeepUploadTask();

private reloadCount: number = 0;
// 定时刷新
private autoRefreshUserDataTimer: number = 0;
// 定时备份
// private autoBackupDataTimer: number = 0;
private autoRefreshUserDataIsWorking: boolean = false;
private autoRefreshUserDataFailedCount: number = 0;

Expand Down Expand Up @@ -450,6 +453,25 @@ export default class PTPlugin {
this.debug("数据刷新完成");
this.autoRefreshUserDataFailedCount = 0;
}
if (this.options.autoBackupData) {
// 可以认为数据不会再变化了
if (!haveError || this.autoRefreshUserDataFailedCount >= failedRetryCount) {
let {autoBackupDataServerId} = this.options
// autoBackupDataMin = parseInt(autoBackupDataMin as any) || 5
console.log(`上传用户数据到 -> ${autoBackupDataServerId}`)
let server = this.options.backupServers?.filter(_ => _.id === autoBackupDataServerId)[0]
if (server) {
console.log(`开始上传用户数据到 -> ${server.name}`)
this.controller.backupToServer(server)
// @ts-ignore
.then(r => console.log(`用户数据上传完成 -> ${server.name}`, r))
// @ts-ignore
.catch(e => console.log(`用户数据上传失败 -> ${server.name}`, e))
} else {
console.log(`未找到备份服务器 -> ${autoBackupDataServerId}`, this.options.backupServers)
}
}
}
});
}
}, 1000);
Expand Down
11 changes: 11 additions & 0 deletions src/interface/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,26 @@ export interface Options {
rowsPerPageItems?: any[];
defaultSearchSolutionId?: string;
searchSolutions?: SearchSolution[];
// 自动刷新用户数据
autoRefreshUserData?: boolean;
// 自动获取用户数据时间间隔(小时)
autoRefreshUserDataHours?: number | string;
// 自动获取用户数据时间间隔(分钟)
autoRefreshUserDataMinutes?: number | string;
// 下一次自动获取用户数据时间(ms)
autoRefreshUserDataNextTime?: number;
// 上一次自动获取用户数据时间(ms)
autoRefreshUserDataLastTime?: number;
// 自动获取用户数据失败重试次数
autoRefreshUserDataFailedRetryCount?: number;
// 自动获取用户数据失败重试间隔时间(分钟)
autoRefreshUserDataFailedRetryInterval?: number;
// 是否自动备份配置
autoBackupData?: boolean;
// 自动备份配置时间间隔(分钟)
// autoBackupDataMin?: number | string;
// 自动备份服务器
autoBackupDataServerId?: string;
// 最近搜索的关键字
lastSearchKey?: string;
// 显示的用名名称
Expand Down
24 changes: 24 additions & 0 deletions src/options/views/settings/Base/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@
</div>
</v-flex>

<!-- 自动备份 -->
<v-switch color="success" v-model="options.autoBackupData" :disabled="!options.autoRefreshUserData"
:label="$t('settings.base.autoBackupDataTip1')"/>
<!-- 自动备份 时间设置 -->
<v-flex xs12 v-if="options.autoRefreshUserData && options.autoBackupData">
<div style="margin: -20px 0 10px 45px;">
<!--<span>{{ $t('settings.base.autoBackupDataTip2') }}</span>-->
<!--<v-select style="max-width: 50px;max-height: 30px;" class="mx-2 d-inline-flex"-->
<!-- v-model="options.autoBackupDataMin" :items="[0,5,10,20,30,45,60,120]"/>-->
<!--<span>{{ $t('settings.base.autoBackupDataTip3') }}</span>-->
<span>{{ $t('settings.base.autoBackupDataTip4') }}</span>
<v-select v-model="options.autoBackupDataServerId" class="mx-2 d-inline-flex"
:items="getBackupServers"/>
</div>
</v-flex>

<!-- 显示插件图标 -->
<v-switch
color="success"
Expand Down Expand Up @@ -808,6 +824,14 @@ export default Vue.extend({
}
},
computed: {
getBackupServers() {
return [].concat(this.$store.state.options.backupServers).map((item: any) => {
return {
text: item.name,
value: item.id
};
});
},
getClientAddress(): any {
if (!this.options.defaultClientId) {
return "";
Expand Down

0 comments on commit 68a553f

Please sign in to comment.