mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Directory (#102)
* changes to explore to take parameter id in /explore * route /expore/node added
This commit is contained in:
committed by
GitHub
parent
3537d04c4d
commit
3a1eece580
@@ -6,84 +6,98 @@ Author: Peter Kleissner
|
||||
package webapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
)
|
||||
|
||||
// queryRecentShared returns recently shared files on the network from random peers until the limit is reached.
|
||||
func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int, limitPeer, offsetTotal, limitTotal uint64) (files []blockchain.BlockRecordFile) {
|
||||
if limitPeer == 0 {
|
||||
limitPeer = 1
|
||||
}
|
||||
func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int, limitPeer, offsetTotal, limitTotal uint64, nodeID []byte, nodeIDState bool) (files []blockchain.BlockRecordFile) {
|
||||
if limitPeer == 0 {
|
||||
limitPeer = 1
|
||||
}
|
||||
|
||||
// Use the peer list to know about active peers. Random order!
|
||||
peerList := api.Backend.PeerlistGet()
|
||||
// Assign peer list as an empty array
|
||||
var peerList []*core.PeerInfo
|
||||
|
||||
// Files from peers exceeding the limit. It is used if from all peers the total limit is not reached.
|
||||
var filesSeconday []blockchain.BlockRecordFile
|
||||
// check if the NodeID is provided or not
|
||||
if len(nodeID) == 0 && !nodeIDState {
|
||||
// Use the peer list to know about active peers. Random order!
|
||||
peerList = api.backend.PeerlistGet()
|
||||
} else {
|
||||
// Get peer information based on the NodeID provided
|
||||
_, peer, err := api.backend.FindNode(nodeID, time.Second*5)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
peerList = append(peerList, peer)
|
||||
}
|
||||
|
||||
for _, peer := range peerList {
|
||||
if peer.BlockchainHeight == 0 {
|
||||
continue
|
||||
}
|
||||
// Files from peers exceeding the limit. It is used if from all peers the total limit is not reached.
|
||||
var filesSeconday []blockchain.BlockRecordFile
|
||||
|
||||
var filesFromPeer uint64
|
||||
for _, peer := range peerList {
|
||||
if peer.BlockchainHeight == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// decode blocks from top down
|
||||
blockLoop:
|
||||
for blockN := peer.BlockchainHeight - 1; blockN > 0; blockN-- {
|
||||
blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
var filesFromPeer uint64
|
||||
|
||||
for _, record := range blockDecoded.RecordsDecoded {
|
||||
if file, ok := record.(blockchain.BlockRecordFile); ok && isFileTypeMatchBlock(&file, fileType) {
|
||||
// add the tags 'Shared By Count' and 'Shared By GeoIP'
|
||||
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
|
||||
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
|
||||
sharedByGeoIP := fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
|
||||
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP))
|
||||
}
|
||||
// decode blocks from top down
|
||||
blockLoop:
|
||||
for blockN := peer.BlockchainHeight - 1; blockN > 0; blockN-- {
|
||||
blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
|
||||
// found a new file! append.
|
||||
if filesFromPeer < limitPeer {
|
||||
filesFromPeer++
|
||||
for _, record := range blockDecoded.RecordsDecoded {
|
||||
if file, ok := record.(blockchain.BlockRecordFile); ok && isFileTypeMatchBlock(&file, fileType) {
|
||||
// add the tags 'Shared By Count' and 'Shared By GeoIP'
|
||||
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
|
||||
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
|
||||
sharedByGeoIP := fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
|
||||
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP))
|
||||
}
|
||||
|
||||
if offsetTotal > 0 {
|
||||
offsetTotal--
|
||||
continue
|
||||
}
|
||||
// found a new file! append.
|
||||
if filesFromPeer < limitPeer {
|
||||
filesFromPeer++
|
||||
|
||||
files = append(files, file)
|
||||
if offsetTotal > 0 {
|
||||
offsetTotal--
|
||||
continue
|
||||
}
|
||||
|
||||
if uint64(len(files)) >= limitTotal {
|
||||
return
|
||||
}
|
||||
} else if uint64(len(filesSeconday)) < limitTotal-uint64(len(files)) {
|
||||
filesSeconday = append(filesSeconday, file)
|
||||
} else {
|
||||
break blockLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
files = append(files, file)
|
||||
|
||||
files = append(files, filesSeconday...)
|
||||
if uint64(len(files)) >= limitTotal {
|
||||
return
|
||||
}
|
||||
} else if uint64(len(filesSeconday)) < limitTotal-uint64(len(files)) {
|
||||
filesSeconday = append(filesSeconday, file)
|
||||
} else {
|
||||
break blockLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
files = append(files, filesSeconday...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// isFileTypeMatchBlock checks if the file type matches. -1 = accept any. -2 = core.TypeBinary, core.TypeCompressed, core.TypeContainer, core.TypeExecutable.
|
||||
func isFileTypeMatchBlock(file *blockchain.BlockRecordFile, fileType int) bool {
|
||||
if fileType == -1 {
|
||||
return true
|
||||
} else if fileType == -2 {
|
||||
return file.Type == core.TypeBinary || file.Type == core.TypeCompressed || file.Type == core.TypeContainer || file.Type == core.TypeExecutable
|
||||
}
|
||||
if fileType == -1 {
|
||||
return true
|
||||
} else if fileType == -2 {
|
||||
return file.Type == core.TypeBinary || file.Type == core.TypeCompressed || file.Type == core.TypeContainer || file.Type == core.TypeExecutable
|
||||
}
|
||||
|
||||
return file.Type == uint8(fileType)
|
||||
return file.Type == uint8(fileType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user