Add support for configs
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -21,4 +21,7 @@
|
|||||||
go.work
|
go.work
|
||||||
|
|
||||||
data/
|
data/
|
||||||
|
|
||||||
|
# Executables
|
||||||
rclone
|
rclone
|
||||||
|
main
|
||||||
|
|||||||
38
config.yml
Normal file
38
config.yml
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
zurg: v1
|
||||||
|
|
||||||
|
directories:
|
||||||
|
torrents:
|
||||||
|
group: asd
|
||||||
|
filters:
|
||||||
|
- not_contains: xxx
|
||||||
|
- not_contains_strict: trailer
|
||||||
|
|
||||||
|
shows:
|
||||||
|
group: sdf
|
||||||
|
filters:
|
||||||
|
- regex: /season/i
|
||||||
|
- regex: /Season/i
|
||||||
|
- regex: /Saison/i
|
||||||
|
|
||||||
|
remuxes:
|
||||||
|
group: gdfg
|
||||||
|
filters:
|
||||||
|
- contains: remux
|
||||||
|
|
||||||
|
"dolby vision":
|
||||||
|
group: zxc
|
||||||
|
filters:
|
||||||
|
- and:
|
||||||
|
- regex: /\bdovi\b/i
|
||||||
|
- contains: 4k
|
||||||
|
kids:
|
||||||
|
group: wqe
|
||||||
|
filters:
|
||||||
|
- id: XFPQ5UCMUVAEG
|
||||||
|
- id: VDRPYNRPQHEXC
|
||||||
|
- id: YELNX3XR5XJQM
|
||||||
|
|
||||||
|
default:
|
||||||
|
group: xcv
|
||||||
|
filters:
|
||||||
|
- regex: /.*/
|
||||||
35
config.yml.example
Normal file
35
config.yml.example
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
zurg: v1
|
||||||
|
|
||||||
|
directories:
|
||||||
|
torrents:
|
||||||
|
or:
|
||||||
|
- regex: /.*/ # you can specify multiple conditions and it works as an OR
|
||||||
|
- contains: season # use 'contains' for string, case insensitive
|
||||||
|
- contains_strict: season # case sensitive
|
||||||
|
- and:
|
||||||
|
- not_contains: xxx
|
||||||
|
- not_contains_strict: trailer
|
||||||
|
shows:
|
||||||
|
or:
|
||||||
|
- regex: /season/i # you can specify multiple regex conditions and it works as an OR
|
||||||
|
- regex: /Season/i
|
||||||
|
- regex: /Saison/i
|
||||||
|
"4k content":
|
||||||
|
regex: /\b4k\b/i
|
||||||
|
"dolby vision":
|
||||||
|
and:
|
||||||
|
regex: /\bdovi\b/i
|
||||||
|
contains: 4k
|
||||||
|
kids:
|
||||||
|
or:
|
||||||
|
- id: XFPQ5UCMUVAEG # you can specify the torrent ID as well
|
||||||
|
- id: VDRPYNRPQHEXC
|
||||||
|
- id: YELNX3XR5XJQM
|
||||||
|
default:
|
||||||
|
regex: /.*/ # if duplicates=false, best practice to add a catch-all bucket
|
||||||
|
|
||||||
|
duplicates: true
|
||||||
|
# if true, it means as long as a torrent satisfies the conditions set, then it will appear on that directory
|
||||||
|
# if false, it follows the order of directories and only appears once in one of them (or none so be careful)
|
||||||
|
# it processes each movie to see if it meets DIR1 conditions
|
||||||
|
# if not, it checks for DIR2 conditions, and so on...
|
||||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/debridmediamanager.com/zurg
|
module github.com/debridmediamanager.com/zurg
|
||||||
|
|
||||||
go 1.21.3
|
go 1.21.3
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -0,0 +1,3 @@
|
|||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
34
internal/config/load.go
Normal file
34
internal/config/load.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigInterface interface {
|
||||||
|
GetVersion() string
|
||||||
|
GetDirectories() []string
|
||||||
|
MeetsConditions(directory, fileID, fileName string) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadZurgConfig(filename string) (ConfigInterface, error) {
|
||||||
|
content, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var initialConfig ZurgConfig
|
||||||
|
if err := yaml.Unmarshal(content, &initialConfig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch initialConfig.Version {
|
||||||
|
case "v1":
|
||||||
|
return loadV1Config(content)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid config version: %s", initialConfig.Version)
|
||||||
|
}
|
||||||
|
}
|
||||||
5
internal/config/types.go
Normal file
5
internal/config/types.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type ZurgConfig struct {
|
||||||
|
Version string `yaml:"zurg"`
|
||||||
|
}
|
||||||
100
internal/config/v1.go
Normal file
100
internal/config/v1.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func loadV1Config(content []byte) (*ZurgConfigV1, error) {
|
||||||
|
var configV1 ZurgConfigV1
|
||||||
|
if err := yaml.Unmarshal(content, &configV1); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &configV1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) GetVersion() string {
|
||||||
|
return z.Version
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) GetDirectories() []string {
|
||||||
|
var rootDirectories []string
|
||||||
|
for directory := range z.Directories {
|
||||||
|
rootDirectories = append(rootDirectories, directory)
|
||||||
|
}
|
||||||
|
return rootDirectories
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
|
||||||
|
var groupMap = make(map[string][]string)
|
||||||
|
for directory, val := range z.Directories {
|
||||||
|
groupMap[val.Group] = append(groupMap[val.Group], directory)
|
||||||
|
}
|
||||||
|
return groupMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) GetDirectoriesByGroup(group string) []string {
|
||||||
|
var groupDirs []string
|
||||||
|
for directory := range z.Directories {
|
||||||
|
if z.Directories[directory].Group == group {
|
||||||
|
groupDirs = append(groupDirs, directory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return groupDirs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) MeetsConditions(directory, fileID, torrentName string) bool {
|
||||||
|
if _, ok := z.Directories[directory]; !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, filter := range z.Directories[directory].Filters {
|
||||||
|
if z.matchFilter(fileID, torrentName, filter) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterConditionsV1) bool {
|
||||||
|
if filter.ID != "" && fileID == filter.ID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if filter.RegexStr != "" {
|
||||||
|
regex := regexp.MustCompile(filter.RegexStr)
|
||||||
|
if regex.MatchString(torrentName) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filter.ContainsStrict != "" && strings.Contains(torrentName, filter.ContainsStrict) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if filter.Contains != "" && strings.Contains(strings.ToLower(torrentName), strings.ToLower(filter.Contains)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if filter.NotContainsStrict != "" && !strings.Contains(torrentName, filter.NotContainsStrict) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if filter.NotContains != "" && !strings.Contains(strings.ToLower(torrentName), strings.ToLower(filter.NotContains)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if len(filter.And) > 0 {
|
||||||
|
andResult := true
|
||||||
|
for _, andFilter := range filter.And {
|
||||||
|
andResult = andResult && z.matchFilter(fileID, torrentName, andFilter)
|
||||||
|
if !andResult {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if len(filter.Or) > 0 {
|
||||||
|
for _, orFilter := range filter.Or {
|
||||||
|
if z.matchFilter(fileID, torrentName, orFilter) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
22
internal/config/v1types.go
Normal file
22
internal/config/v1types.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type ZurgConfigV1 struct {
|
||||||
|
ZurgConfig
|
||||||
|
Directories map[string]*DirectoryFilterConditionsV1 `yaml:"directories"`
|
||||||
|
Duplicates bool `yaml:"duplicates"`
|
||||||
|
}
|
||||||
|
type DirectoryFilterConditionsV1 struct {
|
||||||
|
Group string `yaml:"group"`
|
||||||
|
Filters []*FilterConditionsV1 `yaml:"filters"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type FilterConditionsV1 struct {
|
||||||
|
RegexStr string `yaml:"regex"`
|
||||||
|
Contains string `yaml:"contains"`
|
||||||
|
ContainsStrict string `yaml:"contains_strict"`
|
||||||
|
NotContains string `yaml:"not_contains"`
|
||||||
|
NotContainsStrict string `yaml:"not_contains_strict"`
|
||||||
|
ID string `yaml:"id"`
|
||||||
|
And []*FilterConditionsV1 `yaml:"and"`
|
||||||
|
Or []*FilterConditionsV1 `yaml:"or"`
|
||||||
|
}
|
||||||
@@ -27,10 +27,11 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the last two segments
|
// Get the last two segments
|
||||||
|
baseDirectory := segments[len(segments)-3]
|
||||||
torrentName := segments[len(segments)-2]
|
torrentName := segments[len(segments)-2]
|
||||||
filename := segments[len(segments)-1]
|
filename := segments[len(segments)-1]
|
||||||
|
|
||||||
torrents := findAllTorrentsWithName(t, torrentName)
|
torrents := findAllTorrentsWithName(t, baseDirectory, torrentName)
|
||||||
if torrents == nil {
|
if torrents == nil {
|
||||||
log.Println("Cannot find torrent", torrentName)
|
log.Println("Cannot find torrent", torrentName)
|
||||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||||
|
|||||||
@@ -6,23 +6,40 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HandlePropfindRequest handles a PROPFIND request
|
// HandlePropfindRequest handles a PROPFIND request
|
||||||
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
|
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
|
||||||
var output []byte
|
var output []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
requestPath := path.Clean(r.URL.Path)
|
requestPath := path.Clean(r.URL.Path)
|
||||||
if requestPath == "/" {
|
pathSegments := strings.Split(requestPath, "/")
|
||||||
output, err = handleRoot(w, r)
|
|
||||||
} else if requestPath == "/torrents" {
|
// Remove empty segments caused by leading or trailing slashes
|
||||||
output, err = handleListOfTorrents(w, r, t)
|
filteredSegments := make([]string, 0, len(pathSegments))
|
||||||
} else {
|
for _, segment := range pathSegments {
|
||||||
output, err = handleSingleTorrent(w, r, t)
|
if segment != "" {
|
||||||
|
filteredSegments = append(filteredSegments, segment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch len(filteredSegments) {
|
||||||
|
case 0: // Just the root "/"
|
||||||
|
output, err = handleRoot(w, r, c)
|
||||||
|
case 1: // It's just the basedir e.g. "/basedir"
|
||||||
|
output, err = handleListOfTorrents(requestPath, w, r, t, c)
|
||||||
|
case 2: // It's a specific torrent under a basedir e.g. "/basedir/torrentname/"
|
||||||
|
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||||
|
default:
|
||||||
|
// Handle any other paths, e.g., send a 404 Not Found response
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot marshal xml: %v\n", err.Error())
|
log.Printf("Cannot marshal xml: %v\n", err.Error())
|
||||||
@@ -38,23 +55,39 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleRoot handles a PROPFIND request to the root directory
|
// handleRoot handles a PROPFIND request to the root directory
|
||||||
func handleRoot(w http.ResponseWriter, r *http.Request) ([]byte, error) {
|
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) ([]byte, error) {
|
||||||
|
var responses []dav.Response
|
||||||
|
responses = append(responses, dav.Directory("/"))
|
||||||
|
for _, directory := range c.GetDirectories() {
|
||||||
|
responses = append(responses, dav.Directory(fmt.Sprintf("/%s", directory)))
|
||||||
|
}
|
||||||
rootResponse := dav.MultiStatus{
|
rootResponse := dav.MultiStatus{
|
||||||
XMLNS: "DAV:",
|
XMLNS: "DAV:",
|
||||||
Response: []dav.Response{
|
Response: responses,
|
||||||
dav.Directory("/"),
|
|
||||||
dav.Directory("/torrents"),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return xml.MarshalIndent(rootResponse, "", " ")
|
return xml.MarshalIndent(rootResponse, "", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleListOfTorrents handles a PROPFIND request to the /torrents directory
|
// handleListOfTorrents handles a PROPFIND request to the /torrents directory
|
||||||
func handleListOfTorrents(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) ([]byte, error) {
|
||||||
torrents := t.GetAll()
|
basePath := path.Base(requestPath)
|
||||||
resp, err := createMultiTorrentResponse(torrents)
|
|
||||||
|
found := false
|
||||||
|
for _, directory := range c.GetDirectories() {
|
||||||
|
if basePath == directory {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
log.Println("Cannot find directory", requestPath)
|
||||||
|
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
torrents := t.GetByDirectory(basePath)
|
||||||
|
resp, err := createMultiTorrentResponse(fmt.Sprintf("/%s", basePath), torrents)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot read directory (/torrents): %v\n", err.Error())
|
log.Printf("Cannot read directory (%s): %v\n", basePath, err.Error())
|
||||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -62,17 +95,18 @@ func handleListOfTorrents(w http.ResponseWriter, r *http.Request, t *torrent.Tor
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleSingleTorrent handles a PROPFIND request to a single torrent directory
|
// handleSingleTorrent handles a PROPFIND request to a single torrent directory
|
||||||
func handleSingleTorrent(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
||||||
requestPath := path.Clean(r.URL.Path)
|
basePath := path.Dir(requestPath)
|
||||||
|
|
||||||
torrentName := path.Base(requestPath)
|
torrentName := path.Base(requestPath)
|
||||||
torrents := findAllTorrentsWithName(t, torrentName)
|
sameNameTorrents := findAllTorrentsWithName(t, basePath, torrentName)
|
||||||
if len(torrents) == 0 {
|
if len(sameNameTorrents) == 0 {
|
||||||
log.Println("Cannot find directory", requestPath)
|
log.Println("Cannot find directory", requestPath)
|
||||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
var resp *dav.MultiStatus
|
var resp *dav.MultiStatus
|
||||||
resp, err := createCombinedTorrentResponse(torrents, t)
|
resp, err := createSingleTorrentResponse(fmt.Sprintf("/%s", basePath), sameNameTorrents, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot read directory (%s): %v\n", requestPath, err.Error())
|
log.Printf("Cannot read directory (%s): %v\n", requestPath, err.Error())
|
||||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
|
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
|
||||||
func createMultiTorrentResponse(torrents []torrent.Torrent) (*dav.MultiStatus, error) {
|
func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (*dav.MultiStatus, error) {
|
||||||
var responses []dav.Response
|
var responses []dav.Response
|
||||||
responses = append(responses, dav.Directory("/torrents"))
|
responses = append(responses, dav.Directory(basePath))
|
||||||
|
|
||||||
seen := make(map[string]bool)
|
seen := make(map[string]bool)
|
||||||
|
|
||||||
@@ -19,12 +19,12 @@ func createMultiTorrentResponse(torrents []torrent.Torrent) (*dav.MultiStatus, e
|
|||||||
if item.Progress != 100 {
|
if item.Progress != 100 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, exists := seen[item.Filename]; exists {
|
if _, exists := seen[item.Name]; exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seen[item.Filename] = true
|
seen[item.Name] = true
|
||||||
|
|
||||||
path := filepath.Join("/torrents", item.Filename)
|
path := filepath.Join(basePath, item.Name)
|
||||||
responses = append(responses, dav.Directory(path))
|
responses = append(responses, dav.Directory(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,10 +36,10 @@ func createMultiTorrentResponse(torrents []torrent.Torrent) (*dav.MultiStatus, e
|
|||||||
|
|
||||||
// createTorrentResponse creates a WebDAV response for a single torrent
|
// createTorrentResponse creates a WebDAV response for a single torrent
|
||||||
// but it also handles the case where there are many torrents with the same name
|
// but it also handles the case where there are many torrents with the same name
|
||||||
func createCombinedTorrentResponse(torrents []torrent.Torrent, t *torrent.TorrentManager) (*dav.MultiStatus, error) {
|
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent, t *torrent.TorrentManager) (*dav.MultiStatus, error) {
|
||||||
var responses []dav.Response
|
var responses []dav.Response
|
||||||
// initial response is the directory itself
|
// initial response is the directory itself
|
||||||
currentPath := filepath.Join("/torrents", torrents[0].Filename)
|
currentPath := filepath.Join(basePath, torrents[0].Name)
|
||||||
responses = append(responses, dav.Directory(currentPath))
|
responses = append(responses, dav.Directory(currentPath))
|
||||||
|
|
||||||
seen := make(map[string]bool)
|
seen := make(map[string]bool)
|
||||||
|
|||||||
@@ -5,20 +5,23 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Router creates a WebDAV router
|
// Router creates a WebDAV router
|
||||||
func Router(mux *http.ServeMux) {
|
func Router(mux *http.ServeMux) {
|
||||||
t := torrent.NewTorrentManager(os.Getenv("RD_TOKEN"))
|
c, err := config.LoadZurgConfig("./config.yml")
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Config failed to load: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t := torrent.NewTorrentManager(os.Getenv("RD_TOKEN"), c)
|
||||||
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
// requestPath := path.Clean(r.URL.Path)
|
|
||||||
// log.Println(r.Method, requestPath)
|
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "PROPFIND":
|
case "PROPFIND":
|
||||||
HandlePropfindRequest(w, r, t)
|
HandlePropfindRequest(w, r, t, c)
|
||||||
|
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
HandleGetRequest(w, r, t)
|
HandleGetRequest(w, r, t)
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ func convertDate(input string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// findAllTorrentsWithName finds all torrents with a given name
|
// findAllTorrentsWithName finds all torrents with a given name
|
||||||
func findAllTorrentsWithName(t *torrent.TorrentManager, filename string) []torrent.Torrent {
|
func findAllTorrentsWithName(t *torrent.TorrentManager, directory, torrentName string) []torrent.Torrent {
|
||||||
var matchingTorrents []torrent.Torrent
|
var matchingTorrents []torrent.Torrent
|
||||||
|
|
||||||
torrents := t.GetAll()
|
torrents := t.GetByDirectory(directory)
|
||||||
for _, torrent := range torrents {
|
for _, torrent := range torrents {
|
||||||
if torrent.Filename == filename || strings.HasPrefix(torrent.Filename, filename) {
|
if torrent.Name == torrentName || strings.HasPrefix(torrent.Name, torrentName) {
|
||||||
matchingTorrents = append(matchingTorrents, torrent)
|
matchingTorrents = append(matchingTorrents, torrent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,15 +15,17 @@ type TorrentManager struct {
|
|||||||
token string
|
token string
|
||||||
torrents []Torrent
|
torrents []Torrent
|
||||||
workerPool chan bool
|
workerPool chan bool
|
||||||
|
config config.ConfigInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTorrentManager creates a new torrent manager
|
// NewTorrentManager creates a new torrent manager
|
||||||
// it will fetch all torrents and their info in the background
|
// it will fetch all torrents and their info in the background
|
||||||
// and store them in-memory
|
// and store them in-memory
|
||||||
func NewTorrentManager(token string) *TorrentManager {
|
func NewTorrentManager(token string, config config.ConfigInterface) *TorrentManager {
|
||||||
handler := &TorrentManager{
|
handler := &TorrentManager{
|
||||||
token: token,
|
token: token,
|
||||||
workerPool: make(chan bool, 10),
|
workerPool: make(chan bool, 10),
|
||||||
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.torrents = handler.getAll()
|
handler.torrents = handler.getAll()
|
||||||
@@ -40,18 +43,36 @@ func NewTorrentManager(token string) *TorrentManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) getAll() []Torrent {
|
func (t *TorrentManager) getAll() []Torrent {
|
||||||
|
log.Println("Getting all torrents")
|
||||||
torrents, err := realdebrid.GetTorrents(t.token)
|
torrents, err := realdebrid.GetTorrents(t.token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot get torrents: %v\n", err.Error())
|
log.Printf("Cannot get torrents: %v\n", err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var torrentsV2 []Torrent
|
var torrentsV2 []Torrent
|
||||||
|
|
||||||
|
version := t.config.GetVersion()
|
||||||
|
if version == "v1" {
|
||||||
|
configV1 := t.config.(*config.ZurgConfigV1)
|
||||||
|
groupMap := configV1.GetGroupMap()
|
||||||
|
for _, directories := range groupMap {
|
||||||
for _, torrent := range torrents {
|
for _, torrent := range torrents {
|
||||||
torrentsV2 = append(torrentsV2, Torrent{
|
// process grouping
|
||||||
|
torrentV2 := Torrent{
|
||||||
Torrent: torrent,
|
Torrent: torrent,
|
||||||
SelectedFiles: nil,
|
SelectedFiles: nil,
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
for _, directory := range directories {
|
||||||
|
if configV1.MeetsConditions(directory, torrent.ID, torrent.Name) {
|
||||||
|
torrentV2.Directories = append(torrentV2.Directories, directory)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
torrentsV2 = append(torrentsV2, torrentV2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("Fetched %d torrents", len(torrentsV2))
|
||||||
return torrentsV2
|
return torrentsV2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,6 +80,18 @@ func (t *TorrentManager) GetAll() []Torrent {
|
|||||||
return t.torrents
|
return t.torrents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
|
||||||
|
var torrents []Torrent
|
||||||
|
for _, torrent := range t.torrents {
|
||||||
|
for _, dir := range torrent.Directories {
|
||||||
|
if dir == directory {
|
||||||
|
torrents = append(torrents, torrent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return torrents
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||||
torrentFromFile := t.readFromFile(torrentID)
|
torrentFromFile := t.readFromFile(torrentID)
|
||||||
if torrentFromFile != nil {
|
if torrentFromFile != nil {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import "github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
|||||||
|
|
||||||
type Torrent struct {
|
type Torrent struct {
|
||||||
realdebrid.Torrent
|
realdebrid.Torrent
|
||||||
|
Directories []string
|
||||||
SelectedFiles []File
|
SelectedFiles []File
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ func File(path string, fileSize int64, added string, link string) Response {
|
|||||||
IsHidden: 0,
|
IsHidden: 0,
|
||||||
CreationDate: added,
|
CreationDate: added,
|
||||||
LastModified: added,
|
LastModified: added,
|
||||||
Link: link,
|
|
||||||
},
|
},
|
||||||
Status: "HTTP/1.1 200 OK",
|
Status: "HTTP/1.1 200 OK",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ type Prop struct {
|
|||||||
CreationDate string `xml:"d:creationdate"`
|
CreationDate string `xml:"d:creationdate"`
|
||||||
LastModified string `xml:"d:getlastmodified"`
|
LastModified string `xml:"d:getlastmodified"`
|
||||||
IsHidden int `xml:"d:ishidden"`
|
IsHidden int `xml:"d:ishidden"`
|
||||||
Filename string `xml:"-"`
|
|
||||||
Link string `xml:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceType struct {
|
type ResourceType struct {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type UnrestrictResponse struct {
|
|||||||
|
|
||||||
type Torrent struct {
|
type Torrent struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Filename string `json:"filename"`
|
Name string `json:"filename"`
|
||||||
Hash string `json:"hash"`
|
Hash string `json:"hash"`
|
||||||
Progress int `json:"progress"`
|
Progress int `json:"progress"`
|
||||||
Added string `json:"added"`
|
Added string `json:"added"`
|
||||||
|
|||||||
Reference in New Issue
Block a user