Files
music-agregator/internal/services/torrent.go
T
Alexander 41fb033d30 refactor: rewrite project from Rust to Go
- Replace Axum with Chi router
- Replace sqlx with pgx for PostgreSQL
- Replace tonic/prost with grpc-go
- Replace tracing with zerolog
- Update flake.nix for Go build with protoc generation
- Preserve all existing endpoints and functionality

Stack: Chi, pgx, grpc-go, zerolog, yaml.v3
2026-04-29 10:45:05 +02:00

99 lines
2.3 KiB
Go

package services
import (
"context"
"github.com/fujin/music-agregator/internal/config"
"github.com/fujin/music-agregator/internal/torrent"
)
type TorrentService struct {
client torrent.Client
}
func NewTorrentService(cfg config.TorrentConfig) (*TorrentService, error) {
var client torrent.Client
switch cfg.ClientType {
case config.TorrentClientQBittorrent:
c, err := torrent.NewQBittorrentClient(cfg.URL, cfg.Username, cfg.Password)
if err != nil {
return nil, err
}
client = c
case config.TorrentClientStub:
client = torrent.NewStubClient(cfg.LogPath, cfg.SavePath)
default:
return &TorrentService{client: nil}, nil
}
return &TorrentService{client: client}, nil
}
func (s *TorrentService) Connect(ctx context.Context) error {
if s.client == nil {
return nil
}
return s.client.Connect(ctx)
}
func (s *TorrentService) Disconnect(ctx context.Context) error {
if s.client == nil {
return nil
}
return s.client.Disconnect(ctx)
}
func (s *TorrentService) ListTorrents(ctx context.Context) ([]torrent.TorrentInfo, error) {
if s.client == nil {
return []torrent.TorrentInfo{}, nil
}
return s.client.ListTorrents(ctx)
}
func (s *TorrentService) GetTorrent(ctx context.Context, hash string) (*torrent.TorrentInfo, error) {
if s.client == nil {
return nil, torrent.ErrTorrentNotFound
}
return s.client.GetTorrent(ctx, hash)
}
func (s *TorrentService) AddTorrentURL(ctx context.Context, url string, savePath *string) error {
if s.client == nil {
return nil
}
return s.client.AddTorrentURL(ctx, url, savePath)
}
func (s *TorrentService) AddTorrentFile(ctx context.Context, data []byte, savePath *string) error {
if s.client == nil {
return nil
}
return s.client.AddTorrentFile(ctx, data, savePath)
}
func (s *TorrentService) RemoveTorrent(ctx context.Context, hash string, deleteFiles bool) error {
if s.client == nil {
return nil
}
return s.client.RemoveTorrent(ctx, hash, deleteFiles)
}
func (s *TorrentService) PauseTorrent(ctx context.Context, hash string) error {
if s.client == nil {
return nil
}
return s.client.PauseTorrent(ctx, hash)
}
func (s *TorrentService) ResumeTorrent(ctx context.Context, hash string) error {
if s.client == nil {
return nil
}
return s.client.ResumeTorrent(ctx, hash)
}
func (s *TorrentService) IsConfigured() bool {
return s.client != nil
}