// Writes to w, a metainfo containing the file at name. func CreateMetaInfo(name string, w io.Writer) { builder := metainfo.Builder{} builder.AddFile(name) builder.AddAnnounceGroup([]string{"lol://cheezburger"}) builder.SetPieceLength(5) batch, err := builder.Submit() if err != nil { panic(err) } errs, _ := batch.Start(w, 1) <-errs }
func (v Video) createTorrent() error { log.Println("creating torrent") f, err := os.Create(v.torrentFilename()) defer f.Close() if err != nil { return err } b := torrent.Builder{} b.AddFile(v.OutputFilename) for _, group := range builtinAnnounceList { b.AddAnnounceGroup(group) } batch, err := b.Submit() if err != nil { log.Fatal(err) } errs, status := batch.Start(f, runtime.NumCPU()) lastProgress := int64(-1) for { select { case err, ok := <-errs: if !ok || err == nil { return err } log.Print(err) case bytesDone := <-status: progress := 100 * bytesDone / batch.TotalSize() if progress != lastProgress { log.Printf("%d%%", progress) lastProgress = progress } } } }
func TestInfohash(t *testing.T) { os.RemoveAll(filepath.Join(os.TempDir(), "torrent-test")) os.MkdirAll(filepath.Join(os.TempDir(), "torrent-test"), 0700) var cl_one *Client var cf_one *Config var err error if err != nil { t.Fatal(err) } cf_one = testingConfig() cf_one.ListenAddr = "localhost:43433" cf_one = addirs(cf_one) cl_one, err = NewClient(cf_one) if err != nil { t.Fatal(err) } tfp := filepath.Join(cf_one.DataDir, "testdata") writeranddata(tfp) b := metainfo.Builder{} b.AddFile(tfp) b.AddDhtNodes([]string{fmt.Sprintf(cl_one.DHT().ID())}) ba, err := b.Submit() if err != nil { t.Fatal(err) } ttfp := filepath.Join(cf_one.ConfigDir, "/../torrent") ttf, err := os.Create(ttfp) if err != nil { t.Fatal(err) } ec, _ := ba.Start(ttf, runtime.NumCPU()) err = <-ec if err != nil { t.Fatal(err) } ttf.Close() tor, err := cl_one.AddTorrentFromFile(ttfp) if err != nil { t.Fatal(err) } <-tor.GotInfo() tor.DownloadAll() if cl_one.WaitAll() == false { t.Fatal(errors.New("One did not download torrent")) } }
func main() { b := torrent.Builder{} for _, filename := range flag.Args() { if err := filepath.Walk(filename, func(path string, info os.FileInfo, err error) error { if _, err := os.Stat(path); os.IsNotExist(err) { return err } log.Print(path) if info.IsDir() { return nil } b.AddFile(path) return nil }); err != nil { log.Print(err) } } for _, group := range builtinAnnounceList { b.AddAnnounceGroup(group) } batch, err := b.Submit() if err != nil { log.Fatal(err) } errs, status := batch.Start(os.Stdout, runtime.NumCPU()) lastProgress := int64(-1) for { select { case err, ok := <-errs: if !ok || err == nil { return } log.Print(err) case bytesDone := <-status: progress := 100 * bytesDone / batch.TotalSize() if progress != lastProgress { log.Printf("%d%%", progress) lastProgress = progress } } } }
func (b *Builtin) CreateTorrent(path, torrentPath string, trackers []string) (err error) { builder := metainfo.Builder{} builder.AddFile(path) builder.AddAnnounceGroup(trackers) batch, err := builder.Submit() if err != nil { return } torrentFile, err := os.Create(torrentPath) if err != nil { return } defer torrentFile.Close() errs, _ := batch.Start(torrentFile, runtime.NumCPU()) err = <-errs if err != nil { return } return }