-
Notifications
You must be signed in to change notification settings - Fork 911
Expand file tree
/
Copy pathwebdav.ts
More file actions
187 lines (146 loc) · 4.28 KB
/
webdav.ts
File metadata and controls
187 lines (146 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { create } from 'zustand'
import { invoke } from '@tauri-apps/api/core'
export enum WebDAVConnectionState {
checking = 'checking',
success = 'success',
fail = 'failed',
}
interface WebDAVState {
url: string
setUrl: (url: string) => Promise<void>
username: string
setUsername: (username: string) => Promise<void>
password: string
setPassword: (password: string) => Promise<void>
path: string
setPath: (path: string) => Promise<void>
createWebDAVDir: (path: string) => Promise<void>
connectionState: WebDAVConnectionState
setConnectionState: (state: WebDAVConnectionState) => void
testConnection: () => Promise<boolean>
initWebDAVData: () => Promise<void>
backupState: boolean
setBackupState: (state: boolean) => void
syncState: boolean
setSyncState: (state: boolean) => void
backupToWebDAV: () => Promise<string>
syncFromWebDAV: () => Promise<string>
}
const useWebDAVStore = create<WebDAVState>((set, get) => {
let isTestingInProgress = false
let shouldTestAgain = false
const performConnectionTest = async (): Promise<boolean> => {
// 防止重复执行
if (isTestingInProgress) {
shouldTestAgain = true
return false
}
const { url, username, password, path } = get()
// 检查配置完整性
if (!url?.trim() || !username?.trim() || !password?.trim()) {
set({ connectionState: WebDAVConnectionState.fail })
return false
}
isTestingInProgress = true
set({ connectionState: WebDAVConnectionState.checking })
try {
const result = await invoke<boolean>('webdav_test', {
url,
username,
password,
path
})
set({
connectionState: result ? WebDAVConnectionState.success : WebDAVConnectionState.fail
})
return result
} catch (_error) {
console.error('Failed to test connection:', _error)
set({ connectionState: WebDAVConnectionState.fail })
return false
} finally {
isTestingInProgress = false
// 如果在测试期间有新的测试请求,再次执行
if (shouldTestAgain) {
shouldTestAgain = false
setTimeout(() => performConnectionTest(), 100)
}
}
}
return {
url: '',
setUrl: async (url: string) => {
set({ url })
},
username: '',
setUsername: async (username: string) => {
set({ username })
},
password: '',
setPassword: async (password: string) => {
set({ password })
},
path: '',
setPath: async (path: string) => {
set({ path })
},
connectionState: WebDAVConnectionState.fail,
setConnectionState: (connectionState) => {
set({ connectionState })
},
testConnection: async () => {
return performConnectionTest()
},
initWebDAVData: async () => {
// 不再从本地存储加载连接参数,也不做自动连测
set({ url: '', username: '', password: '', path: '' })
},
backupState: false,
setBackupState: (state: boolean) => {
set({ backupState: state })
},
syncState: false,
setSyncState: (state: boolean) => {
set({ syncState: state })
},
backupToWebDAV: async () => {
const { url, username, password, path, backupState } = get()
if (backupState) {
throw new Error('Backup is already in progress')
}
set({ backupState: true })
try {
return await invoke<string>('webdav_backup', {
url, username, password, path
})
} finally {
set({ backupState: false })
}
},
syncFromWebDAV: async () => {
const { url, username, password, path, syncState } = get()
if (syncState) {
throw new Error('Sync is already in progress')
}
set({ syncState: true })
try {
return await invoke<string>('webdav_sync', {
url, username, password, path
})
} finally {
set({ syncState: false })
}
},
//创建WebDAVD目录
createWebDAVDir: async (dirPath: string) => {
const { url, username, password } = get()
if (!url || !username || !password) {
throw new Error('WebDAV connection parameters are incomplete')
}
await invoke('webdav_create_dir', {
url, username, password, path: dirPath
})
}
}
})
export default useWebDAVStore