]> Sergey Matveev's repositories - btrtrc.git/commitdiff
cmd/torrent serve: Support multiple file paths
authorMatt Joiner <anacrolix@gmail.com>
Sun, 3 Jul 2022 11:46:28 +0000 (21:46 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 12 Jul 2022 06:12:02 +0000 (16:12 +1000)
cmd/torrent/serve.go

index cf6d483f9ff03864ecc8b3f46af580f0e89dcf40..0d3487d4355f407a4827802841a2a030e54abae2 100644 (file)
@@ -14,9 +14,9 @@ import (
 )
 
 func serve() (cmd bargle.Command) {
-       var filePath string
+       var filePaths []string
        cmd.Positionals = append(cmd.Positionals, &bargle.Positional{
-               Value: &bargle.String{Target: &filePath},
+               Value: bargle.AutoUnmarshaler(&filePaths),
        })
        cmd.Desc = "creates and seeds a torrent from a filepath"
        cmd.DefaultAction = func() error {
@@ -30,56 +30,58 @@ func serve() (cmd bargle.Command) {
                http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
                        cl.WriteStatus(w)
                })
-               totalLength, err := totalLength(filePath)
-               if err != nil {
-                       return fmt.Errorf("calculating total length of %q: %v", filePath, err)
-               }
-               pieceLength := metainfo.ChoosePieceLength(totalLength)
-               info := metainfo.Info{
-                       PieceLength: pieceLength,
-               }
-               err = info.BuildFromFilePath(filePath)
-               if err != nil {
-                       return fmt.Errorf("building info from path %q: %w", filePath, err)
-               }
-               for _, fi := range info.Files {
-                       log.Printf("added %q", fi.Path)
-               }
-               mi := metainfo.MetaInfo{
-                       InfoBytes: bencode.MustMarshal(info),
-               }
-               pc, err := storage.NewDefaultPieceCompletionForDir(".")
-               if err != nil {
-                       return fmt.Errorf("new piece completion: %w", err)
-               }
-               defer pc.Close()
-               ih := mi.HashInfoBytes()
-               to, _ := cl.AddTorrentOpt(torrent.AddTorrentOpts{
-                       InfoHash: ih,
-                       Storage: storage.NewFileOpts(storage.NewFileClientOpts{
-                               ClientBaseDir: filePath,
-                               FilePathMaker: func(opts storage.FilePathMakerOpts) string {
-                                       return filepath.Join(opts.File.Path...)
-                               },
-                               TorrentDirMaker: nil,
-                               PieceCompletion: pc,
-                       }),
-               })
-               defer to.Drop()
-               err = to.MergeSpec(&torrent.TorrentSpec{
-                       InfoBytes: mi.InfoBytes,
-                       Trackers: [][]string{{
-                               `wss://tracker.btorrent.xyz`,
-                               `wss://tracker.openwebtorrent.com`,
-                               "http://p4p.arenabg.com:1337/announce",
-                               "udp://tracker.opentrackr.org:1337/announce",
-                               "udp://tracker.openbittorrent.com:6969/announce",
-                       }},
-               })
-               if err != nil {
-                       return fmt.Errorf("setting trackers: %w", err)
+               for _, filePath := range filePaths {
+                       totalLength, err := totalLength(filePath)
+                       if err != nil {
+                               return fmt.Errorf("calculating total length of %q: %v", filePath, err)
+                       }
+                       pieceLength := metainfo.ChoosePieceLength(totalLength)
+                       info := metainfo.Info{
+                               PieceLength: pieceLength,
+                       }
+                       err = info.BuildFromFilePath(filePath)
+                       if err != nil {
+                               return fmt.Errorf("building info from path %q: %w", filePath, err)
+                       }
+                       for _, fi := range info.Files {
+                               log.Printf("added %q", fi.Path)
+                       }
+                       mi := metainfo.MetaInfo{
+                               InfoBytes: bencode.MustMarshal(info),
+                       }
+                       pc, err := storage.NewDefaultPieceCompletionForDir(".")
+                       if err != nil {
+                               return fmt.Errorf("new piece completion: %w", err)
+                       }
+                       defer pc.Close()
+                       ih := mi.HashInfoBytes()
+                       to, _ := cl.AddTorrentOpt(torrent.AddTorrentOpts{
+                               InfoHash: ih,
+                               Storage: storage.NewFileOpts(storage.NewFileClientOpts{
+                                       ClientBaseDir: filePath,
+                                       FilePathMaker: func(opts storage.FilePathMakerOpts) string {
+                                               return filepath.Join(opts.File.Path...)
+                                       },
+                                       TorrentDirMaker: nil,
+                                       PieceCompletion: pc,
+                               }),
+                       })
+                       defer to.Drop()
+                       err = to.MergeSpec(&torrent.TorrentSpec{
+                               InfoBytes: mi.InfoBytes,
+                               Trackers: [][]string{{
+                                       `wss://tracker.btorrent.xyz`,
+                                       `wss://tracker.openwebtorrent.com`,
+                                       "http://p4p.arenabg.com:1337/announce",
+                                       "udp://tracker.opentrackr.org:1337/announce",
+                                       "udp://tracker.openbittorrent.com:6969/announce",
+                               }},
+                       })
+                       if err != nil {
+                               return fmt.Errorf("setting trackers: %w", err)
+                       }
+                       fmt.Printf("%v: %v\n", to, to.Metainfo().Magnet(&ih, &info))
                }
-               fmt.Printf("%v: %v\n", to, to.Metainfo().Magnet(&ih, &info))
                select {}
        }
        return