transfer

package
v1.0.0-rc.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package transfer 实现转移做种:把下载器里已经下完的种子搬到另一个下载器继续做种,数据不动。

流程:拿到种子文件(qBittorrent 导出;否则按 pt-tools 的种子记录从站点重新下载,核对 info hash) → 暂停加入目标下载器,保存路径按两台下载器之间的路径映射换算(推送走 internal.PushTorrentToDownloader, 站点容量闸门照常,不占磁盘预算)→ 让目标校验 → 校验到 100% 恢复目标、从源移除(不删数据); 没到 100% 或超时就从目标移除(不删数据),源不动。每一步的状态写进 TorrentTransferJob,进程重启后接着做。

Index

Constants

View Source
const (
	JobsActive   = "active"
	JobsFinished = "finished"
	// MaxJobsListed 是列表最多返回的任务数(最新的在前)。
	MaxJobsListed = 200
	// MaxPathMaps 是一对下载器之间最多的路径映射条数。
	MaxPathMaps = 50
)

任务列表的筛选。

View Source
const (

	// MaxItems 是一次预览或建任务最多的种子数。
	MaxItems = 500
	// TransferSource 写进推送请求的来源。
	TransferSource = "transfer"
	// ReseedSource 是辅种的推送来源,也写进新种子记录的 download_source。
	ReseedSource = "reseed"
)
View Source
const (
	SourceExport = "export"
	SourceSite   = "site"
)

种子文件的来源。

Variables

View Source
var (
	// ErrRuleNotFound 表示规则不存在。
	ErrRuleNotFound = errors.New("转移规则不存在")
	// ErrRuleNameTaken 表示规则名称重复。
	ErrRuleNameTaken = errors.New("已经有同名的转移规则")
	// ErrPathMapInvalid 表示路径映射的设置不对。
	ErrPathMapInvalid = errors.New("路径映射无效")
)
View Source
var (
	// ErrInvalid 表示请求本身有问题(没选种子、太多、下载器不对)。
	ErrInvalid = errors.New("转移请求无效")
	// ErrNotCancelable 表示任务已经加入目标下载器,不能再取消。
	ErrNotCancelable = errors.New("任务已经加入目标下载器,不能取消")
	// ErrJobNotFound 表示任务不存在。
	ErrJobNotFound = errors.New("转移任务不存在")
	// ErrTargetUnavailable 表示目标下载器不存在、未启用或连不上。
	ErrTargetUnavailable = errors.New("目标下载器不可用")
)
View Source
var ErrJobActive = errors.New("这个种子已经有进行中的任务")

ErrJobActive 表示这个 info hash 已经有进行中的任务。

View Source
var ErrRuleInvalid = errors.New("转移规则无效")

ErrRuleInvalid 表示规则的设置不对。

Functions

func NormalizeRule

func NormalizeRule(r *models.TransferRule) error

NormalizeRule 校验并整理规则:名称必填,源与目标是两台不同的下载器,数值不能为负;0 的间隔和上限换成默认值。

func RuleDue

func RuleDue(r models.TransferRule, now time.Time) bool

RuleDue 报告规则这一刻该不该运行。

Types

type Config

type Config struct {
	DB          *gorm.DB
	Downloaders Downloaders
	// Sites 为 nil 时不能从站点重新下载种子文件(源下载器不能导出时就转不了)。
	Sites    SitesFunc
	Push     PushFunc
	Resolver *v2.TrackerResolver
	Now      func() time.Time
	Logger   *zap.SugaredLogger
	// CheckGrace 默认 2 分钟。
	CheckGrace time.Duration
}

Config 是 Service 的依赖。

type Downloaders

type Downloaders interface {
	TransferDownloader(ctx context.Context, id uint) (downloader.Downloader, models.DownloaderSetting, error)
}

Downloaders 按 ID 取下载器实例与配置;下载器不存在、未启用或连不上时返回错误。

type DownloadersFunc

type DownloadersFunc func(ctx context.Context, id uint) (downloader.Downloader, models.DownloaderSetting, error)

DownloadersFunc 把函数适配为 Downloaders。

func (DownloadersFunc) TransferDownloader

TransferDownloader 调用 f。

type Item

type Item struct {
	SourceID uint   `json:"source_id"`
	Hash     string `json:"hash"`
}

Item 是要转移的一个种子:在哪台源下载器、info hash 是什么。

type PathMapEntry

type PathMapEntry struct {
	SourcePrefix string `json:"source_prefix"`
	TargetPrefix string `json:"target_prefix"`
}

PathMapEntry 是一条路径映射。

type PreviewItem

