This commit is contained in:
YouROK
2021-09-08 12:20:18 +03:00
parent 42434c2f46
commit 52d7b7a10b

View File

@@ -1,214 +1,214 @@
package api package api
import ( import (
"fmt" "fmt"
"strings" "net/url"
"net/url" "path/filepath"
"path/filepath" "strings"
sets "server/settings" sets "server/settings"
"server/torr" "server/torr"
"server/torr/state" "server/torr/state"
"server/utils" "server/utils"
"server/version" "server/version"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type msxMenu struct { type msxMenu struct {
Logo string `json:"logo,omitempty"` Logo string `json:"logo,omitempty"`
Menu []msxMenuItem `json:"menu"` Menu []msxMenuItem `json:"menu"`
} }
type msxMenuItem struct { type msxMenuItem struct {
Icon string `json:"icon,omitempty"` Icon string `json:"icon,omitempty"`
Label string `json:"label,omitempty"` Label string `json:"label,omitempty"`
Data msxData `json:"data,omitempty"` Data msxData `json:"data,omitempty"`
} }
type msxData struct { type msxData struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Headline string `json:"headline,omitempty"` Headline string `json:"headline,omitempty"`
Action string `json:"action,omitempty"` Action string `json:"action,omitempty"`
Template gin.H `json:"template,omitempty"` Template gin.H `json:"template,omitempty"`
Items []msxItem `json:"items,omitempty"` Items []msxItem `json:"items,omitempty"`
Pages []msxPage `json:"pages,omitempty"` Pages []msxPage `json:"pages,omitempty"`
} }
type msxItem struct { type msxItem struct {
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
Label string `json:"label,omitempty"` Label string `json:"label,omitempty"`
PlayerLabel string `json:"playerLabel,omitempty"` PlayerLabel string `json:"playerLabel,omitempty"`
Action string `json:"action,omitempty"` Action string `json:"action,omitempty"`
Image string `json:"image,omitempty"` Image string `json:"image,omitempty"`
Icon string `json:"icon,omitempty"` Icon string `json:"icon,omitempty"`
Badge string `json:"badge,omitempty"` Badge string `json:"badge,omitempty"`
Tag string `json:"tag,omitempty"` Tag string `json:"tag,omitempty"`
} }
type msxPage struct { type msxPage struct {
Items []gin.H `json:"items,omitempty"` Items []gin.H `json:"items,omitempty"`
} }
func msxStart(c *gin.Context) { func msxStart(c *gin.Context) {
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"name": "TorrServer", "name": "TorrServer",
"version": version.Version, "version": version.Version,
"parameter": "menu:{PREFIX}{SERVER}/msx/torrents", "parameter": "menu:{PREFIX}{SERVER}/msx/torrents",
}) })
} }
// /msx/torrents // /msx/torrents
func msxTorrents(c *gin.Context) { func msxTorrents(c *gin.Context) {
torrs := torr.ListTorrent() torrs := torr.ListTorrent()
host := utils.GetScheme(c) + "://" + c.Request.Host host := utils.GetScheme(c) + "://" + c.Request.Host
logo := host + "/apple-touch-icon.png" logo := host + "/apple-touch-icon.png"
list := make([]msxItem, len(torrs)) list := make([]msxItem, len(torrs))
for i, tor := range torrs { for i, tor := range torrs {
item := msxItem{ item := msxItem{
Title: tor.Title, Title: tor.Title,
Image: tor.Poster, Image: tor.Poster,
Action: "content:" + host + "/msx/playlist/" + url.PathEscape(tor.Title) + "?hash=" + tor.TorrentSpec.InfoHash.HexString(), Action: "content:" + host + "/msx/playlist/" + url.PathEscape(tor.Title) + "?hash=" + tor.TorrentSpec.InfoHash.HexString(),
} }
list[i] = item list[i] = item
} }
c.JSON(200, msxMenu{ c.JSON(200, msxMenu{
Logo: logo, Logo: logo,
Menu: []msxMenuItem{ Menu: []msxMenuItem{
// Main page // Main page
{ {
Icon: "list", Icon: "list",
Label: "Torrents", Label: "Torrents",
Data: msxData{ Data: msxData{
Type: "pages", Type: "pages",
Template: gin.H{ Template: gin.H{
"type": "separate", "type": "separate",
"layout": "0,0,2,4", "layout": "0,0,2,4",
"icon": "msx-white-soft:movie", "icon": "msx-white-soft:movie",
"color": "msx-glass", "color": "msx-glass",
}, },
Items: list, Items: list,
}, },
// About // About
},{ }, {
Icon: "info", Icon: "info",
Label: "About", Label: "About",
Data: msxData{ Data: msxData{
Pages: []msxPage{ Pages: []msxPage{
{ {
Items: []gin.H{ Items: []gin.H{
{ {
"type": "default", "type": "default",
"headline": "TorrServer " + version.Version, "headline": "TorrServer " + version.Version,
"text": "https://github.com/YouROK/TorrServer", "text": "https://github.com/YouROK/TorrServer",
"image": logo, "image": logo,
"imageFiller": "height-left", "imageFiller": "height-left",
"imageWidth": 2, "imageWidth": 2,
"layout": "0,0,8,2", "layout": "0,0,8,2",
"color": "msx-gray-soft", "color": "msx-gray-soft",
}, },
}, },
}, },
}, },
}, },
}, },
}, },
}) })
} }
// /msx/playlist?hash=... // /msx/playlist?hash=...
func msxPlaylist(c *gin.Context) { func msxPlaylist(c *gin.Context) {
hash, _ := c.GetQuery("hash") hash, _ := c.GetQuery("hash")
if hash == "" { if hash == "" {
c.JSON(200, msxData{ c.JSON(200, msxData{
Action: "error:Item not found", Action: "error:Item not found",
}) })
return return
} }
tor := torr.GetTorrent(hash) tor := torr.GetTorrent(hash)
if tor == nil { if tor == nil {
c.JSON(200, msxData{ c.JSON(200, msxData{
Action: "error:Item not found", Action: "error:Item not found",
}) })
return return
} }
if tor.Stat == state.TorrentInDB { if tor.Stat == state.TorrentInDB {
tor = torr.LoadTorrent(tor) tor = torr.LoadTorrent(tor)
if tor == nil { if tor == nil {
c.JSON(200, msxData{ c.JSON(200, msxData{
Action: "error:Error while getting torrent info", Action: "error:Error while getting torrent info",
}) })
return return
} }
} }
host := utils.GetScheme(c) + "://" + c.Request.Host host := utils.GetScheme(c) + "://" + c.Request.Host
status := tor.Status() status := tor.Status()
viewed := sets.ListViewed(hash) viewed := sets.ListViewed(hash)
list := []msxItem{} var list []msxItem
for _, f := range status.FileStats { for _, f := range status.FileStats {
mime := utils.GetMimeType(f.Path) mime := utils.GetMimeType(f.Path)
action := mime[0 : len(mime)-2] action := mime[0 : len(mime)-2]
if (action == "*") { if action == "*" {
continue continue
} }
name := filepath.Base(f.Path) name := filepath.Base(f.Path)
item := msxItem{ item := msxItem{
Label: name, Label: name,
PlayerLabel: strings.TrimSuffix(name, filepath.Ext(name)), PlayerLabel: strings.TrimSuffix(name, filepath.Ext(name)),
Action: action + ":" + host + "/stream/" + url.PathEscape(name) + "?link=" + hash + "&index=" + fmt.Sprint(f.Id) + "&play", Action: action + ":" + host + "/stream/" + url.PathEscape(name) + "?link=" + hash + "&index=" + fmt.Sprint(f.Id) + "&play",
} }
if (isViewed(viewed, f.Id)) { if isViewed(viewed, f.Id) {
item.Tag = " " item.Tag = " "
} }
if (action == "audio") { if action == "audio" {
item.Icon = "msx-white-soft:music-note" item.Icon = "msx-white-soft:music-note"
} }
list = append(list, item) list = append(list, item)
} }
if (len(list) == 0) { if len(list) == 0 {
c.JSON(200, msxData{ c.JSON(200, msxData{
Action: "error:No supported content found", Action: "error:No supported content found",
}) })
return return
} }
res := msxData{ res := msxData{
Headline: tor.Title, Headline: tor.Title,
Type: "list", Type: "list",
Template: gin.H{ Template: gin.H{
"type": "control", "type": "control",
"layout": "0,2,12,1", "layout": "0,2,12,1",
"color": "msx-glass", "color": "msx-glass",
"icon": "msx-white-soft:movie", "icon": "msx-white-soft:movie",
"iconSize": "medium", "iconSize": "medium",
"badgeColor": "msx-yellow", "badgeColor": "msx-yellow",
"tagColor": "msx-yellow", "tagColor": "msx-yellow",
}, },
Items: list, Items: list,
} }
// If only one item start to play immediately but it not works // If only one item start to play immediately but it not works
// if (len(list) == 1) { // if (len(list) == 1) {
// res.Action = "execute:" + list[0].Action // res.Action = "execute:" + list[0].Action
// } // }
c.JSON(200, res) c.JSON(200, res)
} }
func isViewed(viewed []*sets.Viewed, id int) bool { func isViewed(viewed []*sets.Viewed, id int) bool {
for _, v := range viewed { for _, v := range viewed {
if (v.FileIndex == id) { if v.FileIndex == id {
return true return true
} }
} }
return false return false
} }