metainfo

package
v0.0.0-...-8135c48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2016 License: MPL-2.0, MIT Imports: 18 Imported by: 0

README

A library for manipulating ".torrent" files.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch struct {
	// contains filtered or unexported fields
}

Batch represents a snapshot of a builder state, ready for transforming it into a torrent file. Note that Batch contains two accessor methods you might be interested in. The TotalSize is the total size of all the files queued for hashing, you will use it for status reporting. The DefaultName is an automatically determined name of the torrent metainfo, you might want to use it for naming the .torrent file itself.

func (*Batch) DefaultName

func (b *Batch) DefaultName() string

Get an automatically determined name of the future torrent metainfo. You can use it for a .torrent file in case user hasn't provided it specifically.

func (*Batch) Start

func (b *Batch) Start(w io.Writer, nworkers int) (<-chan error, <-chan int64)

Starts a process of building the torrent file. This function does everything in a separate goroutine and uses up to 'nworkers' of goroutines to perform SHA1 hashing. Therefore it will return almost immedately. It returns two channels, the first one is for completion awaiting, the second one is for getting status reports. Status report is a number of bytes hashed, you can get the total amount of bytes by inspecting the Batch.TotalSize method return value.

func (*Batch) TotalSize

func (b *Batch) TotalSize() int64

Get a total size of all the files queued for hashing. Useful in conjunction with status reports.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

The Builder type is responsible for .torrent files construction. Just instantiate it, call necessary methods and then call the .Build method. While waiting for completion you can use 'status' channel to get status reports.

func (*Builder) AddAnnounceGroup

func (b *Builder) AddAnnounceGroup(group []string)

Add announce URL group. TODO: better explanation.

func (*Builder) AddDhtNodes

func (b *Builder) AddDhtNodes(group []string)

Add DHT nodes URLs for trackerless mode

func (*Builder) AddFile

func (b *Builder) AddFile(filename string)

Adds a file to the builder queue. You may add one or more files.

func (*Builder) AddWebSeedURL

func (b *Builder) AddWebSeedURL(url string)

Add WebSeed URL to the list.

func (*Builder) SetComment

func (b *Builder) SetComment(comment string)

Sets the comment. The default is no comment.

func (*Builder) SetCreatedBy

func (b *Builder) SetCreatedBy(createdby string)

Sets the "created by" parameter. The default is "libtorgo".

func (*Builder) SetCreationDate

func (b *Builder) SetCreationDate(date time.Time)

Sets creation date. The default is time.Now() when the .Build method was called.

func (*Builder) SetEncoding

func (b *Builder) SetEncoding(encoding string)

Sets the "encoding" parameter. The default is "UTF-8".

func (*Builder) SetName

func (b *Builder) SetName(name string)

Defines a name of the future torrent file. For single file torrents it's the recommended name of the contained file. For multiple files torrents it's the recommended name of the directory in which all of them will be stored. Calling this function is not required. In case if no name was specified, the builder will try to automatically assign it. It will use the name of the file if there is only one file in the queue or it will try to find the rightmost common directory of all the queued files and use its name as a torrent name. In case if name cannot be assigned automatically, it will use "unknown" as a torrent name.

func (*Builder) SetPieceLength

func (b *Builder) SetPieceLength(length int64)

Sets the length of a piece in the torrent file in bytes. The default is 256kb.

func (*Builder) SetPrivate

func (b *Builder) SetPrivate(v bool)

Sets the "private" flag. The default is false.

func (*Builder) Submit

func (b *Builder) Submit() (*Batch, error)

Finalizes the Builder state and makes a Batch out of it. After calling that method, Builder becomes empty and you can use it to create another Batch if you will.

type FileInfo

type FileInfo struct {
	Length int64    `bencode:"length"`
	Path   []string `bencode:"path"`
}

Information specific to a single file inside the MetaInfo structure.

type Hash

type Hash [20]byte

20-byte SHA1 hash used for info and pieces.

func (*Hash) AsString

func (ih *Hash) AsString() string

func (Hash) Bytes

func (me Hash) Bytes() []byte

func (Hash) HexString

func (ih Hash) HexString() string

type Info

type Info struct {
	PieceLength int64      `bencode:"piece length"`
	Pieces      []byte     `bencode:"pieces"`
	Name        string     `bencode:"name"`
	Length      int64      `bencode:"length,omitempty"`
	Private     bool       `bencode:"private,omitempty"`
	Files       []FileInfo `bencode:"files,omitempty"`
}

The info dictionary.

func (*Info) BuildFromFilePath

func (info *Info) BuildFromFilePath(root string) (err error)

func (*Info) GeneratePieces

func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error

Set info.Pieces by hashing info.Files.

func (*Info) IsDir

func (i *Info) IsDir() bool

func (*Info) NumPieces

func (me *Info) NumPieces() int

func (*Info) TotalLength

func (me *Info) TotalLength() (ret int64)

func (*Info) UpvertedFiles

func (i *Info) UpvertedFiles() []FileInfo

The files field, converted up from the old single-file in the parent info dict if necessary. This is a helper to avoid having to conditionally handle single and multi-file torrent infos.

type InfoEx

type InfoEx struct {
	Info
	Hash  *Hash
	Bytes []byte
}

The info dictionary with its hash and raw bytes exposed, as these are important to Bittorrent.

func (InfoEx) MarshalBencode

func (this InfoEx) MarshalBencode() ([]byte, error)

func (*InfoEx) Piece

func (me *InfoEx) Piece(i int) Piece

func (*InfoEx) UnmarshalBencode

func (this *InfoEx) UnmarshalBencode(data []byte) error

type Magnet

type Magnet struct {
	InfoHash    Hash
	Trackers    []string
	DisplayName string
}

Magnet link components.

func ParseMagnetURI

func ParseMagnetURI(uri string) (m Magnet, err error)

ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance

func (Magnet) String

func (m Magnet) String() string

type MetaInfo

type MetaInfo struct {
	Info         InfoEx      `bencode:"info"`
	Announce     string      `bencode:"announce,omitempty"`
	AnnounceList [][]string  `bencode:"announce-list,omitempty"`
	Nodes        []Node      `bencode:"nodes,omitempty"`
	CreationDate int64       `bencode:"creation date,omitempty"`
	Comment      string      `bencode:"comment,omitempty"`
	CreatedBy    string      `bencode:"created by,omitempty"`
	Encoding     string      `bencode:"encoding,omitempty"`
	URLList      interface{} `bencode:"url-list,omitempty"`
}

func Load

func Load(r io.Reader) (*MetaInfo, error)

Load a MetaInfo from an io.Reader. Returns a non-nil error in case of failure.

func LoadFromFile

func LoadFromFile(filename string) (*MetaInfo, error)

Convenience function for loading a MetaInfo from a file.

func (*MetaInfo) Magnet

func (mi *MetaInfo) Magnet() (m Magnet)

Magnetize creates a Magnet from a MetaInfo

func (*MetaInfo) SetDefaults

func (mi *MetaInfo) SetDefaults()

Set good default values in preparation for creating a new MetaInfo file.

func (*MetaInfo) Write

func (mi *MetaInfo) Write(w io.Writer) error

Encode to bencoded form.

type Node

type Node string

func (*Node) UnmarshalBencode

func (me *Node) UnmarshalBencode(b []byte) (err error)

type Piece

type Piece struct {
	Info *InfoEx
	// contains filtered or unexported fields
}

func (Piece) Hash

func (me Piece) Hash() (ret Hash)

func (Piece) Length

func (me Piece) Length() int64

func (Piece) Offset

func (me Piece) Offset() int64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL