Adding progress bar, fixing progress code, preparing for splitting files.
This commit is contained in:
0
engine/engine.go
Normal file
0
engine/engine.go
Normal file
178
main.go
178
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -47,10 +48,10 @@ type clientDB struct {
|
|||||||
UploadSpeed float32 `json:"UploadSpeed"`
|
UploadSpeed float32 `json:"UploadSpeed"`
|
||||||
DataBytesWritten int64
|
DataBytesWritten int64
|
||||||
DataBytesRead int64
|
DataBytesRead int64
|
||||||
ActivePeers int `json:"ActivePeers"`
|
ActivePeers int `json:"ActivePeers"`
|
||||||
TotalPeers int `json:"TotalPeers"`
|
TotalPeers int `json:"TotalPeers"`
|
||||||
TorrentHashString string `json:"TorrentHashString"`
|
TorrentHashString string `json:"TorrentHashString"`
|
||||||
PercentDone int64 `json:"Done"`
|
PercentDone float32 `json:"PercentDone"`
|
||||||
TorrentHash metainfo.Hash
|
TorrentHash metainfo.Hash
|
||||||
StoragePath string `json:"StorageLocation"`
|
StoragePath string `json:"StorageLocation"`
|
||||||
DateAdded string
|
DateAdded string
|
||||||
@@ -63,15 +64,24 @@ type clientDB struct {
|
|||||||
func calculateTorrentSpeed(t *torrent.Torrent, c *clientDB) {
|
func calculateTorrentSpeed(t *torrent.Torrent, c *clientDB) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
bytes := t.BytesCompleted()
|
bytes := t.BytesCompleted()
|
||||||
if !c.UpdatedAt.IsZero() {
|
fmt.Println("UpdatedAt: ", c.UpdatedAt)
|
||||||
|
if c.UpdatedAt.IsZero() {
|
||||||
|
c.UpdatedAt = now
|
||||||
|
fmt.Println("Setting Time", c.UpdatedAt)
|
||||||
|
|
||||||
|
} else {
|
||||||
dt := float32(now.Sub(c.UpdatedAt))
|
dt := float32(now.Sub(c.UpdatedAt))
|
||||||
|
fmt.Println("Delta Time: ", dt)
|
||||||
db := float32(bytes - c.BytesCompleted)
|
db := float32(bytes - c.BytesCompleted)
|
||||||
|
fmt.Println("Delta Bytes:", db)
|
||||||
rate := db * (float32(time.Second) / dt)
|
rate := db * (float32(time.Second) / dt)
|
||||||
|
|
||||||
|
fmt.Println("form: ", float32(time.Second))
|
||||||
if rate >= 0 {
|
if rate >= 0 {
|
||||||
c.DownloadSpeed = rate
|
c.DownloadSpeed = rate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.UpdatedAt = now
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateTorrentStatus(t *torrent.Torrent, c *clientDB) {
|
func calculateTorrentStatus(t *torrent.Torrent, c *clientDB) {
|
||||||
@@ -79,7 +89,9 @@ func calculateTorrentStatus(t *torrent.Torrent, c *clientDB) {
|
|||||||
c.Status = "Seeding"
|
c.Status = "Seeding"
|
||||||
} else if t.Stats().ActivePeers > 0 && t.BytesMissing() > 0 {
|
} else if t.Stats().ActivePeers > 0 && t.BytesMissing() > 0 {
|
||||||
c.Status = "Downloading"
|
c.Status = "Downloading"
|
||||||
} else if t.Stats().ActivePeers == 0 {
|
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() == 0 {
|
||||||
|
c.Status = "Completed"
|
||||||
|
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() > 0 {
|
||||||
c.Status = "Awaiting Peers"
|
c.Status = "Awaiting Peers"
|
||||||
} else {
|
} else {
|
||||||
c.Status = "Unknown"
|
c.Status = "Unknown"
|
||||||
@@ -91,56 +103,104 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
|
|||||||
s1.ExecuteTemplate(w, "base", map[string]string{"APP_ID": APP_ID})
|
s1.ExecuteTemplate(w, "base", map[string]string{"APP_ID": APP_ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
func startTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage *TorrentLocal, Config torrent.Config, torrentDbStorage *bolt.DB, torrentLocal []*TorrentLocal, tclient *torrent.Client) {
|
func startTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage *TorrentLocal, torrentDbStorage *bolt.DB, dataDir string, torrentFile string, torrentFileName string) {
|
||||||
<-clientTorrent.GotInfo() //waiting for all of the torrent info to be downloaded
|
|
||||||
|
timeout := make(chan bool, 1) //creating a timeout channel for our gotinfo
|
||||||
|
go func() {
|
||||||
|
time.Sleep(45 * time.Second)
|
||||||
|
timeout <- true
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-clientTorrent.GotInfo(): //attempting to retrieve info for torrent
|
||||||
|
fmt.Println("Recieved torrent info..")
|
||||||
|
clientTorrent.DownloadAll()
|
||||||
|
case <-timeout: // getting info for torrent has timed out so purging the torrent
|
||||||
|
fmt.Println("Dropping Torrent")
|
||||||
|
clientTorrent.Drop()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var TempHash metainfo.Hash
|
var TempHash metainfo.Hash
|
||||||
TempHash = clientTorrent.InfoHash()
|
TempHash = clientTorrent.InfoHash()
|
||||||
fmt.Println(clientTorrent.Info().Source)
|
fmt.Println(clientTorrent.Info().Source)
|
||||||
torrentLocalStorage.Hash = TempHash.String() // we will store the infohash to add it back later on client restart (if needed)
|
torrentLocalStorage.Hash = TempHash.String() // we will store the infohash to add it back later on client restart (if needed)
|
||||||
torrentLocalStorage.DateAdded = time.Now().Format("Jan _2 2006")
|
torrentLocalStorage.DateAdded = time.Now().Format("Jan _2 2006")
|
||||||
torrentLocalStorage.StoragePath = Config.DataDir //TODO check full path information for torrent storage
|
torrentLocalStorage.StoragePath = dataDir //TODO check full path information for torrent storage
|
||||||
torrentLocalStorage.TorrentName = clientTorrent.Name()
|
torrentLocalStorage.TorrentName = clientTorrent.Name()
|
||||||
torrentLocalStorage.TorrentStatus = "downloading" //by default start all the torrents as downloading.
|
torrentLocalStorage.TorrentStatus = "downloading" //by default start all the torrents as downloading.
|
||||||
|
torrentLocalStorage.TorrentType = torrentFile //either "file" or "magnet" maybe more in the future
|
||||||
|
if torrentFile == "file" {
|
||||||
|
torrentLocalStorage.TorrentFileName = torrentFileName
|
||||||
|
} else {
|
||||||
|
torrentLocalStorage.TorrentFileName = ""
|
||||||
|
}
|
||||||
fmt.Printf("%+v\n", torrentLocalStorage)
|
fmt.Printf("%+v\n", torrentLocalStorage)
|
||||||
addTorrentLocalStorage(torrentDbStorage, torrentLocalStorage) //writing all of the data to the database
|
addTorrentLocalStorage(torrentDbStorage, torrentLocalStorage) //writing all of the data to the database
|
||||||
|
clientTorrent.DownloadAll() //starting the download
|
||||||
clientTorrent.DownloadAll() //starting the download
|
|
||||||
createRunningTorrentArray(tclient, torrentLocal)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*TorrentLocal) (RunningTorrentArray []clientDB) {
|
func createRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*TorrentLocal, config fullClientSettings, db *bolt.DB) (RunningTorrentArray []clientDB) {
|
||||||
for _, element := range TorrentLocalArray { //re-adding all the torrents we had stored from last shutdown
|
for _, element := range TorrentLocalArray { //re-adding all the torrents we had stored from last shutdown
|
||||||
|
|
||||||
elementMagnet := "magnet:?xt=urn:btih:" + element.Hash
|
var singleTorrent *torrent.Torrent
|
||||||
singleTorrent, _ := tclient.AddMagnet(elementMagnet) //adding back in the torrents by hash
|
|
||||||
|
|
||||||
fmt.Println("Here...", elementMagnet)
|
if element.TorrentType == "file" { //if it is a file pull it from the uploaded torrent folder
|
||||||
//<-singleTorrent.GotInfo()
|
fmt.Println("Filename", element.TorrentFileName)
|
||||||
//singleTorrent.DownloadAll()
|
if _, err := os.Stat(element.TorrentFileName); err == nil { //if we CAN find the torrent, add it
|
||||||
fmt.Println("Past...")
|
fmt.Println("Adding file name...", element.TorrentFileName)
|
||||||
|
singleTorrent, _ = tclient.AddTorrentFromFile(element.TorrentFileName)
|
||||||
|
} else { //if we cant find the torrent delete it
|
||||||
|
fmt.Println("File Error", err)
|
||||||
|
delTorrentLocalStorage(db, element)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
elementMagnet := "magnet:?xt=urn:btih:" + element.Hash //For magnet links just need to prepend the magnet part to the hash to readd
|
||||||
|
singleTorrent, _ = tclient.AddMagnet(elementMagnet)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := make(chan bool, 1) //creating a timeout channel for our gotinfo
|
||||||
|
go func() {
|
||||||
|
time.Sleep(45 * time.Second)
|
||||||
|
timeout <- true
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-singleTorrent.GotInfo(): //attempting to retrieve info for torrent
|
||||||
|
singleTorrent.DownloadAll()
|
||||||
|
case <-timeout: // getting info for torrent has timed out so purging the torrent
|
||||||
|
fmt.Println("Dropping Torrent")
|
||||||
|
singleTorrent.Drop()
|
||||||
|
delTorrentLocalStorage(db, element) //purging torrent from the local database
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Recieved info for: ", element.TorrentName)
|
||||||
fullClientDB := new(clientDB)
|
fullClientDB := new(clientDB)
|
||||||
fullStruct := singleTorrent.Stats()
|
fullStruct := singleTorrent.Stats()
|
||||||
|
|
||||||
//bytesMissing := singleTorrent.BytesMissing()
|
bytesTotal := singleTorrent.Length()
|
||||||
//bytesTotal := singleTorrent.Length()
|
|
||||||
//bytesCompleted := singleTorrent.BytesCompleted()
|
//bytesCompleted := singleTorrent.BytesCompleted()
|
||||||
//
|
|
||||||
calculateTorrentSpeed(singleTorrent, fullClientDB) //Setting the downloadSpeed for the torrent
|
calculateTorrentSpeed(singleTorrent, fullClientDB) //Setting the downloadSpeed for the torrent
|
||||||
|
//fullClientDB.UpdatedAt = time.Now() // setting the current time to measure download speed.
|
||||||
var TempHash metainfo.Hash
|
var TempHash metainfo.Hash
|
||||||
TempHash = singleTorrent.InfoHash()
|
TempHash = singleTorrent.InfoHash()
|
||||||
//fullClientDB.DownloadedSize = singleTorrent.BytesCompleted()
|
fullClientDB.DownloadedSize = singleTorrent.BytesCompleted() / 1024 / 1024
|
||||||
//fullClientDB.Size = bytesTotal
|
fullClientDB.Size = bytesTotal / 1024 / 1024
|
||||||
fullClientDB.TorrentHash = TempHash
|
fullClientDB.TorrentHash = TempHash
|
||||||
//fullClientDB.PercentDone = 1 - (bytesMissing / bytesTotal)
|
fullClientDB.PercentDone = (float32(fullClientDB.DownloadedSize) / float32(fullClientDB.Size))
|
||||||
//fullClientDB.DataBytesRead = fullStruct.ConnStats.DataBytesRead
|
fullClientDB.DataBytesRead = fullStruct.ConnStats.DataBytesRead
|
||||||
//fullClientDB.DataBytesWritten = fullStruct.ConnStats.DataBytesWritten
|
fullClientDB.DataBytesWritten = fullStruct.ConnStats.DataBytesWritten
|
||||||
fullClientDB.ActivePeers = fullStruct.ActivePeers
|
fullClientDB.ActivePeers = fullStruct.ActivePeers
|
||||||
fullClientDB.TotalPeers = fullStruct.TotalPeers
|
fullClientDB.TotalPeers = fullStruct.TotalPeers
|
||||||
fullClientDB.TorrentHashString = TempHash.AsString()
|
fullClientDB.TorrentHashString = TempHash.AsString()
|
||||||
fullClientDB.StoragePath = element.StoragePath
|
fullClientDB.StoragePath = element.StoragePath
|
||||||
fullClientDB.TorrentName = element.TorrentName
|
fullClientDB.TorrentName = element.TorrentName
|
||||||
fullClientDB.DateAdded = element.DateAdded
|
fullClientDB.DateAdded = element.DateAdded
|
||||||
|
fmt.Println("Download Speed: ", fullClientDB.DownloadSpeed)
|
||||||
|
fmt.Println("Percent Done: ", fullClientDB.PercentDone)
|
||||||
|
fmt.Println("UpdatedAt: ", fullClientDB.UpdatedAt)
|
||||||
|
|
||||||
calculateTorrentStatus(singleTorrent, fullClientDB) //calculate the status of the torrent
|
calculateTorrentStatus(singleTorrent, fullClientDB) //calculate the status of the torrent
|
||||||
|
|
||||||
@@ -158,9 +218,9 @@ func updateClient(torrentstats []clientDB, conn *websocket.Conn) { //get the tor
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//setting up the torrent client
|
//setting up the torrent client
|
||||||
Config := fullClientSettingsNew() //grabbing from settings.go
|
Config := fullClientSettingsNew() //grabbing from settings.go
|
||||||
|
os.Mkdir(Config.tFileUploadFolder, os.ModeDir) //creating a directory to store uploaded torrent files
|
||||||
torrentLocalStorage := new(TorrentLocal) //creating a new struct that stores all of our local storage info
|
torrentLocalStorage := new(TorrentLocal) //creating a new struct that stores all of our local storage info
|
||||||
|
|
||||||
fmt.Printf("%+v\n", Config)
|
fmt.Printf("%+v\n", Config)
|
||||||
|
|
||||||
@@ -180,8 +240,9 @@ func main() {
|
|||||||
|
|
||||||
TorrentLocalArray = readInTorrents(db) //pulling in all the already added torrents
|
TorrentLocalArray = readInTorrents(db) //pulling in all the already added torrents
|
||||||
|
|
||||||
if TorrentLocalArray != nil {
|
if TorrentLocalArray != nil { //the first creation of the running torrent array
|
||||||
RunningTorrentArray = createRunningTorrentArray(tclient, TorrentLocalArray) //Updates the RunningTorrentArray with the current client data as well
|
RunningTorrentArray = createRunningTorrentArray(tclient, TorrentLocalArray, Config, db) //Updates the RunningTorrentArray with the current client data as well
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Database is empty!")
|
fmt.Println("Database is empty!")
|
||||||
}
|
}
|
||||||
@@ -196,8 +257,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error with fetching file or request issue", file)
|
fmt.Println("Error with fetching file or request issue", file)
|
||||||
}
|
}
|
||||||
defer file.Close() //defer closing the file until we are done manipulating it
|
defer file.Close() //defer closing the file until we are done manipulating it
|
||||||
fileName, err := os.OpenFile(header.Filename, os.O_WRONLY|os.O_CREATE, 0666) //generating the fileName
|
var filePath = filepath.Join(Config.tFileUploadFolder, header.Filename) //creating a full filepath to store the .torrent files
|
||||||
|
fileName, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666) //generating the fileName
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -207,21 +269,20 @@ func main() {
|
|||||||
fmt.Println("Error adding Torrent from file: ", fileName.Name())
|
fmt.Println("Error adding Torrent from file: ", fileName.Name())
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Adding Torrent via file", fileName)
|
fmt.Println("Adding Torrent via file", fileName)
|
||||||
startTorrent(clientTorrent, torrentLocalStorage, Config.Config, db, TorrentLocalArray, tclient)
|
startTorrent(clientTorrent, torrentLocalStorage, db, Config.DataDir, "file", fileName.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { //exposing the data to the
|
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { //exposing the data to the
|
||||||
if len(RunningTorrentArray) > 0 {
|
TorrentLocalArray = readInTorrents(db)
|
||||||
RunningTorrentArray = createRunningTorrentArray(tclient, TorrentLocalArray) //Updates the RunningTorrentArray with the current client data as well
|
RunningTorrentArray = createRunningTorrentArray(tclient, TorrentLocalArray, Config, db) //Updates the RunningTorrentArray with the current client data as well
|
||||||
var torrentlistArray = new(torrentList)
|
var torrentlistArray = new(torrentList)
|
||||||
torrentlistArray.ClientDBstruct = RunningTorrentArray
|
torrentlistArray.ClientDBstruct = RunningTorrentArray
|
||||||
torrentlistArray.Totaltorrents = len(RunningTorrentArray)
|
torrentlistArray.Totaltorrents = len(RunningTorrentArray)
|
||||||
torrentlistArrayJSON, _ := json.Marshal(torrentlistArray)
|
torrentlistArrayJSON, _ := json.Marshal(torrentlistArray)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(torrentlistArrayJSON)
|
w.Write(torrentlistArrayJSON)
|
||||||
//updateClient(RunningTorrentArray, conn) // sending the client update information over the websocket
|
//updateClient(RunningTorrentArray, conn) // sending the client update information over the websocket
|
||||||
}
|
|
||||||
})
|
})
|
||||||
http.HandleFunc("/websocket", func(w http.ResponseWriter, r *http.Request) { //websocket is the main data pipe to the frontend
|
http.HandleFunc("/websocket", func(w http.ResponseWriter, r *http.Request) { //websocket is the main data pipe to the frontend
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
@@ -239,22 +300,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
if string(msg) == "clientUpdateRequest" { //6 second update ping
|
if string(msg) == "clientUpdateRequest" { //6 second update ping
|
||||||
fmt.Println("client Requested Update")
|
fmt.Println("client Requested Update")
|
||||||
time.Sleep(6 * time.Second)
|
//time.Sleep(6 * time.Second)
|
||||||
err = conn.WriteMessage(msgType, []byte("clientUpdate"))
|
err = conn.WriteMessage(msgType, []byte("clientUpdate"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Websocket Write err", err)
|
fmt.Println("Websocket Write err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(RunningTorrentArray) > 0 {
|
TorrentLocalArray = readInTorrents(db)
|
||||||
RunningTorrentArray = createRunningTorrentArray(tclient, TorrentLocalArray) //Updates the RunningTorrentArray with the current client data as well
|
RunningTorrentArray = createRunningTorrentArray(tclient, TorrentLocalArray, Config, db) //Updates the RunningTorrentArray with the current client data as well
|
||||||
var torrentlistArray = new(torrentList)
|
var torrentlistArray = new(torrentList)
|
||||||
torrentlistArray.ClientDBstruct = RunningTorrentArray
|
torrentlistArray.ClientDBstruct = RunningTorrentArray
|
||||||
torrentlistArray.Totaltorrents = len(RunningTorrentArray)
|
torrentlistArray.Totaltorrents = len(RunningTorrentArray)
|
||||||
fmt.Printf("%+v\n", torrentlistArray)
|
//fmt.Printf("%+v\n", torrentlistArray)
|
||||||
conn.WriteJSON(torrentlistArray)
|
conn.WriteJSON(torrentlistArray)
|
||||||
//updateClient(RunningTorrentArray, conn) // sending the client update information over the websocket
|
//updateClient(RunningTorrentArray, conn) // sending the client update information over the websocket
|
||||||
}
|
|
||||||
|
|
||||||
} else if strings.HasPrefix(string(msg), "magnet:") {
|
} else if strings.HasPrefix(string(msg), "magnet:") {
|
||||||
fmt.Println(string(msg))
|
fmt.Println(string(msg))
|
||||||
@@ -263,9 +323,9 @@ func main() {
|
|||||||
fmt.Println("Magnet Error", err)
|
fmt.Println("Magnet Error", err)
|
||||||
}
|
}
|
||||||
fmt.Println(clientTorrent)
|
fmt.Println(clientTorrent)
|
||||||
fmt.Printf("Adding")
|
fmt.Printf("Adding Magnet Link")
|
||||||
|
|
||||||
startTorrent(clientTorrent, torrentLocalStorage, Config.Config, db, TorrentLocalArray, tclient) //starting the torrent and creating local DB entry
|
startTorrent(clientTorrent, torrentLocalStorage, db, Config.DataDir, "magnet", "") //starting the torrent and creating local DB entry
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
File diff suppressed because one or more lines are too long
11
settings.go
11
settings.go
@@ -1,27 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
||||||
"github.com/anacrolix/torrent"
|
|
||||||
"github.com/anacrolix/dht"
|
"github.com/anacrolix/dht"
|
||||||
|
"github.com/anacrolix/torrent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type fullClientSettings struct {
|
type fullClientSettings struct {
|
||||||
version int
|
version int
|
||||||
torrent.Config
|
torrent.Config
|
||||||
|
tFileUploadFolder string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fullClientSettingsNew() fullClientSettings {
|
||||||
func fullClientSettingsNew()(fullClientSettings){
|
|
||||||
//Config := fullClientSettings //generate a new struct
|
//Config := fullClientSettings //generate a new struct
|
||||||
|
|
||||||
var Config fullClientSettings
|
var Config fullClientSettings
|
||||||
|
|
||||||
|
|
||||||
Config.version = 1.0
|
Config.version = 1.0
|
||||||
Config.DataDir = "downloads" //the full OR relative path of the default download directory for torrents
|
Config.DataDir = "downloads" //the full OR relative path of the default download directory for torrents
|
||||||
|
Config.tFileUploadFolder = "uploadedTorrents"
|
||||||
|
|
||||||
Config.DHTConfig = dht.ServerConfig{
|
Config.DHTConfig = dht.ServerConfig{
|
||||||
StartingNodes: dht.GlobalBootstrapAddrs,
|
StartingNodes: dht.GlobalBootstrapAddrs,
|
||||||
|
59
storage.go
59
storage.go
@@ -8,27 +8,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TorrentLocal struct { //local storage of the torrents for readd on server restart
|
type TorrentLocal struct { //local storage of the torrents for readd on server restart
|
||||||
Hash string
|
Hash string
|
||||||
DateAdded string
|
DateAdded string
|
||||||
StoragePath string
|
StoragePath string
|
||||||
TorrentName string
|
TorrentName string
|
||||||
TorrentStatus string
|
TorrentStatus string
|
||||||
TorrentType string //magnet or .torrent file
|
TorrentType string //magnet or .torrent file
|
||||||
|
TorrentFileName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func readInTorrents(torrentStorage *bolt.DB) (TorrentLocalArray []*TorrentLocal) {
|
func readInTorrents(torrentStorage *bolt.DB) (torrentLocalArray []*TorrentLocal) {
|
||||||
|
|
||||||
TorrentLocalArray = []*TorrentLocal{}
|
torrentLocalArray = []*TorrentLocal{}
|
||||||
|
|
||||||
torrentStorage.View(func(tx *bolt.Tx) error {
|
torrentStorage.View(func(tx *bolt.Tx) error {
|
||||||
|
|
||||||
tx.ForEach(func(name []byte, b *bolt.Bucket) error {
|
tx.ForEach(func(name []byte, b *bolt.Bucket) error {
|
||||||
torrentLocal := new(TorrentLocal) //create a struct to store to an array
|
torrentLocal := new(TorrentLocal) //create a struct to store to an array //TODO clean this the fuck up just read the struct into something that converts them all the byte arrays
|
||||||
var Dateadded []byte
|
var Dateadded []byte
|
||||||
var StoragePath []byte
|
var StoragePath []byte
|
||||||
var Hash []byte
|
var Hash []byte
|
||||||
var TorrentName []byte
|
var TorrentName []byte
|
||||||
var TorrentStatus []byte
|
var TorrentStatus []byte
|
||||||
|
var TorrentType []byte
|
||||||
|
var TorrentFileName []byte
|
||||||
Dateadded = b.Get([]byte("Date"))
|
Dateadded = b.Get([]byte("Date"))
|
||||||
if Dateadded == nil {
|
if Dateadded == nil {
|
||||||
fmt.Println("Date added error!")
|
fmt.Println("Date added error!")
|
||||||
@@ -53,21 +56,33 @@ func readInTorrents(torrentStorage *bolt.DB) (TorrentLocalArray []*TorrentLocal)
|
|||||||
fmt.Println("Torrent Status not found in local storage")
|
fmt.Println("Torrent Status not found in local storage")
|
||||||
TorrentStatus = []byte("")
|
TorrentStatus = []byte("")
|
||||||
}
|
}
|
||||||
|
TorrentType = b.Get([]byte("TorrentType"))
|
||||||
|
if TorrentType == nil {
|
||||||
|
fmt.Println("Torrent Type not found in local storage")
|
||||||
|
TorrentStatus = []byte("")
|
||||||
|
}
|
||||||
|
TorrentFileName = b.Get([]byte("TorrentFileName"))
|
||||||
|
if TorrentFileName == nil {
|
||||||
|
fmt.Println("Torrent File Name not found in local storage")
|
||||||
|
TorrentFileName = []byte("")
|
||||||
|
}
|
||||||
|
|
||||||
torrentLocal.DateAdded = string(Dateadded)
|
torrentLocal.DateAdded = string(Dateadded)
|
||||||
torrentLocal.StoragePath = string(StoragePath)
|
torrentLocal.StoragePath = string(StoragePath)
|
||||||
torrentLocal.Hash = string(Hash) //Converting the byte slice back into the full hash
|
torrentLocal.Hash = string(Hash) //Converting the byte slice back into the full hash
|
||||||
torrentLocal.TorrentName = string(TorrentName)
|
torrentLocal.TorrentName = string(TorrentName)
|
||||||
torrentLocal.TorrentStatus = string(TorrentStatus)
|
torrentLocal.TorrentStatus = string(TorrentStatus)
|
||||||
|
torrentLocal.TorrentType = string(TorrentType)
|
||||||
|
torrentLocal.TorrentFileName = string(TorrentFileName)
|
||||||
|
|
||||||
fmt.Println("Torrentlocal list: ", torrentLocal)
|
fmt.Println("Torrentlocal list: ", torrentLocal)
|
||||||
TorrentLocalArray = append(TorrentLocalArray, torrentLocal) //dumping it into the array
|
torrentLocalArray = append(torrentLocalArray, torrentLocal) //dumping it into the array
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return TorrentLocalArray //all done, return the entire Array to add to the torrent client
|
return torrentLocalArray //all done, return the entire Array to add to the torrent client
|
||||||
}
|
}
|
||||||
|
|
||||||
func addTorrentLocalStorage(torrentStorage *bolt.DB, local *TorrentLocal) {
|
func addTorrentLocalStorage(torrentStorage *bolt.DB, local *TorrentLocal) {
|
||||||
@@ -94,7 +109,27 @@ func addTorrentLocalStorage(torrentStorage *bolt.DB, local *TorrentLocal) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = b.Put([]byte("TorrentType"), []byte(local.TorrentType))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = b.Put([]byte("TorrentFileName"), []byte(local.TorrentFileName))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func delTorrentLocalStorage(torrentStorage *bolt.DB, local *TorrentLocal) { //deleting a torrent by hash
|
||||||
|
println("Deleting torrent", local.TorrentFileName)
|
||||||
|
|
||||||
|
torrentStorage.Update(func(tx *bolt.Tx) error {
|
||||||
|
err := tx.DeleteBucket([]byte(local.Hash))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
40
torrent-project/src/progressBarCell.js
Normal file
40
torrent-project/src/progressBarCell.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { TableCell } from 'material-ui';
|
||||||
|
import { withStyles } from 'material-ui/styles';
|
||||||
|
|
||||||
|
const styles = theme => ({
|
||||||
|
progressBarCell: {
|
||||||
|
paddingLeft: theme.spacing.unit,
|
||||||
|
paddingRight: theme.spacing.unit,
|
||||||
|
borderBottom: `1px solid ${theme.palette.text.lightDivider}`,
|
||||||
|
},
|
||||||
|
progressBar: {
|
||||||
|
backgroundColor: theme.palette.primary[300],
|
||||||
|
float: 'left',
|
||||||
|
height: theme.spacing.unit,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ProgressBarCellBase = ({ value, classes, style }) => (
|
||||||
|
<TableCell
|
||||||
|
className={classes.progressBarCell}
|
||||||
|
style={style}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={classes.progressBar}
|
||||||
|
style={{ width: `${value}%` }}
|
||||||
|
title={`${value.toFixed(1)}%`}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
|
);
|
||||||
|
ProgressBarCellBase.propTypes = {
|
||||||
|
value: PropTypes.number.isRequired,
|
||||||
|
classes: PropTypes.object.isRequired,
|
||||||
|
style: PropTypes.object,
|
||||||
|
};
|
||||||
|
ProgressBarCellBase.defaultProps = {
|
||||||
|
style: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ProgressBarCell = withStyles(styles, { name: 'ProgressBarCell' })(ProgressBarCellBase);
|
@@ -12,6 +12,7 @@ import {
|
|||||||
DragDropContext, TableColumnReordering,
|
DragDropContext, TableColumnReordering,
|
||||||
} from '@devexpress/dx-react-grid-material-ui';
|
} from '@devexpress/dx-react-grid-material-ui';
|
||||||
|
|
||||||
|
import { ProgressBarCell } from './ProgressBarCell';
|
||||||
//react redux
|
//react redux
|
||||||
import {connect} from 'react-redux';
|
import {connect} from 'react-redux';
|
||||||
import * as actionTypes from './store/actions';
|
import * as actionTypes from './store/actions';
|
||||||
@@ -51,11 +52,13 @@ ws.onmessage = function (evt) //When we recieve a message from the websocket
|
|||||||
torrents = []; //clearing out the torrent array to make room for new (so that it does keep adding)
|
torrents = []; //clearing out the torrent array to make room for new (so that it does keep adding)
|
||||||
for(var i = 0; i < clientUpdate.total; i++){
|
for(var i = 0; i < clientUpdate.total; i++){
|
||||||
//console.log("TorrentName: ", clientUpdate.data[i].TorrentName)
|
//console.log("TorrentName: ", clientUpdate.data[i].TorrentName)
|
||||||
|
console.log("PercentDone: ", clientUpdate.data[i].PercentDone)
|
||||||
torrents.push({
|
torrents.push({
|
||||||
TorrentHashString: clientUpdate.data[i].TorrentHashString,
|
TorrentHashString: clientUpdate.data[i].TorrentHashString,
|
||||||
TorrentName: clientUpdate.data[i].TorrentName,
|
TorrentName: clientUpdate.data[i].TorrentName,
|
||||||
DownloadedSize: clientUpdate.data[i].DownloadedSize,
|
DownloadedSize: clientUpdate.data[i].DownloadedSize,
|
||||||
DownloadSpeed: clientUpdate.data[i].DownloadedSpeed,
|
Size: clientUpdate.data[i].Size,
|
||||||
|
DownloadSpeed: clientUpdate.data[i].DownloadSpeed,
|
||||||
UploadSpeed: clientUpdate.data[i].UploadSpeed,
|
UploadSpeed: clientUpdate.data[i].UploadSpeed,
|
||||||
PercentDone: clientUpdate.data[i].PercentDone,
|
PercentDone: clientUpdate.data[i].PercentDone,
|
||||||
StoragePath: clientUpdate.data[i].StoragePath,
|
StoragePath: clientUpdate.data[i].StoragePath,
|
||||||
@@ -96,10 +99,10 @@ class TorrentListTable extends React.Component {
|
|||||||
{ name: 'TorrentName', title: 'Torrent Name' },
|
{ name: 'TorrentName', title: 'Torrent Name' },
|
||||||
{ name: 'DownloadedSize', title: 'Dl Size'},
|
{ name: 'DownloadedSize', title: 'Dl Size'},
|
||||||
{ name: 'Size', title: 'Size'},
|
{ name: 'Size', title: 'Size'},
|
||||||
{ name: 'Done', title: 'Percent Done'},
|
{ name: 'PercentDone', title: 'Percent Done'},
|
||||||
{ name: 'Status', title: 'Status'},
|
{ name: 'Status', title: 'Status'},
|
||||||
{ name: 'DownloadSpeed', title: 'Download Speed'},
|
{ name: 'DownloadSpeed', title: 'DL Speed'},
|
||||||
{ name: 'UploadSpeed', title: 'Upload Speed'},
|
{ name: 'UploadSpeed', title: 'UL Speed'},
|
||||||
{ name: 'ActivePeers', title: 'Active Peers' },
|
{ name: 'ActivePeers', title: 'Active Peers' },
|
||||||
{ name: 'TotalPeers', title: 'Total Peers' },
|
{ name: 'TotalPeers', title: 'Total Peers' },
|
||||||
{ name: 'ETA', title: 'ETA'},
|
{ name: 'ETA', title: 'ETA'},
|
||||||
@@ -107,8 +110,8 @@ class TorrentListTable extends React.Component {
|
|||||||
{ name: 'Availability', title: 'Availability'},
|
{ name: 'Availability', title: 'Availability'},
|
||||||
{ name: 'TorrentHashString', title: 'Torrent Hash' }
|
{ name: 'TorrentHashString', title: 'Torrent Hash' }
|
||||||
],
|
],
|
||||||
columnOrder: ['TorrentName', 'DownloadedSize', 'Size', 'Done', 'Status', 'DownloadSpeed', 'UploadSpeed','ActivePeers', 'TotalPeers', 'ETA', 'Ratio', 'Availability', 'TorrentHashString'],
|
columnOrder: ['TorrentName', 'DownloadedSize', 'Size', 'PercentDone', 'Status', 'DownloadSpeed', 'UploadSpeed','ActivePeers', 'TotalPeers', 'ETA', 'Ratio', 'Availability', 'TorrentHashString'],
|
||||||
columnWidths: {TorrentName: 250, DownloadedSize: 175, Size: 175, Done: 175, Status: 250, DownloadSpeed: 100, UploadSpeed: 100, ActivePeers: 100, TotalPeers: 100, ETA: 175, Ratio: 175, Availability: 175, TorrentHashString: 250,}
|
columnWidths: {TorrentName: 250, DownloadedSize: 175, Size: 175, PercentDone: 175, Status: 250, DownloadSpeed: 100, UploadSpeed: 100, ActivePeers: 100, TotalPeers: 100, ETA: 175, Ratio: 175, Availability: 175, TorrentHashString: 250,}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -145,7 +148,14 @@ class TorrentListTable extends React.Component {
|
|||||||
<SortingState sorting={this.props.sorting} onSortingChange={this.props.changeSorting} />
|
<SortingState sorting={this.props.sorting} onSortingChange={this.props.changeSorting} />
|
||||||
<LocalSorting />
|
<LocalSorting />
|
||||||
<SelectionState onSelectionChange={this.props.changeSelection} selection={this.props.selection}/>
|
<SelectionState onSelectionChange={this.props.changeSelection} selection={this.props.selection}/>
|
||||||
<TableView />
|
<TableView tableCellTemplate={({ row, column, style }) => {
|
||||||
|
if (column.name === 'PercentDone') {
|
||||||
|
return (
|
||||||
|
<ProgressBarCell value={row.PercentDone * 100} style={style} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}}/>
|
||||||
<DragDropContext />
|
<DragDropContext />
|
||||||
<TableColumnResizing columnWidths={this.state.columnWidths} onColumnWidthsChange={this.changeColumnWidths}/>
|
<TableColumnResizing columnWidths={this.state.columnWidths} onColumnWidthsChange={this.changeColumnWidths}/>
|
||||||
<TableColumnReordering order={this.state.columnOrder} onOrderChange={this.changeColumnOrder} />
|
<TableColumnReordering order={this.state.columnOrder} onOrderChange={this.changeColumnOrder} />
|
||||||
|
Reference in New Issue
Block a user