|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/fxamacker/cbor/v2" |
| 13 | + "github.com/xmit-co/xmit/protocol" |
| 14 | + "github.com/zeebo/blake3" |
| 15 | +) |
| 16 | + |
| 17 | +func download(key, domain, id, destination string) error { |
| 18 | + downloadParallelism := 3 |
| 19 | + if s := os.Getenv("DOWNLOAD_PARALLELISM"); s != "" { |
| 20 | + if v, err := strconv.Atoi(s); err == nil { |
| 21 | + downloadParallelism = v |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Discover endpoint |
| 26 | + log.Print("🔍 Discovering endpoint…") |
| 27 | + discovery, err := protocol.Discover() |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("discovering endpoint: %w", err) |
| 30 | + } |
| 31 | + log.Printf("🌐 Using URL: %s", discovery.URL) |
| 32 | + |
| 33 | + downloader, err := protocol.NewParallelDownloader(discovery.URL, downloadParallelism) |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("creating parallel downloader: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + resp, err := downloader.DownloadBundle(key, domain, id) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("downloading bundle: %w", err) |
| 41 | + } |
| 42 | + if !resp.Response.Success { |
| 43 | + return fmt.Errorf("downloading bundle, server-side: %v", resp.Response.Errors) |
| 44 | + } |
| 45 | + var node protocol.Node |
| 46 | + if err := cbor.NewDecoder(bytes.NewReader(resp.Bundle)).Decode(&node); err != nil { |
| 47 | + return fmt.Errorf("unmarshaling bundle: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + return downloadTraversal(downloader, key, domain, &node, destination) |
| 51 | +} |
| 52 | + |
| 53 | +func downloadTraversal(downloader *protocol.ParallelDownloader, key, domain string, node *protocol.Node, destination string) error { |
| 54 | + if node.Hash != nil { |
| 55 | + hash := *node.Hash |
| 56 | + if b, err := os.ReadFile(destination); err == nil { |
| 57 | + h2 := protocol.Hash(blake3.Sum256(b)) |
| 58 | + if bytes.Equal(h2[:], hash[:]) { |
| 59 | + return nil |
| 60 | + } |
| 61 | + } |
| 62 | + log.Printf("🎁 Downloading %s", destination) |
| 63 | + resp, err := downloader.DownloadParts(key, domain, []protocol.Hash{hash}) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("downloading part: %w", err) |
| 66 | + } |
| 67 | + if !resp.Response.Success { |
| 68 | + return fmt.Errorf("downloading part, server-side: %v", resp.Response.Errors) |
| 69 | + } |
| 70 | + if len(resp.Parts) == 0 { |
| 71 | + return fmt.Errorf("no part found for %s", hash) |
| 72 | + } |
| 73 | + if err := os.WriteFile(destination, resp.Parts[0], 0644); err != nil { |
| 74 | + return fmt.Errorf("writing file: %w", err) |
| 75 | + } |
| 76 | + log.Printf("✅ Downloaded %s", destination) |
| 77 | + } else { |
| 78 | + if err := os.MkdirAll(destination, 0755); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + var errors []error |
| 82 | + var mu sync.Mutex |
| 83 | + var wg sync.WaitGroup |
| 84 | + for name, child := range node.Children { |
| 85 | + wg.Add(1) |
| 86 | + go func() { |
| 87 | + defer wg.Done() |
| 88 | + if err := downloadTraversal(downloader, key, domain, child, filepath.Join(destination, name)); err != nil { |
| 89 | + mu.Lock() |
| 90 | + errors = append(errors, err) |
| 91 | + mu.Unlock() |
| 92 | + } |
| 93 | + }() |
| 94 | + } |
| 95 | + wg.Wait() |
| 96 | + if len(errors) > 0 { |
| 97 | + for _, err := range errors { |
| 98 | + log.Printf("🛑 Failed to download: %v", err) |
| 99 | + } |
| 100 | + return fmt.Errorf("%d subtraversals failed", len(errors)) |
| 101 | + } |
| 102 | + } |
| 103 | + return nil |
| 104 | +} |
0 commit comments