Try reading rar contents
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/internal/handlers"
|
||||
"github.com/debridmediamanager/zurg/internal/rar"
|
||||
"github.com/debridmediamanager/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager/zurg/internal/universal"
|
||||
"github.com/debridmediamanager/zurg/internal/version"
|
||||
@@ -95,9 +96,12 @@ func MainApp(configPath string) {
|
||||
}
|
||||
defer workerPool.Release()
|
||||
|
||||
rarReader := rar.NewRarReader(downloadClient, log.Named("rar_reader"))
|
||||
|
||||
torrentMgr := torrent.NewTorrentManager(
|
||||
config,
|
||||
api,
|
||||
rarReader,
|
||||
workerPool,
|
||||
log.Named("manager"),
|
||||
log.Named("repair"),
|
||||
|
||||
69
internal/rar/contents.go
Normal file
69
internal/rar/contents.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package rar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
|
||||
"github.com/debridmediamanager/zurg/pkg/logutil"
|
||||
"github.com/nwaples/rardecode"
|
||||
)
|
||||
|
||||
type RarReader struct {
|
||||
client *zurghttp.HTTPClient
|
||||
log *logutil.Logger
|
||||
}
|
||||
|
||||
func NewRarReader(client *zurghttp.HTTPClient, log *logutil.Logger) *RarReader {
|
||||
return &RarReader{
|
||||
client: client,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
// GetRARContents extracts the file names from a RAR archive.
|
||||
func (r *RarReader) GetRARContents(downloadLink string) ([]string, error) {
|
||||
dlReq, err := http.NewRequest(http.MethodGet, downloadLink, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
downloadResp, err := r.client.Do(dlReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer downloadResp.Body.Close()
|
||||
|
||||
if downloadResp.StatusCode/100 != 2 {
|
||||
return nil, fmt.Errorf("received a %s status code", downloadResp.Status)
|
||||
}
|
||||
|
||||
var fileList []string
|
||||
|
||||
rarReader, err := rardecode.NewReader(downloadResp.Body, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for {
|
||||
r.log.Debugf("Reading next file from RAR archive")
|
||||
header, err := rarReader.Next()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "unexpected EOF") {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if header == nil {
|
||||
break
|
||||
}
|
||||
|
||||
fileList = append(fileList, header.Name)
|
||||
r.log.Debugf("Found file: %s", header.Name)
|
||||
r.log.Debugf("File size: %d MB", header.PackedSize/1024/1024)
|
||||
}
|
||||
|
||||
return fileList, nil
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/internal/fs"
|
||||
"github.com/debridmediamanager/zurg/internal/rar"
|
||||
"github.com/debridmediamanager/zurg/pkg/logutil"
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
@@ -28,6 +29,7 @@ type TorrentManager struct {
|
||||
|
||||
Config config.ConfigInterface
|
||||
api *realdebrid.RealDebrid
|
||||
rarReader *rar.RarReader
|
||||
workerPool *ants.Pool
|
||||
log *logutil.Logger
|
||||
repairLog *logutil.Logger
|
||||
@@ -58,12 +60,13 @@ type TorrentManager struct {
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
// it will fetch all torrents and their info in the background
|
||||
// and store them in-memory and cached in files
|
||||
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, workerPool *ants.Pool, log, repairLog *logutil.Logger) *TorrentManager {
|
||||
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, rarReader *rar.RarReader, workerPool *ants.Pool, log, repairLog *logutil.Logger) *TorrentManager {
|
||||
t := &TorrentManager{
|
||||
requiredVersion: "0.10.0",
|
||||
|
||||
Config: cfg,
|
||||
api: api,
|
||||
rarReader: rarReader,
|
||||
workerPool: workerPool,
|
||||
log: log,
|
||||
repairLog: repairLog,
|
||||
|
||||
@@ -316,6 +316,12 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
|
||||
if !assigned {
|
||||
// if not assigned and is a rar, likely it was rar'ed by RD
|
||||
if strings.HasSuffix(strings.ToLower(unrestrict.Filename), ".rar") {
|
||||
// t.log.Debugf("Trying to get contents of rar file %s", unrestrict.Filename)
|
||||
// contents, err := t.rarReader.GetRARContents(unrestrict.Download)
|
||||
// if err != nil {
|
||||
// t.repairLog.Warnf("Cannot get contents of rar file %s: %v", unrestrict.Filename, err)
|
||||
// }
|
||||
// t.log.Debugf("contents: %v", contents)
|
||||
rarCount++
|
||||
} else {
|
||||
t.repairLog.Warnf("Cannot assign %s to any file in torrent %s", unrestrict.Filename, t.GetKey(torrent))
|
||||
|
||||
Reference in New Issue
Block a user