| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/sylvinus/agent-vm/internal/host" |
| 10 | "github.com/sylvinus/agent-vm/internal/remotehost" |
| 11 | ) |
| 12 | |
| 13 | func importDesktopConnections(full, entry string) error { |
| 14 | old := []string{full, filepath.Join(full, "client")} |
| 15 | if err := remotehost.NewStore(entry).ImportOnce(old...); err != nil { |
| 16 | return err |
| 17 | } |
| 18 | marker := filepath.Join(entry, "downloads-imported.json") |
| 19 | if err := host.CheckPrivateFile(marker); err == nil { |
| 20 | return nil |
| 21 | } else if !os.IsNotExist(err) { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | |
| 26 | for _, profile := range old { |
| 27 | root := filepath.Join(profile, "remote-downloads") |
| 28 | info, err := os.Lstat(root) |
| 29 | if os.IsNotExist(err) { |
| 30 | continue |
| 31 | } |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | if !info.IsDir() { |
| 36 | return errors.New("旧入口下载记录目录无效") |
| 37 | } |
| 38 | instances, err := os.ReadDir(root) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | for _, instance := range instances { |
| 43 | if !instance.IsDir() || !remoteInstanceID.MatchString(instance.Name()) { |
| 44 | continue |
| 45 | } |
| 46 | from := filepath.Join(root, instance.Name()) |
| 47 | files, err := os.ReadDir(filepath.Join(from, "browser-downloads")) |
| 48 | if os.IsNotExist(err) { |
| 49 | continue |
| 50 | } |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | to := filepath.Join(entry, "remote-downloads", instance.Name()) |
| 55 | for _, file := range files { |
| 56 | id := strings.TrimSuffix(file.Name(), ".json") |
| 57 | if !file.Type().IsRegular() || !browserFileID.MatchString(id) { |
| 58 | continue |
| 59 | } |
| 60 | record, err := readSavedDownload(from, id) |
| 61 | if err != nil || record.InstanceID != instance.Name() { |
| 62 | continue |
| 63 | } |
| 64 | current, err := readSavedDownload(to, id) |
| 65 | if err == nil && !record.SavedAt.After(current.SavedAt) { |
| 66 | continue |
| 67 | } |
| 68 | if err != nil && !os.IsNotExist(err) { |
| 69 | return err |
| 70 | } |
| 71 | if err = writePrivate(savedDownloadPath(to, id), record); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return writePrivate(marker, struct{}{}) |
| 78 | } |