Skip to content

fix(backup): [dependent on #240] resolve cross-platform path issues and restore crash#287

Closed
luosc wants to merge 1 commit intoChevey339:masterfrom
luosc:fix/backup-restore-image-path
Closed

fix(backup): [dependent on #240] resolve cross-platform path issues and restore crash#287
luosc wants to merge 1 commit intoChevey339:masterfrom
luosc:fix/backup-restore-image-path

Conversation

@luosc
Copy link
Copy Markdown
Contributor

@luosc luosc commented Jan 25, 2026

Description

This PR addresses cross-platform image path compatibility issues when restoring backups on different devices (e.g., Windows User A to Windows User B, or PC to Mobile). It ensures that file paths are portable and correctly resolved regardless of the environment.

Additionally, it fixes a crash in the Desktop backup pane that occurred during long restore operations.

Fixes #243
Depends on #240 (Granular backup/restore)

Key Changes

  1. Cross-Platform Path Resolution (data_sync.dart):

    • Export: Now converts absolute paths to portable relative paths (e.g., upload/img.jpg) before saving to the backup. It uses aggressive directory matching to correctly handle iOS/macOS symlinks.
    • Import:
      • Automatically expands relative paths to the current device's absolute path structure.
      • Legacy Support: Heals existing backups containing absolute paths by using SandboxPathResolver with a fallback mechanism to locate files in standard subfolders.
  2. Stability Improvements (backup_pane.dart):

    • Fixed a Widget unmounted crash in DesktopBackupPane by adding !mounted checks during asynchronous restore operations.
  3. Initialization:

    • Added SandboxPathResolver initialization to the restore flow to ensure path helpers are ready before processing files.

描述

本 PR 解决了在不同设备间(例如 Windows 用户 A 到 Windows 用户 B,或电脑到手机)恢复备份时,图片路径不兼容的问题。它确保文件路径具有可移植性,并在不同环境下都能被正确解析。

此外,修复了桌面端在长时间恢复操作过程中可能发生的崩溃问题。

修复 #243
依赖 #240 (颗粒化备份/恢复)

主要变更

  1. 跨平台路径解析 (data_sync.dart):

    • 导出: 在保存备份前,将绝对路径转换为可移植的相对路径(例如 upload/img.jpg)。采用了更激进的目录匹配策略,以正确处理 iOS/macOS 的符号链接。
    • 导入:
      • 自动将相对路径扩展为当前设备的绝对路径结构。
      • 遗留支持: 使用 SandboxPathResolver 修复包含绝对路径的旧备份,并通过回退机制在标准子目录中查找文件。
  2. 稳定性改进 (backup_pane.dart):

    • DesktopBackupPane 的异步恢复操作中添加了 !mounted 检查,修复了 Widget unmounted 导致的崩溃。
  3. 初始化:

    • 在恢复流程中添加了 SandboxPathResolver 的初始化,确保在处理文件前路径助手已准备就绪。

 Chevey339#243)

- Fix 'PathNotFoundException' by adding robust path resolution logic:
  - Export: Convert absolute paths to portable relative paths (e.g. 'upload/img.jpg') using aggressive directory matching to handle iOS/macOS symlinks.
  - Import: Expand relative paths to current device's absolute path.
  - Import: Heal legacy absolute paths (from old backups) using 'SandboxPathResolver' with a generic fallback for generic subfolders.
- Fix 'Widget unmounted' crash in 'DesktopBackupPane' during long restore operations by checking '!mounted'.
- Add 'SandboxPathResolver' initialization to restore flow.
@luosc luosc changed the title fix(backup): resolve cross-platform path issues and restore crash (Fi… fix(backup): [dependent on #240] resolve cross-platform path issues and restore crash Jan 25, 2026
@luosc
Copy link
Copy Markdown
Contributor Author

luosc commented Jan 26, 2026

@jacob-sheng
Copy link
Copy Markdown
Contributor

支持,现有备份在不同设备间会丢失一些照片之类的插入文件。

@luosc
Copy link
Copy Markdown
Contributor Author

luosc commented Mar 3, 2026

“往后退一步,只改还原(Restore)逻辑,不改备份(Export)逻辑”的想法,从系统设计的角度来看,这种做法的核心优势在于**“数据无损性(Non-Destructive)和备份安全性”**。

为什么只改“还原逻辑”更好?

  1. 消除永久性数据损坏的风险(最重要的一点)

    • 如果改导出逻辑:我们在导出时使用正则修改文本。如果正则在某些罕见的边界情况下出错(比如文件名中包含特殊字符,或者用户发了某些不符合预期的文本),生成的备份文件就会被永久性“污染”或损坏。如果用户依赖这个损坏的备份并清空了原设备,数据就永远丢失了。
    • 如果只改还原逻辑:导出时,我们对数据库内容进行“原样 Dump”(保留绝对路径)。所有的跨平台转换(剥离旧前缀、拼接新前缀)都放在导入/还原时进行。如果我们在还原时的解析逻辑出了 bug(比如没认出 Windows 路径),图片可能暂时裂开,但用户的备份文件本身是原封不动、完全健康的! 我们只需要在下个版本发个热修复,用户重新导入一次就能完美恢复。
  2. 符合序列化最佳实践

    • 备份的本质是当前状态的快照(Snapshot)。强行在制作快照时引入业务转换逻辑是不推荐的。“忠实记录(Dump as-is),智能解析(Transform on deserialize)”会极大地降低系统的脆弱性。
  3. 代码侵入性更低,不再需要“Legacy 警告”

    • 如果我们不在导出时转换路径,那么所有的备份文件默认都会带有绝对路径
    • 这样一来,你在 commit 99ad16a 中加入的 Legacy Backup Warning 就可以去掉了。系统只需要安静地、在后台通过 SandboxPathResolver 把所有跨平台或者跨容器的绝对路径自适应映射到当前平台即可,完全对用户透明。

实施(Action Plan)

  1. 撤销/精简导出时的逻辑:在 DataSync.exportToFile 中,移除把绝对路径转换为相对路径的 makeRelative 逻辑和相关正则替换。保持原样导出。
  2. 保留并信赖还原时的逻辑:保留 DataSync._restoreFromBackupFile 中的 resolvePath 以及 SandboxPathResolver.fix(path) 逻辑。目前的 SandboxPathResolver 的 fallback 机制(查找 /upload/, /images/, /avatars/)已经足够强大,能完美应付 iOS、Android、Windows 等各平台的路径漂移。
  3. 移除旧版备份警告:撤销 99ad16a 提交(移除 legacyBackupDetected 相关的 Snackbar),把包含绝对路径的备份视为“标准行为”,由底层默默处理。

总结:退回一步,只在**读取(导入)时做兼容,而不在写入(导出)**时做变异,是目前保证用户数据绝对安全、且又能完美解决 #243 跨平台恢复痛点的最优解!

@luosc
Copy link
Copy Markdown
Contributor Author

luosc commented Mar 3, 2026

fixed by ca3299c dc40484

@luosc luosc closed this Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

透过WebDAV在不同主机还原后, 图片路径无法对应到另一台主机

2 participants