type PreviewItem struct {
	SourceID   uint   `json:"source_id"`
	SourceName string `json:"source_name"`
	Hash       string `json:"hash"`
	Name       string `json:"name"`
	Size       int64  `json:"size"`
	SavePath   string `json:"save_path"`
	TargetPath string `json:"target_path"`
	// Mapped 表示路径按映射换算过;为 false 时目标用同一个路径。
	Mapped   bool   `json:"mapped"`
	SiteName string `json:"site_name,omitempty"`
	// Source 是种子文件的来源:export(从源下载器导出)或 site(按 pt-tools 的记录从站点重新下载)。
	Source string `json:"source,omitempty"`
	OK     bool   `json:"ok"`
	Reason string `json:"reason,omitempty"`
	// contains filtered or unexported fields
}

PreviewItem 是预览里的一行:能不能转、换算后的路径、种子文件从哪来。

type PushFunc

PushFunc 是推送入口,默认 internal.PushTorrentToDownloader。

type RuleResult

type RuleResult struct {
	Matched int      `json:"matched"`
	Created int      `json:"created"`
	Skipped []string `json:"skipped,omitempty"`
}

RuleResult 是规则一轮运行的结果。

func (RuleResult) Summary

func (r RuleResult) Summary() string

Summary 是写进规则 last_result 的一句话。

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service 预览、建立并推进转移任务。

func New

func New(cfg Config) *Service

New 构造 Service。

func (*Service) Cancel

func (s *Service) Cancel(ctx context.Context, id uint) error

Cancel 取消还没加入目标下载器的任务。

func (*Service) ClearFinished

func (s *Service) ClearFinished(ctx context.Context, kind string) (int64, error)

ClearFinished 删除这一种已经结束的任务记录,返回删除的条数。

func (*Service) Create

func (s *Service) Create(ctx context.Context, targetID uint, items []Item, ruleID *uint) ([]models.TorrentTransferJob, []PreviewItem, error)

Create 按预览的结果为能转的种子建任务,返回建好的任务与跳过的种子(带原因)。

func (*Service) DeleteRule

func (s *Service) DeleteRule(ctx context.Context, id uint) error

DeleteRule 删除规则;它建过的任务保留。

func (*Service) EnqueueReseed

func (s *Service) EnqueueReseed(ctx context.Context, j *models.TorrentTransferJob) error

EnqueueReseed 建一个辅种任务:种子文件已经下好并核对过(TorrentData),直接从「等待加入」开始, 加入下载器时暂停、保存路径用原种子的路径,校验到 100% 才开始做种,不到就移除;不移除任何原有的种子。

func (*Service) GetRule

func (s *Service) GetRule(ctx context.Context, id uint) (models.TransferRule, error)

GetRule 按 ID 取规则。

func (*Service) ListJobs

func (s *Service) ListJobs(ctx context.Context, status, kind string) ([]models.TorrentTransferJob, error)

ListJobs 返回最新的这一种任务;status 为 active 只列未结束的,finished 只列已结束的,其他值列全部。

func (*Service) ListPathMaps

func (s *Service) ListPathMaps(ctx context.Context, sourceID, targetID uint) ([]models.DownloaderPathMap, error)

ListPathMaps 返回路径映射;sourceID、targetID 都不为 0 时只返回这一对下载器的。

func (*Service) ListRules

func (s *Service) ListRules(ctx context.Context) ([]models.TransferRule, error)

ListRules 返回全部规则。

func (*Service) Now

func (s *Service) Now() time.Time

Now 返回服务的当前时间(测试里可以替换)。

func (*Service) Preview

func (s *Service) Preview(ctx context.Context, targetID uint, items []Item) ([]PreviewItem, error)

Preview 列出这些种子能不能转到目标下载器,以及换算后的路径。

func (*Service) RecordRuleRun

func (s *Service) RecordRuleRun(ctx context.Context, id uint, at time.Time, summary string) error

RecordRuleRun 记下规则的运行时间与结果。

func (*Service) RunOnce

func (s *Service) RunOnce(ctx context.Context) int

RunOnce 把所有没结束的任务各推进一步,返回状态有变化的任务数。 每台目标下载器同一时间只有一个任务在加入或校验,免得一次校验很多种子把硬盘占满。

func (*Service) RunRule

func (s *Service) RunRule(ctx context.Context, r models.TransferRule) (RuleResult, error)

RunRule 按规则在源下载器里挑种子建转移任务:已下完、不是刷流的种子,分类、标签、站点、做种时长都符合, 目标里还没有、也没有进行中的任务;按添加时间从早到晚,最多 MaxPerRun 个。

func (*Service) SavePathMaps

func (s *Service) SavePathMaps(ctx context.Context, sourceID, targetID uint, entries []PathMapEntry) ([]models.DownloaderPathMap, error)

SavePathMaps 用 entries 整体替换一对下载器之间的路径映射;前缀前后的空白去掉,两边都不能为空,源前缀不能重复。

func (*Service) SaveRule

SaveRule 新建(ID 为 0)或修改规则;运行时间与结果保持原样。

type SitesFunc

type SitesFunc func(siteName string) (v2.Site, bool)

SitesFunc 按站点名取共享的站点实例(与搜索、登录探测共用限速器);拿不到时第二个返回值为 false。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL