Skip to content

Commit

Permalink
Changelog 0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
sirrobot01 committed Sep 16, 2024
1 parent 2ec0354 commit 0198111
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@
#### 0.2.2
- Fix name mismatch in the cache
- Fix directory mapping with mounts
- Add Support for refreshing the *arrs
- Add Support for refreshing the *arrs

#### 0.2.3

- Delete uncached items from RD
- Fail if the torrent is not cached(optional)
- Fix cache not being updated
3 changes: 2 additions & 1 deletion pkg/debrid/debrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Service interface {
SubmitMagnet(torrent *Torrent) (*Torrent, error)
CheckStatus(torrent *Torrent) (*Torrent, error)
DownloadLink(torrent *Torrent) error
DeleteTorrent(torrent *Torrent)
IsAvailable(infohashes []string) map[string]bool
GetMountPath() string
GetDownloadUncached() bool
Expand Down Expand Up @@ -134,7 +135,7 @@ func ProcessQBitTorrent(d Service, magnet *common.Magnet, arr *Arr) (*Torrent, e
if !exists || !hash {
return debridTorrent, fmt.Errorf("torrent: %s is not cached", debridTorrent.Name)
} else {
logger.Printf("Torrent: %s is cached", debridTorrent.Name)
logger.Printf("Torrent: %s is cached(or downloading)", debridTorrent.Name)
}
}

Expand Down
15 changes: 14 additions & 1 deletion pkg/debrid/realdebrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,28 @@ func (r *RealDebrid) CheckStatus(torrent *Torrent) (*Torrent, error) {
break
} else if status == "downloading" {
if !r.DownloadUncached {
// @TODO: Delete the torrent if it's not cached
go r.DeleteTorrent(torrent)
return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name)
}
// Break out of the loop if the torrent is downloading.
// This is necessary to prevent infinite loop since we moved to sync downloading and async processing
break
}

}
return torrent, nil
}

func (r *RealDebrid) DeleteTorrent(torrent *Torrent) {
url := fmt.Sprintf("%s/torrents/delete/%s", r.Host, torrent.Id)
_, err := r.client.MakeRequest(http.MethodDelete, url, nil)
if err == nil {
r.logger.Printf("Torrent: %s deleted\n", torrent.Name)
} else {
r.logger.Printf("Error deleting torrent: %s", err)
}
}

func (r *RealDebrid) DownloadLink(torrent *Torrent) error {
return nil
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/qbit/handlers_torrent.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package qbit

import (
"goBlack/common"
"io"
"net/http"
"path/filepath"
"strings"
Expand Down Expand Up @@ -47,29 +45,21 @@ func (q *QBit) handleTorrentsAdd(w http.ResponseWriter, r *http.Request) {
}

for _, url := range urlList {
magnet, err := common.GetMagnetFromUrl(url)
if err != nil {
q.logger.Printf("Error parsing magnet link: %v\n", err)
if err := q.AddMagnet(ctx, url, category); err != nil {
q.logger.Printf("Error adding magnet: %v\n", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
go q.Process(ctx, magnet, category)

}

if contentType == "multipart/form-data" {
files := r.MultipartForm.File["torrents"]
for _, fileHeader := range files {
file, _ := fileHeader.Open()
defer file.Close()
var reader io.Reader = file
magnet, err := common.GetMagnetFromFile(reader, fileHeader.Filename)
if err != nil {
if err := q.AddTorrent(ctx, fileHeader, category); err != nil {
q.logger.Printf("Error adding torrent: %v\n", err)
http.Error(w, err.Error(), http.StatusBadRequest)
q.logger.Printf("Error reading file: %s", fileHeader.Filename)
return
}
go q.Process(ctx, magnet, category)
}
}

Expand Down
52 changes: 44 additions & 8 deletions pkg/qbit/qbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,70 @@ package qbit

import (
"context"
"fmt"
"github.com/google/uuid"
"goBlack/common"
"goBlack/pkg/debrid"
"io"
"mime/multipart"
"os"
"path/filepath"
"strings"
"sync"
"time"
)

func (q *QBit) Process(ctx context.Context, magnet *common.Magnet, category string) (*Torrent, error) {
func (q *QBit) AddMagnet(ctx context.Context, url, category string) error {
magnet, err := common.GetMagnetFromUrl(url)
if err != nil {
q.logger.Printf("Error parsing magnet link: %v\n", err)
return err
}
err = q.Process(ctx, magnet, category)
if err != nil {
q.logger.Println("Failed to process magnet:", err)
return err
}
return nil
}

func (q *QBit) AddTorrent(ctx context.Context, fileHeader *multipart.FileHeader, category string) error {
file, _ := fileHeader.Open()
defer file.Close()
var reader io.Reader = file
magnet, err := common.GetMagnetFromFile(reader, fileHeader.Filename)
if err != nil {
q.logger.Printf("Error reading file: %s", fileHeader.Filename)
return err
}
err = q.Process(ctx, magnet, category)
if err != nil {
q.logger.Println("Failed to process torrent:", err)
return err
}
return nil
}

func (q *QBit) Process(ctx context.Context, magnet *common.Magnet, category string) error {
torrent := q.CreateTorrentFromMagnet(magnet, category)
go q.storage.AddOrUpdate(torrent)
arr := &debrid.Arr{
Name: category,
Token: ctx.Value("token").(string),
Host: ctx.Value("host").(string),
}
debridTorrent, err := debrid.ProcessQBitTorrent(q.debrid, magnet, arr)
if err != nil || debridTorrent == nil {
// Mark as failed
q.logger.Printf("Failed to process torrent: %s: %v", magnet.Name, err)
q.MarkAsFailed(torrent)
return torrent, err
if err == nil {
err = fmt.Errorf("failed to process torrent")
}
return err
}
torrent.ID = debridTorrent.Id
torrent.DebridTorrent = debridTorrent
torrent.Name = debridTorrent.Name
q.processFiles(torrent, debridTorrent, arr)
return torrent, nil
q.storage.AddOrUpdate(torrent)
go q.processFiles(torrent, debridTorrent, arr) // We can send async for file processing not to delay the response
return nil
}

func (q *QBit) CreateTorrentFromMagnet(magnet *common.Magnet, category string) *Torrent {
Expand Down Expand Up @@ -72,6 +106,7 @@ func (q *QBit) processFiles(torrent *Torrent, debridTorrent *debrid.Torrent, arr
rCloneBase := q.debrid.GetMountPath()
torrentPath, err := q.getTorrentPath(rCloneBase, debridTorrent) // /MyTVShow/
if err != nil {
q.MarkAsFailed(torrent)
q.logger.Printf("Error: %v", err)
return
}
Expand All @@ -80,6 +115,7 @@ func (q *QBit) processFiles(torrent *Torrent, debridTorrent *debrid.Torrent, arr
err = os.MkdirAll(torrentSymlinkPath, os.ModePerm)
if err != nil {
q.logger.Printf("Failed to create directory: %s\n", torrentSymlinkPath)
q.MarkAsFailed(torrent)
return
}
torrentRclonePath := filepath.Join(rCloneBase, torrentPath)
Expand Down

0 comments on commit 0198111

Please sign in to comment.