func createTorrent(fileName string, f io.ReadCloser, r *http.Request) ([]byte, error) { chunk := make([]byte, TORRENT_PIECE_LENGTH) torrent := Torrent{ Encoding: "UTF-8", Info: TorrentInfo{ PieceLength: TORRENT_PIECE_LENGTH, Name: fileName, }, UrlList: []string{fmt.Sprintf("%sselif/%s", getSiteURL(r), fileName)}, } for { n, err := f.Read(chunk) if err == io.EOF { break } else if err != nil { return []byte{}, err } torrent.Info.Length += n torrent.Info.Pieces += string(hashPiece(chunk[:n])) } data, err := bencode.EncodeBytes(&torrent) if err != nil { return []byte{}, err } return data, nil }
// Encode KRPC response package func (k *KRPC) EncodeResponsePackage(tid string, response map[string]string) ([]byte, error) { dat := make(map[string]interface{}) dat["t"] = tid dat["y"] = "r" dat["r"] = response return bencode.EncodeBytes(dat) }
// Encode KRPC Query package func (k *KRPC) EncodeQueryPackage(methodName string, args map[string]interface{}) ([]byte, error) { dat := make(map[string]interface{}) dat["t"] = fmt.Sprintf("%d", k.GenerateTID()) dat["y"] = "q" dat["q"] = methodName dat["a"] = args return bencode.EncodeBytes(dat) }
func encodeMessage(msg interface{}) ([]byte, error) { return bencode.EncodeBytes(msg) }