Adding logic to change torrent storage path
This commit is contained in:
@@ -9,15 +9,17 @@ import (
|
||||
|
||||
//All the message types are first, first the server handling messages from the client
|
||||
|
||||
//Message contains the JSON messages from the client, we first unmarshal to get the messagetype, then each module unmarshalls the actual message once we know the type
|
||||
//Message contains the JSON messages from the client, we first unmarshal to get the messagetype, then each module un-marshalls the actual message once we know the type
|
||||
type Message struct {
|
||||
MessageType string
|
||||
MessageDetail string `json:",omitempty"`
|
||||
MessageDetailTwo string `json:",omitempty"`
|
||||
Payload []string
|
||||
MessageType string
|
||||
MessageDetail string `json:",omitempty"`
|
||||
MessageDetailTwo string `json:",omitempty"`
|
||||
MessageDetailThree string `json:",omitempty"`
|
||||
Payload []string
|
||||
}
|
||||
|
||||
//Next are the messages the server sends to the client
|
||||
|
||||
//ServerPushMessage is information (usually logs and status messages) that the server pushes to the client
|
||||
type ServerPushMessage struct {
|
||||
MessageType string
|
||||
@@ -70,7 +72,7 @@ type TorrentFile struct {
|
||||
}
|
||||
|
||||
//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
|
||||
type ClientDB struct { //TODO maybe separate 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
|
||||
@@ -83,7 +85,7 @@ type ClientDB struct { //TODO maybe seperate out the internal bits into another
|
||||
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
|
||||
TorrentLabel 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
|
||||
|
@@ -40,7 +40,7 @@ func CheckTorrentWatchFolder(c *cron.Cron, db *storm.DB, tclient *torrent.Client
|
||||
break //break out of the loop entirely for this message since we hit an error
|
||||
}
|
||||
fullNewFilePath := filepath.Join(config.TFileUploadFolder, file.Name())
|
||||
StartTorrent(clientTorrent, torrentLocalStorage, db, config.TorrentConfig.DataDir, "file", file.Name(), config.DefaultMoveFolder)
|
||||
StartTorrent(clientTorrent, torrentLocalStorage, db, config.TorrentConfig.DataDir, "file", file.Name(), config.DefaultMoveFolder, "default")
|
||||
CopyFile(fullFilePath, fullNewFilePath)
|
||||
os.Remove(fullFilePath) //delete the torrent after adding it and copying it over
|
||||
Logger.WithFields(logrus.Fields{"Source Folder": config.TorrentWatchFolder, "Destination Folder": config.TFileUploadFolder, "Torrent": file.Name()}).Info("Added torrent from watch folder, and moved torrent file")
|
||||
@@ -78,7 +78,7 @@ func RefreshRSSCron(c *cron.Cron, db *storm.DB, tclient *torrent.Client, torrent
|
||||
Logger.WithFields(logrus.Fields{"err": err, "Torrent": RSSTorrent.Title}).Warn("Unable to add torrent to torrent client!")
|
||||
break //break out of the loop entirely for this message since we hit an error
|
||||
}
|
||||
StartTorrent(clientTorrent, torrentLocalStorage, db, config.TorrentConfig.DataDir, "magnet", "", config.DefaultMoveFolder) //TODO let user specify torrent default storage location and let change on fly
|
||||
StartTorrent(clientTorrent, torrentLocalStorage, db, config.TorrentConfig.DataDir, "magnet", "", config.DefaultMoveFolder, "RSS") //TODO let user specify torrent default storage location and let change on fly
|
||||
singleFeed.Torrents = append(singleFeed.Torrents, singleRSSTorrent)
|
||||
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/asdine/storm"
|
||||
Storage "github.com/deranjer/goTorrent/storage"
|
||||
pushbullet "github.com/mitsuse/pushbullet-go"
|
||||
@@ -16,11 +15,11 @@ import (
|
||||
)
|
||||
|
||||
//MoveAndLeaveSymlink takes the file from the default download dir and moves it to the user specified directory and then leaves a symlink behind.
|
||||
func MoveAndLeaveSymlink(config FullClientSettings, singleTorrent *torrent.Torrent, db *storm.DB) {
|
||||
Logger.WithFields(logrus.Fields{"Torrent Name": singleTorrent.Name()}).Info("Move and Create symlink started for torrent")
|
||||
tStorage := Storage.FetchTorrentFromStorage(db, singleTorrent.InfoHash().String())
|
||||
oldFilePath := filepath.Join(config.TorrentConfig.DataDir, singleTorrent.Name())
|
||||
newFilePath := filepath.Join(tStorage.StoragePath, singleTorrent.Name())
|
||||
func MoveAndLeaveSymlink(config FullClientSettings, tHash string, db *storm.DB) {
|
||||
tStorage := Storage.FetchTorrentFromStorage(db, tHash)
|
||||
Logger.WithFields(logrus.Fields{"Torrent Name": tStorage.TorrentFileName}).Info("Move and Create symlink started for torrent")
|
||||
oldFilePath := filepath.Join(config.TorrentConfig.DataDir, tStorage.TorrentFileName)
|
||||
newFilePath := filepath.Join(tStorage.StoragePath, tStorage.TorrentFileName)
|
||||
_, err := os.Stat(tStorage.StoragePath)
|
||||
if os.IsNotExist(err) {
|
||||
err := os.MkdirAll(tStorage.StoragePath, 0755)
|
||||
@@ -40,7 +39,7 @@ func MoveAndLeaveSymlink(config FullClientSettings, singleTorrent *torrent.Torre
|
||||
os.Mkdir(newFilePath, 0755)
|
||||
folderCopy.Copy(oldFilePath, newFilePath) //copy the folder to the new location
|
||||
os.Chmod(newFilePath, 0777)
|
||||
notifyUser(tStorage, config, singleTorrent, db)
|
||||
notifyUser(tStorage, config, db)
|
||||
return
|
||||
}
|
||||
srcFile, err := os.Open(oldFilePath)
|
||||
@@ -66,7 +65,7 @@ func MoveAndLeaveSymlink(config FullClientSettings, singleTorrent *torrent.Torre
|
||||
Logger.WithFields(logrus.Fields{"Old File Path": oldFilePath, "New File Path": newFilePath, "error": err}).Error("Windows: Error syncing new file to disk")
|
||||
}
|
||||
Logger.WithFields(logrus.Fields{"Old File Path": oldFilePath, "New File Path": newFilePath, "bytesWritten": bytesWritten}).Info("Windows Torrent Copy Completed")
|
||||
notifyUser(tStorage, config, singleTorrent, db)
|
||||
notifyUser(tStorage, config, db)
|
||||
} else {
|
||||
folderCopy.Copy(oldFilePath, newFilePath)
|
||||
os.Chmod(newFilePath, 0777) //changing permissions on the new file to be permissive
|
||||
@@ -76,28 +75,28 @@ func MoveAndLeaveSymlink(config FullClientSettings, singleTorrent *torrent.Torre
|
||||
Logger.WithFields(logrus.Fields{"Old File Path": oldFilePath, "New File Path": newFilePath, "error": err}).Error("Error creating symlink")
|
||||
return
|
||||
}
|
||||
notifyUser(tStorage, config, singleTorrent, db)
|
||||
notifyUser(tStorage, config, db)
|
||||
Logger.WithFields(logrus.Fields{"Old File Path": oldFilePath, "New File Path": newFilePath}).Info("Moving completed torrent")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func notifyUser(tStorage Storage.TorrentLocal, config FullClientSettings, singleTorrent *torrent.Torrent, db *storm.DB) {
|
||||
Logger.WithFields(logrus.Fields{"New File Path": tStorage.StoragePath, "Torrent Name": singleTorrent.Name()}).Info("Attempting to notify user..")
|
||||
func notifyUser(tStorage Storage.TorrentLocal, config FullClientSettings, db *storm.DB) {
|
||||
Logger.WithFields(logrus.Fields{"New File Path": tStorage.StoragePath, "Torrent Name": tStorage.TorrentFileName}).Info("Attempting to notify user..")
|
||||
tStorage.TorrentMoved = true
|
||||
Storage.AddTorrentLocalStorage(db, tStorage) //Updating the fact that we moved the torrent
|
||||
if config.PushBulletToken != "" {
|
||||
pb := pushbullet.New(config.PushBulletToken)
|
||||
n := requests.NewNote()
|
||||
n.Title = singleTorrent.Name()
|
||||
n.Title = tStorage.TorrentFileName
|
||||
n.Body = "Completed and moved to " + tStorage.StoragePath
|
||||
if _, err := pb.PostPushesNote(n); err != nil {
|
||||
Logger.WithFields(logrus.Fields{"Torrent": singleTorrent.Name(), "New File Path": tStorage.StoragePath, "error": err}).Error("Error pushing PushBullet Note")
|
||||
Logger.WithFields(logrus.Fields{"Torrent": tStorage.TorrentFileName, "New File Path": tStorage.StoragePath, "error": err}).Error("Error pushing PushBullet Note")
|
||||
return
|
||||
}
|
||||
Logger.WithFields(logrus.Fields{"Torrent": singleTorrent.Name(), "New File Path": tStorage.StoragePath}).Info("Pushbullet note sent")
|
||||
Logger.WithFields(logrus.Fields{"Torrent": tStorage.TorrentFileName, "New File Path": tStorage.StoragePath}).Info("Pushbullet note sent")
|
||||
} else {
|
||||
Logger.WithFields(logrus.Fields{"New File Path": tStorage.StoragePath, "Torrent Name": singleTorrent.Name()}).Info("No pushbullet API key set, not notifying")
|
||||
Logger.WithFields(logrus.Fields{"New File Path": tStorage.StoragePath, "Torrent Name": tStorage.TorrentFileName}).Info("No pushbullet API key set, not notifying")
|
||||
}
|
||||
}
|
||||
|
46
engine/doneTorrentActions_test.go
Normal file
46
engine/doneTorrentActions_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/asdine/storm"
|
||||
Storage "github.com/deranjer/goTorrent/storage"
|
||||
)
|
||||
|
||||
func TestMoveAndLeaveSymlink(t *testing.T) {
|
||||
type args struct {
|
||||
config FullClientSettings
|
||||
tStorage Storage.TorrentLocal
|
||||
db *storm.DB
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
MoveAndLeaveSymlink(tt.args.config, tt.args.tStorage, tt.args.db)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_notifyUser(t *testing.T) {
|
||||
type args struct {
|
||||
tStorage Storage.TorrentLocal
|
||||
config FullClientSettings
|
||||
db *storm.DB
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
notifyUser(tt.args.tStorage, tt.args.config, tt.args.db)
|
||||
})
|
||||
}
|
||||
}
|
@@ -84,7 +84,7 @@ func timeOutInfo(clientTorrent *torrent.Torrent, seconds time.Duration) (deleted
|
||||
}()
|
||||
select {
|
||||
case <-clientTorrent.GotInfo(): //attempting to retrieve info for torrent
|
||||
Logger.WithFields(logrus.Fields{"clientTorrentName": clientTorrent.Name()}).Debug("Recieved torrent info for torrent")
|
||||
Logger.WithFields(logrus.Fields{"clientTorrentName": clientTorrent.Name()}).Debug("Received torrent info for torrent")
|
||||
clientTorrent.DownloadAll()
|
||||
return false
|
||||
case <-timeout: // getting info for torrent has timed out so purging the torrent
|
||||
@@ -123,8 +123,8 @@ func readTorrentFileFromDB(element *Storage.TorrentLocal, tclient *torrent.Clien
|
||||
}
|
||||
|
||||
//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 *storm.DB, dataDir string, torrentType string, torrentFileName string, torrentStoragePath string) {
|
||||
timedOut := timeOutInfo(clientTorrent, 45) //seeing if adding the torrrent times out (giving 45 seconds)
|
||||
func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage Storage.TorrentLocal, torrentDbStorage *storm.DB, dataDir string, torrentType string, torrentFileName string, torrentStoragePath string, labelValue string) {
|
||||
timedOut := timeOutInfo(clientTorrent, 45) //seeing if adding the torrent times out (giving 45 seconds)
|
||||
if timedOut { //if we fail to add the torrent return
|
||||
return
|
||||
}
|
||||
@@ -139,6 +139,7 @@ func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage Storage.To
|
||||
}
|
||||
torrentLocalStorage.Hash = TempHash.String() // we will store the infohash to add it back later on client restart (if needed)
|
||||
torrentLocalStorage.InfoBytes = clientTorrent.Metainfo().InfoBytes
|
||||
torrentLocalStorage.Label = labelValue
|
||||
torrentLocalStorage.DateAdded = time.Now().Format("Jan _2 2006")
|
||||
torrentLocalStorage.StoragePath = torrentStoragePath
|
||||
torrentLocalStorage.TorrentName = clientTorrent.Name()
|
||||
@@ -190,7 +191,7 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
}
|
||||
if len(singleTorrentFromStorage.InfoBytes) == 0 { //TODO.. kind of a fringe scenario.. not sure if needed since the db should always have the infobytes
|
||||
timeOut := timeOutInfo(singleTorrent, 45)
|
||||
if timeOut == true { // if we did timeout then drop the torrent from the boltdb database
|
||||
if timeOut == true { // if we did timeout then drop the torrent from the bolt.db database
|
||||
Storage.DelTorrentLocalStorage(db, singleTorrentFromStorage.Hash) //purging torrent from the local database
|
||||
continue
|
||||
}
|
||||
@@ -204,8 +205,8 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
//Logger.WithFields(logrus.Fields{"singleTorrent": singleTorrentFromStorage.TorrentName}).Info("Generating infohash")
|
||||
TempHash = singleTorrent.InfoHash()
|
||||
|
||||
if (singleTorrent.BytesCompleted() == singleTorrent.Length()) && (singleTorrentFromStorage.TorrentMoved == false) { //if we are done downloading and havent moved torrent yet
|
||||
MoveAndLeaveSymlink(config, singleTorrent, db) //can take some time to move file so running this in another thread TODO make this a goroutine and skip this block if the routine is still running
|
||||
if (singleTorrent.BytesCompleted() == singleTorrent.Length()) && (singleTorrentFromStorage.TorrentMoved == false) { //if we are done downloading and haven't moved torrent yet
|
||||
MoveAndLeaveSymlink(config, singleTorrent.InfoHash().String(), db) //can take some time to move file so running this in another thread TODO make this a goroutine and skip this block if the routine is still running
|
||||
}
|
||||
|
||||
fullStruct := singleTorrent.Stats()
|
||||
@@ -232,6 +233,7 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
fullClientDB.StoragePath = singleTorrentFromStorage.StoragePath
|
||||
fullClientDB.TorrentName = singleTorrentFromStorage.TorrentName
|
||||
fullClientDB.DateAdded = singleTorrentFromStorage.DateAdded
|
||||
fullClientDB.TorrentLabel = singleTorrentFromStorage.Label
|
||||
fullClientDB.BytesCompleted = singleTorrent.BytesCompleted()
|
||||
fullClientDB.NumberofFiles = len(singleTorrent.Files())
|
||||
|
||||
@@ -245,7 +247,7 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
}
|
||||
}
|
||||
}
|
||||
CalculateTorrentETA(singleTorrent, fullClientDB) //needs to be here since we need the speed calcuated before we can estimate the eta.
|
||||
CalculateTorrentETA(singleTorrent, fullClientDB) //needs to be here since we need the speed calculated before we can estimate the eta.
|
||||
|
||||
fullClientDB.TotalUploadedSize = HumanizeBytes(float32(fullClientDB.TotalUploadedBytes))
|
||||
fullClientDB.UploadRatio = CalculateUploadRatio(singleTorrent, fullClientDB) //calculate the upload ratio
|
||||
@@ -343,3 +345,19 @@ func CreateTorrentDetailJSON(tclient *torrent.Client, selectedHash string, torre
|
||||
}
|
||||
return TorrentDetailStruct
|
||||
}
|
||||
|
||||
func ChangeStorageLocation(newStorageLocation string, torrentHashes []string, db *storm.DB) {
|
||||
|
||||
for _, torrentHash := range torrentHashes {
|
||||
var selectedTorrent Storage.TorrentLocal
|
||||
err := db.Find("TorrentLocal", torrentHash, &selectedTorrent)
|
||||
if err != nil {
|
||||
Logger.WithFields(logrus.Fields{"torrentHash": torrentHash}).Error("Unable to find torrent in db to update!")
|
||||
CreateServerPushMessage(ServerPushMessage{MessageType: "serverPushMessage", MessageLevel: "error", Payload: "Failed to change Storage Location"}, Conn)
|
||||
}
|
||||
fmt.Printf("%+v\n", selectedTorrent)
|
||||
db.UpdateField(&selectedTorrent{Hash: torrentHash}, "StoragePath", newStorageLocation)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user