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"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db *bolt.DB
|
db *bolt.DB
|
||||||
|
dbViewedName = []byte("Viewed")
|
||||||
dbInfosName = []byte("Infos")
|
dbInfosName = []byte("Infos")
|
||||||
dbTorrentsName = []byte("Torrents")
|
dbTorrentsName = []byte("Torrents")
|
||||||
dbSettingsName = []byte("Settings")
|
dbSettingsName = []byte("Settings")
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddInfo(hash, info string) error {
|
func AddInfo(hash, info string) error {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Torrent struct {
|
type Torrent struct {
|
||||||
@@ -19,44 +19,9 @@ type Torrent struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string
|
Name string
|
||||||
Id int
|
Id int
|
||||||
Size int64
|
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
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveTorrentDB(torrent *Torrent) error {
|
func SaveTorrentDB(torrent *Torrent) error {
|
||||||
@@ -117,16 +82,6 @@ func SaveTorrentDB(torrent *Torrent) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error save torrent files: %v", err)
|
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
|
return nil
|
||||||
@@ -220,11 +175,6 @@ func LoadTorrentDB(hash string) (*Torrent, error) {
|
|||||||
}
|
}
|
||||||
file.Size = b2i(tmp)
|
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)
|
torr.Files = append(torr.Files, file)
|
||||||
}
|
}
|
||||||
SortFiles(torr.Files)
|
SortFiles(torr.Files)
|
||||||
@@ -297,11 +247,6 @@ func LoadTorrentsDB() ([]*Torrent, error) {
|
|||||||
}
|
}
|
||||||
file.Size = b2i(tmp)
|
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)
|
torr.Files = append(torr.Files, file)
|
||||||
}
|
}
|
||||||
SortFiles(torr.Files)
|
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/cache", torrentCache)
|
||||||
e.POST("/torrent/drop", torrentDrop)
|
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/restart", torrentRestart)
|
||||||
|
|
||||||
e.GET("/torrent/playlist.m3u", torrentPlayListAll)
|
e.GET("/torrent/playlist.m3u", torrentPlayListAll)
|
||||||
@@ -441,6 +445,46 @@ func torrentDrop(c echo.Context) error {
|
|||||||
return c.NoContent(http.StatusOK)
|
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 {
|
func torrentRestart(c echo.Context) error {
|
||||||
fmt.Println("Restart torrent engine")
|
fmt.Println("Restart torrent engine")
|
||||||
err := bts.Reconnect()
|
err := bts.Reconnect()
|
||||||
@@ -611,10 +655,9 @@ func toTorrentDB(t *torr.Torrent) *settings.Torrent {
|
|||||||
|
|
||||||
for _, f := range st.FileStats {
|
for _, f := range st.FileStats {
|
||||||
tf := settings.File{
|
tf := settings.File{
|
||||||
Id: f.Id,
|
Id: f.Id,
|
||||||
Name: f.Path,
|
Name: f.Path,
|
||||||
Size: f.Length,
|
Size: f.Length,
|
||||||
Viewed: false,
|
|
||||||
}
|
}
|
||||||
tor.Files = append(tor.Files, tf)
|
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),
|
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",
|
Preload: "/torrent/play/" + utils.CleanFName(f.Name) + "?link=" + mag.String() + "&file=" + fmt.Sprint(f.Id) + "&preload=true",
|
||||||
Size: f.Size,
|
Size: f.Size,
|
||||||
Viewed: f.Viewed,
|
Viewed: settings.GetViewed(tor.Hash, f.Name),
|
||||||
}
|
}
|
||||||
js.Files = append(js.Files, tf)
|
js.Files = append(js.Files, tf)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user