mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
init viewed api
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
func AddInfo(hash, info string) error {
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -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)
|
||||
|
||||
150
src/server/settings/Viewed.go
Normal file
150
src/server/settings/Viewed.go
Normal 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
|
||||
}
|
||||
@@ -32,6 +32,10 @@ func initTorrent(e *echo.Echo) {
|
||||
e.POST("/torrent/cache", torrentCache)
|
||||
e.POST("/torrent/drop", torrentDrop)
|
||||
|
||||
e.POST("/torrent/viewed/add", torrentViewedAdd)
|
||||
e.POST("/torrent/viewed/remove", torrentViewedRem)
|
||||
e.GET("/torrent/viewed/list", torrentViewedList)
|
||||
|
||||
e.GET("/torrent/restart", torrentRestart)
|
||||
|
||||
e.GET("/torrent/playlist.m3u", torrentPlayListAll)
|
||||
@@ -441,6 +445,46 @@ func torrentDrop(c echo.Context) error {
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func torrentViewedAdd(c echo.Context) error {
|
||||
jreq, err := getJsReqTorr(c)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
if jreq.Hash == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Hash must be non-empty")
|
||||
}
|
||||
|
||||
err = settings.SetViewed(jreq.Hash)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func torrentViewedRem(c echo.Context) error {
|
||||
jreq, err := getJsReqTorr(c)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
if jreq.Hash == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Hash must be non-empty")
|
||||
}
|
||||
|
||||
err = settings.RemTorrentViewed(jreq.Hash)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func torrentViewedList(c echo.Context) error {
|
||||
err = settings.List(jreq.Hash)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func torrentRestart(c echo.Context) error {
|
||||
fmt.Println("Restart torrent engine")
|
||||
err := bts.Reconnect()
|
||||
@@ -611,10 +655,9 @@ func toTorrentDB(t *torr.Torrent) *settings.Torrent {
|
||||
|
||||
for _, f := range st.FileStats {
|
||||
tf := settings.File{
|
||||
Id: f.Id,
|
||||
Name: f.Path,
|
||||
Size: f.Length,
|
||||
Viewed: false,
|
||||
Id: f.Id,
|
||||
Name: f.Path,
|
||||
Size: f.Length,
|
||||
}
|
||||
tor.Files = append(tor.Files, tf)
|
||||
}
|
||||
@@ -643,7 +686,7 @@ func getTorrentJS(tor *settings.Torrent) (*TorrentJsonResponse, error) {
|
||||
Play: "/torrent/play/" + utils.CleanFName(f.Name) + "?link=" + mag.String() + "&file=" + fmt.Sprint(f.Id),
|
||||
Preload: "/torrent/play/" + utils.CleanFName(f.Name) + "?link=" + mag.String() + "&file=" + fmt.Sprint(f.Id) + "&preload=true",
|
||||
Size: f.Size,
|
||||
Viewed: f.Viewed,
|
||||
Viewed: settings.GetViewed(tor.Hash, f.Name),
|
||||
}
|
||||
js.Files = append(js.Files, tf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user