]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
Move TorrentSpec stuff into its own file
[btrtrc.git] / spec.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/torrent/metainfo"
5         "github.com/anacrolix/torrent/storage"
6 )
7
8 // Specifies a new torrent for adding to a client. There are helpers for
9 // magnet URIs and torrent metainfo files.
10 type TorrentSpec struct {
11         // The tiered tracker URIs.
12         Trackers  [][]string
13         InfoHash  metainfo.Hash
14         InfoBytes []byte
15         // The name to use if the Name field from the Info isn't available.
16         DisplayName string
17         // The chunk size to use for outbound requests. Defaults to 16KiB if not
18         // set.
19         ChunkSize int
20         Storage   storage.ClientImpl
21 }
22
23 func TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error) {
24         m, err := metainfo.ParseMagnetURI(uri)
25         if err != nil {
26                 return
27         }
28         spec = &TorrentSpec{
29                 Trackers:    [][]string{m.Trackers},
30                 DisplayName: m.DisplayName,
31                 InfoHash:    m.InfoHash,
32         }
33         return
34 }
35
36 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) {
37         info := mi.UnmarshalInfo()
38         spec = &TorrentSpec{
39                 Trackers:    mi.AnnounceList,
40                 InfoBytes:   mi.InfoBytes,
41                 DisplayName: info.Name,
42                 InfoHash:    mi.HashInfoBytes(),
43         }
44         if spec.Trackers == nil && mi.Announce != "" {
45                 spec.Trackers = [][]string{{mi.Announce}}
46         }
47         return
48 }