Added the Files tab, fixed peer tab, started adding functionality for the buttons, cleaned up general tab

This commit is contained in:
2017-12-30 23:24:17 -05:00
parent a9315a4b54
commit 7411638c95
17 changed files with 21952 additions and 21939 deletions

View File

@@ -15,21 +15,6 @@ type Message struct {
Payload []string
}
//GenericPayload is for any request to the server that only requires the TorrentHashString for matching (which is a lot of requests)
type GenericPayload struct {
TorrentHashString string
}
//MagnetMessage contains the magnet link entered by the user under ADD MAGNET link on the top toolbar
type MagnetMessage struct {
MagnetLink string `json:MagnetLink`
}
//TorrentCommandMessage contains a slice of strings that has the list of torrents to be acted on.
type TorrentCommandMessage struct {
TorrentHashStrings []string
}
//Next are the messages the server sends to the client
//TorrentList struct contains the torrent list that is sent to the client
@@ -42,8 +27,8 @@ type TorrentList struct { //helps create the JSON structure that react expects t
//TorrentFileList supplies a list of files attached to a single torrent along with some additional information
type TorrentFileList struct {
MessageType string
TotalFiles int `json:"total"`
FileList []torrent.File `json:"fileList"`
TotalFiles int `json:"TotalFiles"`
FileList []TorrentFile `json:"FileList"`
}
//PeerFileList returns a slice of peers
@@ -53,30 +38,40 @@ type PeerFileList struct {
PeerList []torrent.Peer `json:"PeerList"`
}
//ClientDB struct contains the struct that is used to compose the torrentlist
type ClientDB struct {
TorrentName string `json:"TorrentName"`
DownloadedSize string `json:"DownloadedSize"`
Size string `json:"Size"`
DownloadSpeed string `json:"DownloadSpeed"`
downloadSpeedInt int64
UploadSpeed string `json:"UploadSpeed"`
//UploadSpeedInt int64
DataBytesWritten int64
DataBytesRead int64
ActivePeers string `json:"ActivePeers"`
TorrentHashString string `json:"TorrentHashString"`
PercentDone string `json:"PercentDone"`
TorrentHash metainfo.Hash
StoragePath string `json:"StoragePath"`
DateAdded string
KnownSwarm []torrent.Peer
Status string `json:"Status"`
BytesCompleted int64
UpdatedAt time.Time
UploadRatio string
AddedAt string
ETA string `json:"ETA"`
Label string
SourceType string `json:"SourceType"`
//TorrentFile describes a single file that a torrent client is downloading for a single torrent
type TorrentFile struct {
TorrentHashString string //Used to tie the file to a torrent //TODO not sure if neededs
FileName string
FilePath string
FileSize string
FilePercent string
FilePriority string
}
//ClientDB struct contains the struct that is used to compose the torrentlist
type ClientDB struct { //TODO maybe seperate out the internal bits into another client struct
TorrentHashString string `json:"TorrentHashString"` //Passed to client for displaying hash and is used to uniquly identify all torrents
TorrentName string `json:"TorrentName"`
DownloadedSize string `json:"DownloadedSize"` //how much the client has downloaded total
Size string `json:"Size"` //total size of the torrent
DownloadSpeed string `json:"DownloadSpeed"` //the dl speed of the torrent
Status string `json:"Status"` //Passed to client for display
PercentDone string `json:"PercentDone"` //Passed to client to show percent done
ActivePeers string `json:"ActivePeers"` //passed to client
UploadSpeed string `json:"UploadSpeed"` //passed to client to show Uploadspeed
StoragePath string `json:"StoragePath"` //Passed to client (and stored in stormdb)
DateAdded string //Passed to client (and stored in stormdb)
ETA string `json:"ETA"` //Passed to client
Label string //Passed to client and stored in stormdb
SourceType string `json:"SourceType"` //Stores whether the torrent came from a torrent file or a magnet link
KnownSwarm []torrent.Peer //Passed to client for Peer Tab
UploadRatio string //Passed to client, stores the string for uploadratio stored in stormdb
TotalUploadedSize string //Humanized version of TotalUploadedBytes to pass to the client
TotalUploadedBytes int64 //includes bytes that happened before reboot (from stormdb)
downloadSpeedInt int64 //Internal used for calculating dl speed
BytesCompleted int64 //Internal used for calculating the dl speed
DataBytesWritten int64 //Internal used for calculating dl speed
DataBytesRead int64 //Internal used for calculating dl speed
UpdatedAt time.Time //Internal used for calculating speeds of upload and download
TorrentHash metainfo.Hash //Used to create string for TorrentHashString... not sure why I have it... make that a TODO I guess
}

View File

@@ -3,12 +3,13 @@ package engine //main file for all the calculations and data gathering needed fo
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/boltdb/bolt"
"github.com/asdine/storm"
Storage "github.com/deranjer/goTorrent/storage"
)
@@ -20,7 +21,7 @@ func timeOutInfo(clientTorrent *torrent.Torrent, seconds time.Duration) (deleted
}()
select {
case <-clientTorrent.GotInfo(): //attempting to retrieve info for torrent
//fmt.Println("Recieved torrent info for...", clientTorrent.Name())
fmt.Println("Recieved torrent info for...", clientTorrent.Name())
clientTorrent.DownloadAll()
return false
case <-timeout: // getting info for torrent has timed out so purging the torrent
@@ -32,7 +33,7 @@ func timeOutInfo(clientTorrent *torrent.Torrent, seconds time.Duration) (deleted
}
//StartTorrent creates the storage.db entry and starts A NEW TORRENT and adds to the running torrent array
func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage *Storage.TorrentLocal, torrentDbStorage *bolt.DB, dataDir string, torrentFile string, torrentFileName string) {
func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage Storage.TorrentLocal, torrentDbStorage *storm.DB, dataDir string, torrentFile string, torrentFileName string) {
timeOutInfo(clientTorrent, 45) //seeing if adding the torrrent times out (giving 45 seconds)
var TempHash metainfo.Hash
@@ -55,7 +56,7 @@ func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage *Storage.T
}
//CreateRunningTorrentArray creates the entire torrent list to pass to client
func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Storage.TorrentLocal, PreviousTorrentArray []ClientDB, config FullClientSettings, db *bolt.DB) (RunningTorrentArray []ClientDB) {
func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Storage.TorrentLocal, PreviousTorrentArray []ClientDB, config FullClientSettings, db *storm.DB) (RunningTorrentArray []ClientDB) {
for _, element := range TorrentLocalArray { //re-adding all the torrents we had stored from last shutdown or just added via file or magnet link
@@ -94,17 +95,12 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
}
}
}
activePeersString := fmt.Sprintf("%v", fullStruct.ActivePeers) //converting to strings
activePeersString := strconv.Itoa(fullStruct.ActivePeers) //converting to strings
totalPeersString := fmt.Sprintf("%v", fullStruct.TotalPeers)
bytesCompletedMB := float32(singleTorrent.BytesCompleted() / 1024 / 1024)
totalSizeMB := float32(singleTorrent.Length() / 1024 / 1024)
//downloadSizeString := fmt.Sprintf("%d", bytesCompletedMB)
tSize, dSize := ConvertSizetoGB(totalSizeMB, bytesCompletedMB) //convert size to GB if needed
var TempHash metainfo.Hash
TempHash = singleTorrent.InfoHash()
singleTorrentStorageInfo := Storage.FetchTorrentFromStorage(db, []byte(TempHash.String())) //fetching all the info from the database
singleTorrentStorageInfo := Storage.FetchTorrentFromStorage(db, TempHash.String()) //fetching all the info from the database
var torrentTypeTemp string
torrentTypeTemp = singleTorrentStorageInfo.TorrentType //either "file" or "magnet" maybe more in the future
if torrentTypeTemp == "file" {
@@ -112,16 +108,19 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
} else {
fullClientDB.SourceType = "Magnet Link"
}
fullClientDB.StoragePath = singleTorrentStorageInfo.StoragePath //grabbed from database
fullClientDB.StoragePath = singleTorrentStorageInfo.StoragePath
totalSizeHumanized := HumanizeBytes(float32(singleTorrent.BytesCompleted())) //convert size to GB if needed
downloadedSizeHumanized := HumanizeBytes(float32(singleTorrent.Length()))
fullClientDB.DownloadedSize = dSize
fullClientDB.Size = tSize
PercentDone := fmt.Sprintf("%.2f", bytesCompletedMB/totalSizeMB)
//grabbed from torrent client
fullClientDB.DownloadedSize = downloadedSizeHumanized
fullClientDB.Size = totalSizeHumanized
PercentDone := fmt.Sprintf("%.2f", float32(singleTorrent.BytesCompleted())/float32(singleTorrent.Length()))
fullClientDB.TorrentHash = TempHash
fullClientDB.PercentDone = PercentDone
fullClientDB.DataBytesRead = fullStruct.ConnStats.DataBytesRead
fullClientDB.DataBytesWritten = fullStruct.ConnStats.DataBytesWritten
fullClientDB.DataBytesRead = fullStruct.ConnStats.DataBytesRead //used for calculations not passed to client calculating up/down speed
fullClientDB.DataBytesWritten = fullStruct.ConnStats.DataBytesWritten //used for calculations not passed to client calculating up/down speed
fullClientDB.ActivePeers = activePeersString + " / (" + totalPeersString + ")"
fullClientDB.TorrentHashString = TempHash.String()
fullClientDB.StoragePath = element.StoragePath
@@ -130,21 +129,21 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
fullClientDB.BytesCompleted = singleTorrent.BytesCompleted()
CalculateTorrentETA(singleTorrent, fullClientDB) //calculating the ETA for the torrent
fullClientDB.UploadRatio = CalculateUploadRatio(singleTorrent, fullClientDB, db) //calculate the upload ratio
tickUpdateStruct := Storage.TorrentLocal{} //we are shoving the tick updates into a torrentlocal struct to pass to storage
fullClientDB.TotalUploadedBytes = singleTorrentStorageInfo.UploadedBytes
fullClientDB.TotalUploadedSize = HumanizeBytes(float32(fullClientDB.TotalUploadedBytes))
fullClientDB.UploadRatio = CalculateUploadRatio(singleTorrent, fullClientDB) //calculate the upload ratio
tickUpdateStruct := Storage.TorrentLocal{} //we are shoving the tick updates into a torrentlocal struct to pass to storage
tickUpdateStruct.UploadRatio = fullClientDB.UploadRatio
tickUpdateStruct.UploadedBytes = fullClientDB.DataBytesWritten
tickUpdateStruct.Hash = fullClientDB.TorrentHashString
tickUpdateStruct.Hash = fullClientDB.TorrentHashString //needed for index
Storage.UpdateStorageTick(db, tickUpdateStruct)
//fmt.Println("Download Speed: ", fullClientDB.DownloadSpeed)
//fmt.Println("Percent Done: ", fullClientDB.PercentDone)
//tclient.WriteStatus(os.Stdout)
CalculateTorrentStatus(singleTorrent, fullClientDB) //calculate the status of the torrent, ie downloading seeding etc
RunningTorrentArray = append(RunningTorrentArray, *fullClientDB)
}
//fmt.Println("RunningTorrentArrayCreated...")
return RunningTorrentArray
}
@@ -152,12 +151,23 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
func CreateFileListArray(tclient *torrent.Client, selectedHash string) TorrentFileList {
runningTorrents := tclient.Torrents() //don't need running torrent array since we aren't adding or deleting from storage
TorrentFileListSelected := TorrentFileList{}
TorrentFileStruct := TorrentFile{}
for _, singleTorrent := range runningTorrents {
tempHash := singleTorrent.InfoHash().String()
if tempHash == selectedHash { // if our selection hash equals our torrent hash
TorrentFileListSelected.FileList = singleTorrent.Files()
torrentFilesRaw := singleTorrent.Files()
for _, singleFile := range torrentFilesRaw {
TorrentFileStruct.TorrentHashString = tempHash
TorrentFileStruct.FileName = singleFile.DisplayPath()
TorrentFileStruct.FilePath = singleFile.Path()
TorrentFileStruct.FilePercent = fmt.Sprintf("%.2f", float32(singleFile.Length())/float32(singleFile.Length())) //TODO figure out downloaded size of file
TorrentFileStruct.FilePriority = "Normal" //TODO, figure out how to store this per file in storage and also tie a priority to a file
TorrentFileStruct.FileSize = HumanizeBytes(float32(singleFile.Length()))
TorrentFileListSelected.FileList = append(TorrentFileListSelected.FileList, TorrentFileStruct)
}
TorrentFileListSelected.MessageType = "torrentFileList"
TorrentFileListSelected.TotalFiles = len(singleTorrent.Files())
fmt.Println("filelist", TorrentFileListSelected)
return TorrentFileListSelected
}
@@ -183,9 +193,9 @@ func CreatePeerListArray(tclient *torrent.Client, selectedHash string) PeerFileL
}
//CreateTorrentDetailJSON creates the json response for a request for more torrent information
func CreateTorrentDetailJSON(tclient *torrent.Client, selectedHash string, torrentStorage *bolt.DB) ClientDB {
func CreateTorrentDetailJSON(tclient *torrent.Client, selectedHash string, torrentStorage *storm.DB) ClientDB {
localTorrentInfo := Storage.FetchTorrentFromStorage(torrentStorage, []byte(selectedHash))
localTorrentInfo := Storage.FetchTorrentFromStorage(torrentStorage, selectedHash)
runningTorrents := tclient.Torrents()

View File

@@ -5,7 +5,6 @@ import (
"time"
"github.com/anacrolix/torrent"
"github.com/boltdb/bolt"
)
func secondsToMinutes(inSeconds int64) string {
@@ -17,34 +16,22 @@ func secondsToMinutes(inSeconds int64) string {
return str
}
//ConvertSizetoGB changes the sizes
func ConvertSizetoGB(t float32, d float32) (tDelta string, dDelta string) { //converting sizes to MB or GB as needed and adding string
if t > 1024 && d > 1024 {
t := fmt.Sprintf("%.2f", t/1024)
t = t + " GB"
d := fmt.Sprintf("%.2f", d/1024)
d = d + " GB"
return t, d
} else if d > 1024 || t > 1024 {
if d > 1024 {
d := fmt.Sprintf("%.2f", d/1024)
d = d + " GB"
t := fmt.Sprintf("%.2f", t)
t = t + " MB"
return t, d
}
d := fmt.Sprintf("%.2f", d)
d = d + " MB"
t := fmt.Sprintf("%.2f", t/1024)
t = t + " GB"
return t, d
} else {
d := fmt.Sprintf("%.2f", d)
t := fmt.Sprintf("%.2f", t)
t = t + " MB"
d = d + " MB"
return t, d
//HumanizeBytes returns a nice humanized version of bytes in either GB or MB
func HumanizeBytes(bytes float32) string {
if bytes < 1000000 { //if we have less than 1MB in bytes convert to KB
pBytes := fmt.Sprintf("%.2f", bytes/1024)
pBytes = pBytes + " KB"
return pBytes
}
bytes = bytes / 1024 / 1024 //Converting bytes to a useful measure
if bytes > 1024 {
pBytes := fmt.Sprintf("%.2f", bytes/1024)
pBytes = pBytes + " GB"
return pBytes
}
pBytes := fmt.Sprintf("%.2f", bytes) //If not too big or too small leave it as MB
pBytes = pBytes + " MB"
return pBytes
}
//CalculateTorrentSpeed is used to calculate the torrent upload and download speed over time c is current clientdb, oc is last client db to calculate speed over time
@@ -56,9 +43,6 @@ func CalculateTorrentSpeed(t *torrent.Torrent, c *ClientDB, oc ClientDB) {
db := float32(bytes - oc.BytesCompleted) //getting the delta bytes
rate := db * (float32(time.Second) / dt) // converting into seconds
dbU := float32(bytesUpload - oc.DataBytesWritten)
//fmt.Println("BytesWritten", bytesUpload)
//fmt.Println("WireBytes", t.Stats().DataBytesWritten)
//fmt.Println("ChunksWritten", t.Stats().ChunksWritten)
rateUpload := dbU * (float32(time.Second) / dt)
if rate >= 0 {
rate = rate / 1024 / 1024 //creating integer to calculate ETA
@@ -90,18 +74,18 @@ func CalculateTorrentETA(t *torrent.Torrent, c *ClientDB) {
}
}
func CalculateUploadRatio(t *torrent.Torrent, c *ClientDB, db *bolt.DB) string {
if c.DataBytesWritten > 0 {
uploadRatioTemp := c.DataBytesRead / c.DataBytesWritten
uploadRatio := fmt.Sprintf("%.2f", uploadRatioTemp)
//CalculateUploadRatio calculates the download to upload ratio so you can see if you are being a good seeder
func CalculateUploadRatio(t *torrent.Torrent, c *ClientDB) string {
if c.TotalUploadedBytes > 0 && t.BytesCompleted() > 0 { //If we have actually started uploading and downloading stuff start calculating our ratio
uploadRatio := fmt.Sprintf("%.2f", float64(c.TotalUploadedBytes)/float64(t.BytesCompleted()))
return uploadRatio
}
uploadRatio := fmt.Sprintf("%.2f", 0.00) //we haven't uploaded anything so no upload ratio
uploadRatio := "0.00" //we haven't uploaded anything so no upload ratio just pass a string directly
return uploadRatio
}
//CalculateTorrentStatus is used to determine what the STATUS column of the frontend will display ll2
func CalculateTorrentStatus(t *torrent.Torrent, c *ClientDB) {
func CalculateTorrentStatus(t *torrent.Torrent, c *ClientDB) { //TODO redo all of this to allow for stopped torrents
if t.Seeding() && t.Stats().ActivePeers > 0 && t.BytesMissing() == 0 {
c.Status = "Seeding"
} else if t.Stats().ActivePeers > 0 && t.BytesMissing() > 0 {