@@ -93,22 +93,35 @@ export async function syncFromWebDAV(): Promise<void> {
9393
9494 console . log ( `[WebDAV] Downloaded size: ${ ( data . length / 1024 ) . toFixed ( 2 ) } KB` )
9595
96- // 检查当前 localStorage 使用情况
97- let currentSize = 0
98- for ( const key in localStorage ) {
99- if ( localStorage . hasOwnProperty ( key ) )
100- currentSize += ( localStorage [ key ] ?. length || 0 ) + key . length
101- }
102- console . log ( `[WebDAV] Current localStorage: ${ ( currentSize / 1024 ) . toFixed ( 2 ) } KB` )
103-
10496 try {
97+ // 先尝试直接保存
10598 localStorage . setItem ( 'chatStorage' , data )
10699 console . log ( '[WebDAV] Saved successfully' )
107100 }
108101 catch ( error : any ) {
109102 console . error ( '[WebDAV] Save error:' , error )
110- if ( error . name === 'QuotaExceededError' )
111- throw new Error ( `Safari存储空间不足 (需要${ ( data . length / 1024 ) . toFixed ( 1 ) } KB)。请在Safari设置中清除网站数据` )
112- throw new Error ( `保存失败: ${ error . message } ` )
103+
104+ if ( error . name === 'QuotaExceededError' ) {
105+ // Safari 配额不足,尝试清理后再保存
106+ console . log ( '[WebDAV] Attempting to clear old data and retry...' )
107+
108+ try {
109+ // 保留配置,清除聊天数据
110+ const oldConfig = localStorage . getItem ( WEBDAV_CONFIG_KEY )
111+ localStorage . clear ( )
112+ if ( oldConfig )
113+ localStorage . setItem ( WEBDAV_CONFIG_KEY , oldConfig )
114+
115+ // 重试保存
116+ localStorage . setItem ( 'chatStorage' , data )
117+ console . log ( '[WebDAV] Saved after clearing old data' )
118+ }
119+ catch ( retryError : any ) {
120+ throw new Error ( 'Safari存储空间不足。请在Safari设置 > 隐私 > 管理网站数据中清除本站数据,或使用Chrome/Edge浏览器' )
121+ }
122+ }
123+ else {
124+ throw new Error ( `保存失败: ${ error . message } ` )
125+ }
113126 }
114127}
0 commit comments