add migrate torrents from old server

This commit is contained in:
YouROK
2021-01-19 13:17:50 +03:00
parent 3ea0ca6dd0
commit b210178c51
2 changed files with 106 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
package settings
import (
"encoding/binary"
"fmt"
"os"
"path/filepath"
bolt "go.etcd.io/bbolt"
"server/web/api/utils"
)
var (
dbTorrentsName = []byte("Torrents")
)
type torrentOldDB struct {
Name string
Magnet string
InfoBytes []byte
Hash string
Size int64
Timestamp int64
}
func Migrate() {
if _, err := os.Lstat(filepath.Join(Path, "torrserver.db")); os.IsNotExist(err) {
return
}
db, err := bolt.Open(filepath.Join(Path, "torrserver.db"), 0666, nil)
if err != nil {
return
}
torrs := make([]*torrentOldDB, 0)
err = db.View(func(tx *bolt.Tx) error {
tdb := tx.Bucket(dbTorrentsName)
if tdb == nil {
return nil
}
c := tdb.Cursor()
for h, _ := c.First(); h != nil; h, _ = c.Next() {
hdb := tdb.Bucket(h)
if hdb != nil {
torr := new(torrentOldDB)
torr.Hash = string(h)
tmp := hdb.Get([]byte("Name"))
if tmp == nil {
return fmt.Errorf("error load torrent")
}
torr.Name = string(tmp)
tmp = hdb.Get([]byte("Link"))
if tmp == nil {
return fmt.Errorf("error load torrent")
}
torr.Magnet = string(tmp)
tmp = hdb.Get([]byte("Size"))
if tmp == nil {
return fmt.Errorf("error load torrent")
}
torr.Size = b2i(tmp)
tmp = hdb.Get([]byte("Timestamp"))
if tmp == nil {
return fmt.Errorf("error load torrent")
}
torr.Timestamp = b2i(tmp)
torrs = append(torrs, torr)
}
}
return nil
})
db.Close()
if err == nil && len(torrs) > 0 {
for _, torr := range torrs {
spec, err := utils.ParseLink(torr.Magnet)
if err != nil {
continue
}
title := torr.Name
if len(spec.DisplayName) > len(title) {
title = spec.DisplayName
}
AddTorrent(&TorrentDB{
TorrentSpec: spec,
Name: torr.Name,
Title: title,
Poster: "",
Timestamp: torr.Timestamp,
Size: torr.Size,
})
}
}
os.Remove(filepath.Join(Path, "torrserver.db"))
}
func b2i(v []byte) int64 {
return int64(binary.BigEndian.Uint64(v))
}

View File

@@ -8,6 +8,7 @@ var (
func InitSets(readOnly bool) { func InitSets(readOnly bool) {
tdb = NewTDB(readOnly) tdb = NewTDB(readOnly)
loadBTSets() loadBTSets()
Migrate()
} }
func CloseDB() { func CloseDB() {