init viewed api

This commit is contained in:
YouROK
2020-09-14 09:08:22 +03:00
parent 76ad8f6ade
commit fb4cf8a490
6 changed files with 206 additions and 67 deletions

View File

@@ -4,11 +4,12 @@ import (
"fmt"
"path/filepath"
"github.com/boltdb/bolt"
bolt "go.etcd.io/bbolt"
)
var (
db *bolt.DB
dbViewedName = []byte("Viewed")
dbInfosName = []byte("Infos")
dbTorrentsName = []byte("Torrents")
dbSettingsName = []byte("Settings")

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"github.com/boltdb/bolt"
bolt "go.etcd.io/bbolt"
)
func AddInfo(hash, info string) error {

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"time"
"github.com/boltdb/bolt"
bolt "go.etcd.io/bbolt"
)
var (

View File

@@ -4,7 +4,7 @@ import (
"encoding/binary"
"fmt"
"github.com/boltdb/bolt"
bolt "go.etcd.io/bbolt"
)
type Torrent struct {
@@ -19,44 +19,9 @@ type Torrent struct {
}
type File struct {
Name string
Id int
Size int64
Viewed bool
}
func SetViewed(hash, filename string) error {
err := openDB()
if err != nil {
return err
}
return db.Update(func(tx *bolt.Tx) error {
dbt := tx.Bucket(dbTorrentsName)
if dbt == nil {
return fmt.Errorf("could not find torrent")
}
hdb := dbt.Bucket([]byte(hash))
if hdb == nil {
return fmt.Errorf("could not find torrent")
}
fdb := hdb.Bucket([]byte("Files"))
if fdb == nil {
return fmt.Errorf("could not find torrent")
}
fdb = fdb.Bucket([]byte(filename))
if fdb == nil {
return fmt.Errorf("could not find torrent")
}
err = fdb.Put([]byte("Viewed"), []byte{1})
if err != nil {
return fmt.Errorf("error save torrent %v", err)
}
return nil
})
Name string
Id int
Size int64
}
func SaveTorrentDB(torrent *Torrent) error {
@@ -117,16 +82,6 @@ func SaveTorrentDB(torrent *Torrent) error {
if err != nil {
return fmt.Errorf("error save torrent files: %v", err)
}
b := 0
if f.Viewed {
b = 1
}
err = ffdb.Put([]byte("Viewed"), []byte{byte(b)})
if err != nil {
return fmt.Errorf("error save torrent files: %v", err)
}
}
return nil
@@ -220,11 +175,6 @@ func LoadTorrentDB(hash string) (*Torrent, error) {
}
file.Size = b2i(tmp)
tmp = ffdb.Get([]byte("Viewed"))
if tmp == nil {
return fmt.Errorf("error load torrent file")
}
file.Viewed = len(tmp) > 0 && tmp[0] == 1
torr.Files = append(torr.Files, file)
}
SortFiles(torr.Files)
@@ -297,11 +247,6 @@ func LoadTorrentsDB() ([]*Torrent, error) {
}
file.Size = b2i(tmp)
tmp = ffdb.Get([]byte("Viewed"))
if tmp == nil {
return fmt.Errorf("error load torrent file")
}
file.Viewed = len(tmp) > 0 && tmp[0] == 1
torr.Files = append(torr.Files, file)
}
SortFiles(torr.Files)

View File

@@ -0,0 +1,150 @@
package settings
import (
"fmt"
bolt "go.etcd.io/bbolt"
)
func SetViewed(hash, filename string) error {
err := openDB()
if err != nil {
return err
}
return db.Update(func(tx *bolt.Tx) error {
dbt, err := tx.CreateBucketIfNotExists(dbViewedName)
if err != nil {
return fmt.Errorf("could not find torrent")
}
hdb, err := dbt.CreateBucketIfNotExists([]byte(hash))
if err != nil {
return fmt.Errorf("could not find torrent")
}
fdb, err := hdb.CreateBucketIfNotExists([]byte("Files"))
if err != nil {
return fmt.Errorf("could not find torrent")
}
fdb, err = fdb.CreateBucketIfNotExists([]byte(filename))
if err != nil {
return fmt.Errorf("could not find torrent")
}
err = fdb.Put([]byte("Viewed"), []byte{1})
if err != nil {
return fmt.Errorf("error save torrent %v", err)
}
return nil
})
}
func RemTorrentViewed(hash string) error {
err := openDB()
if err != nil {
return err
}
return db.Update(func(tx *bolt.Tx) error {
dbt := tx.Bucket(dbViewedName)
if dbt == nil {
return nil
}
err = dbt.DeleteBucket([]byte(hash))
if err == nil || err == bolt.ErrBucketNotFound {
return nil
}
return err
})
}
func GetViewed(hash, filename string) bool {
err := openDB()
if err != nil {
return false
}
viewed := false
err = db.View(func(tx *bolt.Tx) error {
hdb := tx.Bucket(dbViewedName)
if hdb == nil {
return fmt.Errorf("could not find torrent")
}
hdb = hdb.Bucket([]byte(hash))
if hdb != nil {
fdb := hdb.Bucket([]byte("Files"))
if fdb == nil {
return fmt.Errorf("error load torrent files")
}
cf := fdb.Cursor()
for fn, _ := cf.First(); fn != nil; fn, _ = cf.Next() {
if string(fn) != filename {
continue
}
ffdb := fdb.Bucket(fn)
if ffdb == nil {
return fmt.Errorf("error load torrent files")
}
tmp := ffdb.Get([]byte("Viewed"))
if tmp == nil {
return fmt.Errorf("error load torrent file")
}
if len(tmp) > 0 && tmp[0] == 1 {
viewed = true
break
}
}
}
return nil
})
return viewed
}
func GetViewedList() []struct {
Hash string
File string
} {
err := openDB()
if err != nil {
return nil
}
viewed := false
err = db.View(func(tx *bolt.Tx) error {
hdb := tx.Bucket(dbViewedName)
if hdb == nil {
return fmt.Errorf("could not find torrent")
}
hdb = hdb.Bucket([]byte(hash))
if hdb != nil {
fdb := hdb.Bucket([]byte("Files"))
if fdb == nil {
return fmt.Errorf("error load torrent files")
}
cf := fdb.Cursor()
for fn, _ := cf.First(); fn != nil; fn, _ = cf.Next() {
if string(fn) != filename {
continue
}
ffdb := fdb.Bucket(fn)
if ffdb == nil {
return fmt.Errorf("error load torrent files")
}
tmp := ffdb.Get([]byte("Viewed"))
if tmp == nil {
return fmt.Errorf("error load torrent file")
}
if len(tmp) > 0 && tmp[0] == 1 {
viewed = true
break
}
}
}
return nil
})
return viewed
}