fix: 修复webdav自动定时同步异常的问题 (#1921)
This commit is contained in:
5 files changed
+271
-52
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import { onMounted, onUnmounted } from "vue";
|
||||
import { useSettingsStore } from "@/stores/settingsStore";
|
||||
import { appendDebugLog } from "@/lib/debugLog";
|
||||
import { webdavSyncUpload } from "@/lib/api";
|
||||
import { readWebDavAutoUploadConfig, WEB_DAV_AUTO_UPLOAD_STORAGE_KEYS } from "@/lib/webdavAutoUploadConfig";
|
||||
|
||||
export function useWebDavAutoUpload() {
|
||||
const settingsStore = useSettingsStore();
|
||||
let timer: ReturnType<typeof window.setInterval> | undefined;
|
||||
let uploading = false;
|
||||
|
||||
function clearTimer() {
|
||||
if (!timer) return;
|
||||
window.clearInterval(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
|
||||
function schedule() {
|
||||
clearTimer();
|
||||
const config = readWebDavAutoUploadConfig();
|
||||
if (!config.enabled || !config.webDavConfig) return;
|
||||
|
||||
timer = window.setInterval(() => {
|
||||
void runAutoUpload();
|
||||
}, config.intervalMinutes * 60_000);
|
||||
}
|
||||
|
||||
async function runAutoUpload() {
|
||||
if (uploading) return;
|
||||
const config = readWebDavAutoUploadConfig();
|
||||
if (!config.enabled || !config.webDavConfig) return;
|
||||
|
||||
uploading = true;
|
||||
try {
|
||||
const summary = await webdavSyncUpload(config.webDavConfig, settingsStore.editorSettings);
|
||||
appendDebugLog("info", "[DBX][webdav:auto-upload:success]", {
|
||||
bytes: summary.bytes,
|
||||
remotePath: summary.remotePath,
|
||||
exportedAt: summary.exportedAt,
|
||||
});
|
||||
} catch (error) {
|
||||
appendDebugLog("error", "[DBX][webdav:auto-upload:error]", error);
|
||||
} finally {
|
||||
uploading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onStorage(event: StorageEvent) {
|
||||
if (event.key && !WEB_DAV_AUTO_UPLOAD_STORAGE_KEYS.includes(event.key as (typeof WEB_DAV_AUTO_UPLOAD_STORAGE_KEYS)[number])) return;
|
||||
schedule();
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
schedule();
|
||||
window.addEventListener("storage", onStorage);
|
||||
window.addEventListener("dbx:webdav-auto-upload-config-changed", schedule);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
clearTimer();
|
||||
window.removeEventListener("storage", onStorage);
|
||||
window.removeEventListener("dbx:webdav-auto-upload-config-changed", schedule);
|
||||
});
|
||||
|
||||
return {
|
||||
scheduleWebDavAutoUpload: schedule,
|
||||
runWebDavAutoUpload: runAutoUpload,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user