Files
kiss-translator/src/libs/sync.js

229 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-07-31 15:08:51 +08:00
import {
2023-09-18 17:36:10 +08:00
APP_LCNAME,
2023-07-31 15:08:51 +08:00
KV_SETTING_KEY,
KV_RULES_KEY,
2023-08-20 23:30:08 +08:00
KV_RULES_SHARE_KEY,
KV_SALT_SHARE,
2023-09-18 15:45:32 +08:00
OPT_SYNCTYPE_WEBDAV,
2023-07-31 15:08:51 +08:00
} from "../config";
2023-08-31 00:18:57 +08:00
import {
getSyncWithDefault,
updateSync,
getSettingWithDefault,
getRulesWithDefault,
setSetting,
setRules,
} from "./storage";
2023-07-31 15:08:51 +08:00
import { apiSyncData } from "../apis";
2023-08-20 23:30:08 +08:00
import { sha256 } from "./utils";
2023-09-19 11:56:19 +08:00
import { createClient, getPatcher } from "webdav";
import { fetchApi } from "./fetch";
getPatcher().patch("request", (opts) => {
return fetchApi({
input: opts.url,
init: { method: opts.method, headers: opts.headers, body: opts.data },
});
});
2023-09-18 17:36:10 +08:00
const syncByWebdav = async ({
key,
value,
syncUrl,
syncUser,
syncKey,
updateAt = 0,
syncAt = 0,
isForce = false,
}) => {
const client = createClient(syncUrl, {
username: syncUser,
password: syncKey,
});
const pathname = `/${APP_LCNAME}`;
const filename = `/${APP_LCNAME}/${key}`;
const data = JSON.stringify(value, null, " ");
if ((await client.exists(pathname)) === false) {
await client.createDirectory(pathname);
}
const isExist = await client.exists(filename);
if (isExist && !isForce) {
const { lastmod } = await client.stat(filename);
const fileUpdateAt = Date.parse(lastmod);
if (syncAt === 0 || fileUpdateAt > updateAt) {
const data = await client.getFileContents(filename, { format: "text" });
return { updateAt: fileUpdateAt, value: JSON.parse(data) };
}
}
await client.putFileContents(filename, data);
const { lastmod } = await client.stat(filename);
const fileUpdateAt = Date.parse(lastmod);
return { updateAt: fileUpdateAt, value };
};
2023-07-31 15:08:51 +08:00
2023-09-18 15:45:32 +08:00
const syncByWorker = async ({
key,
value,
syncUrl,
syncKey,
updateAt = 0,
syncAt = 0,
isBg = false,
isForce = false,
}) => {
if (isForce) {
updateAt = Date.now();
}
return await apiSyncData(
`${syncUrl}/sync`,
syncKey,
{
key,
value,
updateAt: syncAt === 0 ? 0 : updateAt,
},
isBg
);
};
/**
* 同步设置
* @returns
*/
2023-09-11 11:33:28 +08:00
const syncSetting = async (isBg = false, isForce = false) => {
2023-09-18 15:45:32 +08:00
const {
syncType,
2023-09-11 11:33:28 +08:00
syncUrl,
2023-09-18 15:45:32 +08:00
syncUser,
2023-09-11 11:33:28 +08:00
syncKey,
settingUpdateAt = 0,
settingSyncAt = 0,
} = await getSyncWithDefault();
2023-09-18 15:45:32 +08:00
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
2023-08-26 14:31:13 +08:00
return;
}
2023-07-31 15:08:51 +08:00
2023-08-31 00:18:57 +08:00
const setting = await getSettingWithDefault();
2023-09-18 17:36:10 +08:00
const args = {
2023-09-18 15:45:32 +08:00
key: KV_SETTING_KEY,
value: setting,
syncUrl,
2023-09-18 17:36:10 +08:00
syncUser,
2023-08-26 14:31:13 +08:00
syncKey,
2023-09-18 15:45:32 +08:00
updateAt: settingUpdateAt,
syncAt: settingSyncAt,
isBg,
isForce,
2023-09-18 17:36:10 +08:00
};
const res =
syncType === OPT_SYNCTYPE_WEBDAV
? await syncByWebdav(args)
: await syncByWorker(args);
2023-08-17 13:27:22 +08:00
2023-09-09 19:26:22 +08:00
if (res.updateAt > settingUpdateAt) {
2023-08-31 00:18:57 +08:00
await setSetting(res.value);
2023-08-26 14:31:13 +08:00
}
2023-09-09 19:26:22 +08:00
await updateSync({
settingUpdateAt: res.updateAt,
settingSyncAt: Date.now(),
});
return res.value;
2023-08-26 14:31:13 +08:00
};
2023-09-11 11:33:28 +08:00
export const trySyncSetting = async (isBg = false, isForce = false) => {
2023-08-26 14:31:13 +08:00
try {
2023-09-11 11:33:28 +08:00
return await syncSetting(isBg, isForce);
2023-08-17 13:27:22 +08:00
} catch (err) {
console.log("[sync setting]", err);
2023-07-31 15:08:51 +08:00
}
};
/**
* 同步规则
* @returns
*/
2023-09-11 11:33:28 +08:00
const syncRules = async (isBg = false, isForce = false) => {
2023-09-18 15:45:32 +08:00
const {
syncType,
2023-09-11 11:33:28 +08:00
syncUrl,
2023-09-18 15:45:32 +08:00
syncUser,
2023-09-11 11:33:28 +08:00
syncKey,
rulesUpdateAt = 0,
rulesSyncAt = 0,
} = await getSyncWithDefault();
2023-09-18 15:45:32 +08:00
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
2023-08-26 14:31:13 +08:00
return;
}
2023-08-31 00:18:57 +08:00
const rules = await getRulesWithDefault();
2023-09-18 17:36:10 +08:00
const args = {
2023-09-18 15:45:32 +08:00
key: KV_RULES_KEY,
value: rules,
syncUrl,
2023-09-18 17:36:10 +08:00
syncUser,
2023-08-26 14:31:13 +08:00
syncKey,
2023-09-18 15:45:32 +08:00
updateAt: rulesUpdateAt,
syncAt: rulesSyncAt,
isBg,
isForce,
2023-09-18 17:36:10 +08:00
};
const res =
syncType === OPT_SYNCTYPE_WEBDAV
? await syncByWebdav(args)
: await syncByWorker(args);
2023-07-31 15:08:51 +08:00
2023-09-09 19:26:22 +08:00
if (res.updateAt > rulesUpdateAt) {
2023-08-31 00:18:57 +08:00
await setRules(res.value);
2023-08-26 14:31:13 +08:00
}
2023-09-09 19:26:22 +08:00
await updateSync({
rulesUpdateAt: res.updateAt,
rulesSyncAt: Date.now(),
});
return res.value;
2023-08-26 14:31:13 +08:00
};
2023-08-17 13:27:22 +08:00
2023-09-11 11:33:28 +08:00
export const trySyncRules = async (isBg = false, isForce = false) => {
2023-08-26 14:31:13 +08:00
try {
2023-09-11 11:33:28 +08:00
return await syncRules(isBg, isForce);
2023-08-17 13:27:22 +08:00
} catch (err) {
2023-08-21 21:31:20 +08:00
console.log("[sync user rules]", err);
2023-07-31 15:08:51 +08:00
}
};
/**
* 同步分享规则
* @param {*} param0
* @returns
*/
2023-08-20 23:30:08 +08:00
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
2023-09-18 17:36:10 +08:00
const args = {
2023-08-20 23:30:08 +08:00
key: KV_RULES_SHARE_KEY,
value: rules,
2023-09-18 15:45:32 +08:00
syncUrl,
syncKey,
2023-08-20 23:30:08 +08:00
updateAt: Date.now(),
2023-09-18 15:45:32 +08:00
syncAt: Date.now(),
2023-09-18 17:36:10 +08:00
};
await syncByWorker(args);
2023-08-20 23:30:08 +08:00
const psk = await sha256(syncKey, KV_SALT_SHARE);
2023-09-18 13:28:36 +08:00
const shareUrl = `${syncUrl}/rules?psk=${psk}`;
2023-08-20 23:30:08 +08:00
return shareUrl;
};
/**
* 同步个人设置和规则
* @returns
*/
2023-08-31 13:38:06 +08:00
export const syncSettingAndRules = async (isBg = false) => {
2023-09-01 10:15:57 +08:00
return [await syncSetting(isBg), await syncRules(isBg)];
2023-07-31 15:08:51 +08:00
};
2023-08-26 14:31:13 +08:00
2023-08-31 13:38:06 +08:00
export const trySyncSettingAndRules = async (isBg = false) => {
2023-09-01 10:15:57 +08:00
return [await trySyncSetting(isBg), await trySyncRules(isBg)];
2023-08-26 14:31:13 +08:00
};