2017-05-06 13:43:24 +02:00
package metainfo
// this file is from https://github.com/majestrate/XD
import (
"crypto/sha1"
2017-05-28 23:47:47 +02:00
"encoding/hex"
2017-05-06 13:43:24 +02:00
"io"
"os"
"path/filepath"
2017-05-28 23:47:47 +02:00
"strings"
2017-05-09 01:56:57 +02:00
"github.com/zeebo/bencode"
2017-05-06 13:43:24 +02:00
)
2017-05-26 12:12:52 +02:00
// FilePath type
2017-05-06 13:43:24 +02:00
type FilePath [ ] string
2017-05-26 12:12:52 +02:00
// FilePath : get filepath
2017-05-06 13:43:24 +02:00
func ( f FilePath ) FilePath ( ) string {
return filepath . Join ( f ... )
}
2017-05-26 12:12:52 +02:00
// Open : open file using base path
2017-05-06 13:43:24 +02:00
func ( f FilePath ) Open ( base string ) ( * os . File , error ) {
return os . OpenFile ( filepath . Join ( base , f . FilePath ( ) ) , os . O_RDWR | os . O_CREATE , 0600 )
}
2017-05-26 12:12:52 +02:00
// FileInfo struct
2017-05-06 13:43:24 +02:00
type FileInfo struct {
// length of file
Length uint64 ` bencode:"length" `
// relative path of file
Path FilePath ` bencode:"path" `
// md5sum
Sum [ ] byte ` bencode:"md5sum,omitempty" `
}
2017-05-26 12:12:52 +02:00
// Info : info section of torrent file
2017-05-06 13:43:24 +02:00
type Info struct {
// length of pices in bytes
PieceLength uint32 ` bencode:"piece length" `
// piece data
Pieces [ ] byte ` bencode:"pieces" `
// name of root file
Path string ` bencode:"name" `
// file metadata
Files [ ] FileInfo ` bencode:"files,omitempty" `
// private torrent
Private * int64 ` bencode:"private,omitempty" `
// length of file in signle file mode
Length uint64 ` bencode:"length,omitempty" `
// md5sum
Sum [ ] byte ` bencode:"md5sum,omitempty" `
}
2017-05-26 12:12:52 +02:00
// GetFiles : get fileinfos from this info section
2017-05-06 13:43:24 +02:00
func ( i Info ) GetFiles ( ) ( infos [ ] FileInfo ) {
if i . Length > 0 {
infos = append ( infos , FileInfo {
Length : i . Length ,
Path : FilePath ( [ ] string { i . Path } ) ,
Sum : i . Sum ,
} )
} else {
infos = append ( infos , i . Files ... )
}
return
}
2017-05-26 12:12:52 +02:00
// NumPieces : length of Info
2017-05-06 13:43:24 +02:00
func ( i Info ) NumPieces ( ) uint32 {
return uint32 ( len ( i . Pieces ) / 20 )
}
2017-05-26 12:12:52 +02:00
// TorrentFile : a torrent file
2017-05-06 13:43:24 +02:00
type TorrentFile struct {
Info Info ` bencode:"info" `
Announce string ` bencode:"announce" `
AnnounceList [ ] [ ] string ` bencode:"announce-list" `
Created uint64 ` bencode:"created" `
Comment [ ] byte ` bencode:"comment" `
CreatedBy [ ] byte ` bencode:"created by" `
Encoding [ ] byte ` bencode:"encoding" `
}
2017-05-26 12:12:52 +02:00
// TotalSize : get total size of files from torrent info section
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) TotalSize ( ) uint64 {
if tf . IsSingleFile ( ) {
return tf . Info . Length
}
total := uint64 ( 0 )
for _ , f := range tf . Info . Files {
total += f . Length
}
return total
}
2017-05-26 12:12:52 +02:00
// GetAllAnnounceURLS : get all trackers url
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) GetAllAnnounceURLS ( ) ( l [ ] string ) {
2017-05-09 01:56:57 +02:00
l = make ( [ ] string , 0 , 64 )
2017-05-06 13:43:24 +02:00
if len ( tf . Announce ) > 0 {
l = append ( l , tf . Announce )
}
for _ , al := range tf . AnnounceList {
for _ , a := range al {
if len ( a ) > 0 {
l = append ( l , a )
}
}
}
return
}
2017-05-26 12:12:52 +02:00
// TorrentName : return torrent name
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) TorrentName ( ) string {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return tf . Info . Path
2017-05-06 13:43:24 +02:00
}
2017-05-26 12:12:52 +02:00
// IsPrivate : return true if this torrent is private otherwise return false
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) IsPrivate ( ) bool {
2017-05-07 12:20:08 +02:00
return tf . Info . Private != nil && * tf . Info . Private == 1
2017-05-06 13:43:24 +02:00
}
2017-05-26 12:12:52 +02:00
// IsSingleFile : return true if this torrent is for a single file
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) IsSingleFile ( ) bool {
return tf . Info . Length > 0
}
2017-05-26 12:12:52 +02:00
// Encode : bencode this file via an io.Writer
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) Encode ( w io . Writer ) ( err error ) {
enc := bencode . NewEncoder ( w )
err = enc . Encode ( tf )
return
}
2017-05-26 12:12:52 +02:00
// Decode : load from an io.Reader
2017-05-06 13:43:24 +02:00
func ( tf * TorrentFile ) Decode ( r io . Reader ) ( err error ) {
dec := bencode . NewDecoder ( r )
err = dec . Decode ( tf )
return
}
2017-05-28 23:47:47 +02:00
type torrentRaw struct {
InfoRaw bencode . RawMessage ` bencode:"info" `
}
// DecodeInfohash : Decode and calculate the info hash
func DecodeInfohash ( r io . Reader ) ( hash string , err error ) {
var t torrentRaw
d := bencode . NewDecoder ( r )
err = d . Decode ( & t )
if err != nil {
return
}
s := sha1 . New ( )
_ , err = s . Write ( t . InfoRaw )
if err != nil {
return
}
rawHash := s . Sum ( nil )
hash = strings . ToUpper ( hex . EncodeToString ( rawHash ) )
return
}