Пример #1
0
// Checks if the torrent exists in cache. Otherwise attempts to fill
// cache from the database.
//
// TODO:
// * Cache-filler should probably have some sort of timeout per torrent.
// * Cache-filler should check that `info_hash` is not obviously malformed.
// * Distributed/coordinated cache fills? [ref: groupcache]
func (s *Server) torrentExists(infoHash string) (*libTorrent.Torrent, bool) {
	torrent := s.torrentCache[infoHash]

	if torrent == nil {
		// Cache miss

		dbTorrent := &models.Torrent{}
		if err := dbTorrent.SelectHash(infoHash); err != nil {
			// Check DB for torrent
			return nil, false
		} else {
			// Attempt to lib.Torrent{} from database model failed.
			prepareTorrent, err := dbTorrent.LoadTorrent()
			if err != nil {
				return nil, false
			}

			trackerTorrent := libTorrent.NewTorrent(prepareTorrent)
			trackerTorrent.InfoHash = dbTorrent.InfoHash
			s.torrentCache[infoHash] = trackerTorrent

			return s.torrentCache[infoHash], true
		}

		// All torrent cache-filling strategies have failed.
		return nil, false
	} else {
		// Cache hit

		return torrent, true
	}
}
Пример #2
0
// Creates a fake torrent we can use for testing purposes.
func MockTorrent() *torrent.Torrent {
	file := &torrent.TorrentFile{}
	t := torrent.NewTorrent(file)

	file.Announce = "http://localhost:4200/null/null/announce"
	file.Comment = "created by babou test suite"
	file.CreatedBy = "babou v0.0"
	file.CreationDate = time.Now().Unix()
	file.Encoding = "UTF-8"

	file.Info = make(map[string]interface{})

	file.Info["name"] = "fff.mkv"
	file.Info["length"] = 1024

	file.Info["piece length"] = 1024
	file.Info["pieces"] = "01234567890123456789"

	return t
}