Add repair worker
This commit is contained in:
@@ -4,9 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"goBlack/common"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@@ -23,7 +21,6 @@ const (
|
||||
|
||||
var (
|
||||
client *common.RLHTTPClient = common.NewRLHTTPClient(nil, nil)
|
||||
logger *log.Logger = common.NewLogger("QBit", os.Stdout)
|
||||
)
|
||||
|
||||
type Arr struct {
|
||||
@@ -87,11 +84,12 @@ func inferType(host, name string) Type {
|
||||
}
|
||||
}
|
||||
|
||||
func NewStorage() *Storage {
|
||||
func NewStorage(cfg []common.ArrConfig) *Storage {
|
||||
arrs := make(map[string]*Arr)
|
||||
//for name, arrCfg := range cfg {
|
||||
// arrs[name] = NewArr(name, arrCfg.Host, arrCfg.Token, inferType(arrCfg.Host, name))
|
||||
//}
|
||||
for _, a := range cfg {
|
||||
name := a.Name
|
||||
arrs[name] = NewArr(name, a.Host, a.Token, inferType(a.Host, name))
|
||||
}
|
||||
return &Storage{
|
||||
Arrs: arrs,
|
||||
}
|
||||
|
||||
@@ -3,27 +3,102 @@ package arr
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ContentRequest struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"name"`
|
||||
Arr string `json:"arr"`
|
||||
func (a *Arr) GetMedia(tvId string) ([]Content, error) {
|
||||
// Get series
|
||||
resp, err := a.Request(http.MethodGet, fmt.Sprintf("api/v3/series?tvdbId=%s", tvId), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
// This is Radarr
|
||||
log.Printf("Radarr detected\n")
|
||||
a.Type = Radarr
|
||||
return GetMovies(a, tvId)
|
||||
}
|
||||
a.Type = Sonarr
|
||||
defer resp.Body.Close()
|
||||
type series struct {
|
||||
Title string `json:"title"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
var data []series
|
||||
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Get series files
|
||||
contents := make([]Content, 0)
|
||||
for _, d := range data {
|
||||
resp, err = a.Request(http.MethodGet, fmt.Sprintf("api/v3/episodefile?seriesId=%d", d.Id), nil)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var seriesFiles []seriesFile
|
||||
if err = json.NewDecoder(resp.Body).Decode(&seriesFiles); err != nil {
|
||||
continue
|
||||
}
|
||||
ct := Content{
|
||||
Title: d.Title,
|
||||
Id: d.Id,
|
||||
}
|
||||
files := make([]contentFile, 0)
|
||||
for _, file := range seriesFiles {
|
||||
files = append(files, contentFile{
|
||||
Id: file.Id,
|
||||
Path: file.Path,
|
||||
})
|
||||
}
|
||||
ct.Files = files
|
||||
contents = append(contents, ct)
|
||||
}
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
func (a *Arr) GetContents() *ContentRequest {
|
||||
resp, err := a.Request(http.MethodGet, "api/v3/series", nil)
|
||||
func GetMovies(a *Arr, tvId string) ([]Content, error) {
|
||||
resp, err := a.Request(http.MethodGet, fmt.Sprintf("api/v3/movie?tmdbId=%s", tvId), nil)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var data *ContentRequest
|
||||
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return nil
|
||||
var movies []Movie
|
||||
if err = json.NewDecoder(resp.Body).Decode(&movies); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("Data: %v\n", data)
|
||||
//data.Arr = a.Name
|
||||
return data
|
||||
contents := make([]Content, 0)
|
||||
for _, movie := range movies {
|
||||
ct := Content{
|
||||
Title: movie.Title,
|
||||
Id: movie.Id,
|
||||
}
|
||||
files := make([]contentFile, 0)
|
||||
files = append(files, contentFile{
|
||||
Id: movie.MovieFile.Id,
|
||||
Path: movie.MovieFile.Path,
|
||||
})
|
||||
ct.Files = files
|
||||
contents = append(contents, ct)
|
||||
}
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
func (a *Arr) DeleteFile(id int) error {
|
||||
switch a.Type {
|
||||
case Sonarr:
|
||||
_, err := a.Request(http.MethodDelete, fmt.Sprintf("api/v3/episodefile/%d", id), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case Radarr:
|
||||
_, err := a.Request(http.MethodDelete, fmt.Sprintf("api/v3/moviefile/%d", id), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknown arr type: %s", a.Type)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
270
pkg/arr/repair.go
Normal file
270
pkg/arr/repair.go
Normal file
@@ -0,0 +1,270 @@
|
||||
package arr
|
||||
|
||||
import (
|
||||
"goBlack/common"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
repairLogger *log.Logger = common.NewLogger("Repair", os.Stdout)
|
||||
)
|
||||
|
||||
func (a *Arr) SearchMissing(id int) {
|
||||
var payload interface{}
|
||||
|
||||
switch a.Type {
|
||||
case Sonarr:
|
||||
payload = struct {
|
||||
Name string `json:"name"`
|
||||
SeriesId int `json:"seriesId"`
|
||||
}{
|
||||
Name: "SeriesSearch",
|
||||
SeriesId: id,
|
||||
}
|
||||
case Radarr:
|
||||
payload = struct {
|
||||
Name string `json:"name"`
|
||||
MovieId int `json:"movieId"`
|
||||
}{
|
||||
Name: "MoviesSearch",
|
||||
MovieId: id,
|
||||
}
|
||||
default:
|
||||
repairLogger.Printf("Unknown arr type: %s\n", a.Type)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := a.Request(http.MethodPost, "api/v3/command", payload)
|
||||
if err != nil {
|
||||
repairLogger.Printf("Failed to search missing: %v\n", err)
|
||||
return
|
||||
}
|
||||
if statusOk := strconv.Itoa(resp.StatusCode)[0] == '2'; !statusOk {
|
||||
repairLogger.Printf("Failed to search missing: %s\n", resp.Status)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Arr) Repair(tmdbId string) error {
|
||||
|
||||
repairLogger.Printf("Starting repair for %s\n", a.Name)
|
||||
media, err := a.GetMedia(tmdbId)
|
||||
if err != nil {
|
||||
repairLogger.Printf("Failed to get %s media: %v\n", a.Type, err)
|
||||
return err
|
||||
}
|
||||
repairLogger.Printf("Found %d %s media\n", len(media), a.Type)
|
||||
|
||||
brokenMedia := a.processMedia(media)
|
||||
repairLogger.Printf("Found %d %s broken media files\n", len(brokenMedia), a.Type)
|
||||
|
||||
// Automatic search for missing files
|
||||
for _, m := range brokenMedia {
|
||||
a.SearchMissing(m.Id)
|
||||
}
|
||||
repairLogger.Printf("Search missing completed for %s\n", a.Name)
|
||||
repairLogger.Printf("Repair completed for %s\n", a.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Arr) processMedia(media []Content) []Content {
|
||||
if len(media) <= 1 {
|
||||
var brokenMedia []Content
|
||||
for _, m := range media {
|
||||
if a.checkMediaFiles(m) {
|
||||
brokenMedia = append(brokenMedia, m)
|
||||
}
|
||||
}
|
||||
return brokenMedia
|
||||
}
|
||||
|
||||
workerCount := runtime.NumCPU() * 4
|
||||
if len(media) < workerCount {
|
||||
workerCount = len(media)
|
||||
}
|
||||
|
||||
jobs := make(chan Content)
|
||||
results := make(chan Content)
|
||||
var brokenMedia []Content
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < workerCount; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for m := range jobs {
|
||||
if a.checkMediaFilesParallel(m) {
|
||||
results <- m
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
go func() {
|
||||
for _, m := range media {
|
||||
jobs <- m
|
||||
}
|
||||
close(jobs)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(results)
|
||||
}()
|
||||
|
||||
for m := range results {
|
||||
brokenMedia = append(brokenMedia, m)
|
||||
}
|
||||
|
||||
return brokenMedia
|
||||
}
|
||||
|
||||
func (a *Arr) checkMediaFilesParallel(m Content) bool {
|
||||
if len(m.Files) <= 1 {
|
||||
return a.checkMediaFiles(m)
|
||||
}
|
||||
|
||||
fileWorkers := runtime.NumCPU() * 2
|
||||
if len(m.Files) < fileWorkers {
|
||||
fileWorkers = len(m.Files)
|
||||
}
|
||||
|
||||
fileJobs := make(chan contentFile)
|
||||
brokenFiles := make(chan bool, len(m.Files))
|
||||
|
||||
var fileWg sync.WaitGroup
|
||||
for i := 0; i < fileWorkers; i++ {
|
||||
fileWg.Add(1)
|
||||
go func() {
|
||||
defer fileWg.Done()
|
||||
for f := range fileJobs {
|
||||
isBroken := false
|
||||
if fileIsSymlinked(f.Path) {
|
||||
if !fileIsCorrectSymlink(f.Path) {
|
||||
isBroken = true
|
||||
if err := a.DeleteFile(f.Id); err != nil {
|
||||
repairLogger.Printf("Failed to delete file: %s %d: %v\n", f.Path, f.Id, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !fileIsReadable(f.Path) {
|
||||
isBroken = true
|
||||
if err := a.DeleteFile(f.Id); err != nil {
|
||||
repairLogger.Printf("Failed to delete file: %s %d: %v\n", f.Path, f.Id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
brokenFiles <- isBroken
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
go func() {
|
||||
for _, f := range m.Files {
|
||||
fileJobs <- f
|
||||
}
|
||||
close(fileJobs)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
fileWg.Wait()
|
||||
close(brokenFiles)
|
||||
}()
|
||||
|
||||
isBroken := false
|
||||
for broken := range brokenFiles {
|
||||
if broken {
|
||||
isBroken = true
|
||||
}
|
||||
}
|
||||
|
||||
return isBroken
|
||||
}
|
||||
|
||||
func (a *Arr) checkMediaFiles(m Content) bool {
|
||||
isBroken := false
|
||||
for _, f := range m.Files {
|
||||
if fileIsSymlinked(f.Path) {
|
||||
if !fileIsCorrectSymlink(f.Path) {
|
||||
isBroken = true
|
||||
if err := a.DeleteFile(f.Id); err != nil {
|
||||
repairLogger.Printf("Failed to delete file: %s %d: %v\n", f.Path, f.Id, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !fileIsReadable(f.Path) {
|
||||
isBroken = true
|
||||
if err := a.DeleteFile(f.Id); err != nil {
|
||||
repairLogger.Printf("Failed to delete file: %s %d: %v\n", f.Path, f.Id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isBroken
|
||||
}
|
||||
|
||||
func fileIsSymlinked(file string) bool {
|
||||
info, err := os.Lstat(file)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return info.Mode()&os.ModeSymlink != 0
|
||||
}
|
||||
|
||||
func fileIsCorrectSymlink(file string) bool {
|
||||
target, err := os.Readlink(file)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if !filepath.IsAbs(target) {
|
||||
dir := filepath.Dir(file)
|
||||
target = filepath.Join(dir, target)
|
||||
}
|
||||
|
||||
return fileIsReadable(target)
|
||||
}
|
||||
|
||||
func fileIsReadable(filePath string) bool {
|
||||
// First check if file exists and is accessible
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if it's a regular file
|
||||
if !info.Mode().IsRegular() {
|
||||
return false
|
||||
}
|
||||
|
||||
// Try to read the first 1024 bytes
|
||||
err = checkFileStart(filePath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func checkFileStart(filePath string) error {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
_, err = io.ReadAtLeast(f, buffer, 1024)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
34
pkg/arr/structs.go
Normal file
34
pkg/arr/structs.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package arr
|
||||
|
||||
type Movie struct {
|
||||
Title string `json:"title"`
|
||||
OriginalTitle string `json:"originalTitle"`
|
||||
Path string `json:"path"`
|
||||
MovieFile struct {
|
||||
MovieId int `json:"movieId"`
|
||||
RelativePath string `json:"relativePath"`
|
||||
Path string `json:"path"`
|
||||
Size int `json:"size"`
|
||||
Id int `json:"id"`
|
||||
} `json:"movieFile"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
type contentFile struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
type Content struct {
|
||||
Title string `json:"title"`
|
||||
Id int `json:"id"`
|
||||
Files []contentFile `json:"files"`
|
||||
}
|
||||
|
||||
type seriesFile struct {
|
||||
SeriesId int `json:"seriesId"`
|
||||
SeasonNumber int `json:"seasonNumber"`
|
||||
Path string `json:"path"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
5
pkg/arr/utils.go
Normal file
5
pkg/arr/utils.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package arr
|
||||
|
||||
func Readfile(path string) error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user