feat: save hash instead of link, and refactor torrent download

This commit is contained in:
Simon Ding
2025-02-01 14:22:34 +08:00
parent 8b6558b2b5
commit 2821d49673
18 changed files with 463 additions and 236 deletions

View File

@@ -38,8 +38,10 @@ type History struct {
DownloadClientID int `json:"download_client_id,omitempty"`
// IndexerID holds the value of the "indexer_id" field.
IndexerID int `json:"indexer_id,omitempty"`
// Link holds the value of the "link" field.
// deprecated, use hash instead
Link string `json:"link,omitempty"`
// torrent hash
Hash string `json:"hash,omitempty"`
// Status holds the value of the "status" field.
Status history.Status `json:"status,omitempty"`
// deprecated
@@ -56,7 +58,7 @@ func (*History) scanValues(columns []string) ([]any, error) {
values[i] = new([]byte)
case history.FieldID, history.FieldMediaID, history.FieldEpisodeID, history.FieldSeasonNum, history.FieldSize, history.FieldDownloadClientID, history.FieldIndexerID:
values[i] = new(sql.NullInt64)
case history.FieldSourceTitle, history.FieldTargetDir, history.FieldLink, history.FieldStatus, history.FieldSaved:
case history.FieldSourceTitle, history.FieldTargetDir, history.FieldLink, history.FieldHash, history.FieldStatus, history.FieldSaved:
values[i] = new(sql.NullString)
case history.FieldDate:
values[i] = new(sql.NullTime)
@@ -149,6 +151,12 @@ func (h *History) assignValues(columns []string, values []any) error {
} else if value.Valid {
h.Link = value.String
}
case history.FieldHash:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field hash", values[i])
} else if value.Valid {
h.Hash = value.String
}
case history.FieldStatus:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field status", values[i])
@@ -230,6 +238,9 @@ func (h *History) String() string {
builder.WriteString("link=")
builder.WriteString(h.Link)
builder.WriteString(", ")
builder.WriteString("hash=")
builder.WriteString(h.Hash)
builder.WriteString(", ")
builder.WriteString("status=")
builder.WriteString(fmt.Sprintf("%v", h.Status))
builder.WriteString(", ")

View File

@@ -35,6 +35,8 @@ const (
FieldIndexerID = "indexer_id"
// FieldLink holds the string denoting the link field in the database.
FieldLink = "link"
// FieldHash holds the string denoting the hash field in the database.
FieldHash = "hash"
// FieldStatus holds the string denoting the status field in the database.
FieldStatus = "status"
// FieldSaved holds the string denoting the saved field in the database.
@@ -57,6 +59,7 @@ var Columns = []string{
FieldDownloadClientID,
FieldIndexerID,
FieldLink,
FieldHash,
FieldStatus,
FieldSaved,
}
@@ -160,6 +163,11 @@ func ByLink(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldLink, opts...).ToFunc()
}
// ByHash orders the results by the hash field.
func ByHash(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldHash, opts...).ToFunc()
}
// ByStatus orders the results by the status field.
func ByStatus(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldStatus, opts...).ToFunc()

View File

@@ -104,6 +104,11 @@ func Link(v string) predicate.History {
return predicate.History(sql.FieldEQ(FieldLink, v))
}
// Hash applies equality check predicate on the "hash" field. It's identical to HashEQ.
func Hash(v string) predicate.History {
return predicate.History(sql.FieldEQ(FieldHash, v))
}
// Saved applies equality check predicate on the "saved" field. It's identical to SavedEQ.
func Saved(v string) predicate.History {
return predicate.History(sql.FieldEQ(FieldSaved, v))
@@ -644,6 +649,81 @@ func LinkContainsFold(v string) predicate.History {
return predicate.History(sql.FieldContainsFold(FieldLink, v))
}
// HashEQ applies the EQ predicate on the "hash" field.
func HashEQ(v string) predicate.History {
return predicate.History(sql.FieldEQ(FieldHash, v))
}
// HashNEQ applies the NEQ predicate on the "hash" field.
func HashNEQ(v string) predicate.History {
return predicate.History(sql.FieldNEQ(FieldHash, v))
}
// HashIn applies the In predicate on the "hash" field.
func HashIn(vs ...string) predicate.History {
return predicate.History(sql.FieldIn(FieldHash, vs...))
}
// HashNotIn applies the NotIn predicate on the "hash" field.
func HashNotIn(vs ...string) predicate.History {
return predicate.History(sql.FieldNotIn(FieldHash, vs...))
}
// HashGT applies the GT predicate on the "hash" field.
func HashGT(v string) predicate.History {
return predicate.History(sql.FieldGT(FieldHash, v))
}
// HashGTE applies the GTE predicate on the "hash" field.
func HashGTE(v string) predicate.History {
return predicate.History(sql.FieldGTE(FieldHash, v))
}
// HashLT applies the LT predicate on the "hash" field.
func HashLT(v string) predicate.History {
return predicate.History(sql.FieldLT(FieldHash, v))
}
// HashLTE applies the LTE predicate on the "hash" field.
func HashLTE(v string) predicate.History {
return predicate.History(sql.FieldLTE(FieldHash, v))
}
// HashContains applies the Contains predicate on the "hash" field.
func HashContains(v string) predicate.History {
return predicate.History(sql.FieldContains(FieldHash, v))
}
// HashHasPrefix applies the HasPrefix predicate on the "hash" field.
func HashHasPrefix(v string) predicate.History {
return predicate.History(sql.FieldHasPrefix(FieldHash, v))
}
// HashHasSuffix applies the HasSuffix predicate on the "hash" field.
func HashHasSuffix(v string) predicate.History {
return predicate.History(sql.FieldHasSuffix(FieldHash, v))
}
// HashIsNil applies the IsNil predicate on the "hash" field.
func HashIsNil() predicate.History {
return predicate.History(sql.FieldIsNull(FieldHash))
}
// HashNotNil applies the NotNil predicate on the "hash" field.
func HashNotNil() predicate.History {
return predicate.History(sql.FieldNotNull(FieldHash))
}
// HashEqualFold applies the EqualFold predicate on the "hash" field.
func HashEqualFold(v string) predicate.History {
return predicate.History(sql.FieldEqualFold(FieldHash, v))
}
// HashContainsFold applies the ContainsFold predicate on the "hash" field.
func HashContainsFold(v string) predicate.History {
return predicate.History(sql.FieldContainsFold(FieldHash, v))
}
// StatusEQ applies the EQ predicate on the "status" field.
func StatusEQ(v Status) predicate.History {
return predicate.History(sql.FieldEQ(FieldStatus, v))

View File

@@ -134,6 +134,20 @@ func (hc *HistoryCreate) SetNillableLink(s *string) *HistoryCreate {
return hc
}
// SetHash sets the "hash" field.
func (hc *HistoryCreate) SetHash(s string) *HistoryCreate {
hc.mutation.SetHash(s)
return hc
}
// SetNillableHash sets the "hash" field if the given value is not nil.
func (hc *HistoryCreate) SetNillableHash(s *string) *HistoryCreate {
if s != nil {
hc.SetHash(*s)
}
return hc
}
// SetStatus sets the "status" field.
func (hc *HistoryCreate) SetStatus(h history.Status) *HistoryCreate {
hc.mutation.SetStatus(h)
@@ -290,6 +304,10 @@ func (hc *HistoryCreate) createSpec() (*History, *sqlgraph.CreateSpec) {
_spec.SetField(history.FieldLink, field.TypeString, value)
_node.Link = value
}
if value, ok := hc.mutation.Hash(); ok {
_spec.SetField(history.FieldHash, field.TypeString, value)
_node.Hash = value
}
if value, ok := hc.mutation.Status(); ok {
_spec.SetField(history.FieldStatus, field.TypeEnum, value)
_node.Status = value

View File

@@ -259,6 +259,26 @@ func (hu *HistoryUpdate) ClearLink() *HistoryUpdate {
return hu
}
// SetHash sets the "hash" field.
func (hu *HistoryUpdate) SetHash(s string) *HistoryUpdate {
hu.mutation.SetHash(s)
return hu
}
// SetNillableHash sets the "hash" field if the given value is not nil.
func (hu *HistoryUpdate) SetNillableHash(s *string) *HistoryUpdate {
if s != nil {
hu.SetHash(*s)
}
return hu
}
// ClearHash clears the value of the "hash" field.
func (hu *HistoryUpdate) ClearHash() *HistoryUpdate {
hu.mutation.ClearHash()
return hu
}
// SetStatus sets the "status" field.
func (hu *HistoryUpdate) SetStatus(h history.Status) *HistoryUpdate {
hu.mutation.SetStatus(h)
@@ -421,6 +441,12 @@ func (hu *HistoryUpdate) sqlSave(ctx context.Context) (n int, err error) {
if hu.mutation.LinkCleared() {
_spec.ClearField(history.FieldLink, field.TypeString)
}
if value, ok := hu.mutation.Hash(); ok {
_spec.SetField(history.FieldHash, field.TypeString, value)
}
if hu.mutation.HashCleared() {
_spec.ClearField(history.FieldHash, field.TypeString)
}
if value, ok := hu.mutation.Status(); ok {
_spec.SetField(history.FieldStatus, field.TypeEnum, value)
}
@@ -680,6 +706,26 @@ func (huo *HistoryUpdateOne) ClearLink() *HistoryUpdateOne {
return huo
}
// SetHash sets the "hash" field.
func (huo *HistoryUpdateOne) SetHash(s string) *HistoryUpdateOne {
huo.mutation.SetHash(s)
return huo
}
// SetNillableHash sets the "hash" field if the given value is not nil.
func (huo *HistoryUpdateOne) SetNillableHash(s *string) *HistoryUpdateOne {
if s != nil {
huo.SetHash(*s)
}
return huo
}
// ClearHash clears the value of the "hash" field.
func (huo *HistoryUpdateOne) ClearHash() *HistoryUpdateOne {
huo.mutation.ClearHash()
return huo
}
// SetStatus sets the "status" field.
func (huo *HistoryUpdateOne) SetStatus(h history.Status) *HistoryUpdateOne {
huo.mutation.SetStatus(h)
@@ -872,6 +918,12 @@ func (huo *HistoryUpdateOne) sqlSave(ctx context.Context) (_node *History, err e
if huo.mutation.LinkCleared() {
_spec.ClearField(history.FieldLink, field.TypeString)
}
if value, ok := huo.mutation.Hash(); ok {
_spec.SetField(history.FieldHash, field.TypeString, value)
}
if huo.mutation.HashCleared() {
_spec.ClearField(history.FieldHash, field.TypeString)
}
if value, ok := huo.mutation.Status(); ok {
_spec.SetField(history.FieldStatus, field.TypeEnum, value)
}

View File

@@ -83,6 +83,7 @@ var (
{Name: "download_client_id", Type: field.TypeInt, Nullable: true},
{Name: "indexer_id", Type: field.TypeInt, Nullable: true},
{Name: "link", Type: field.TypeString, Nullable: true},
{Name: "hash", Type: field.TypeString, Nullable: true},
{Name: "status", Type: field.TypeEnum, Enums: []string{"running", "success", "fail", "uploading", "seeding"}},
{Name: "saved", Type: field.TypeString, Nullable: true},
}

View File

@@ -2350,6 +2350,7 @@ type HistoryMutation struct {
indexer_id *int
addindexer_id *int
link *string
hash *string
status *history.Status
saved *string
clearedFields map[string]struct{}
@@ -3070,6 +3071,55 @@ func (m *HistoryMutation) ResetLink() {
delete(m.clearedFields, history.FieldLink)
}
// SetHash sets the "hash" field.
func (m *HistoryMutation) SetHash(s string) {
m.hash = &s
}
// Hash returns the value of the "hash" field in the mutation.
func (m *HistoryMutation) Hash() (r string, exists bool) {
v := m.hash
if v == nil {
return
}
return *v, true
}
// OldHash returns the old "hash" field's value of the History entity.
// If the History object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *HistoryMutation) OldHash(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldHash is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldHash requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldHash: %w", err)
}
return oldValue.Hash, nil
}
// ClearHash clears the value of the "hash" field.
func (m *HistoryMutation) ClearHash() {
m.hash = nil
m.clearedFields[history.FieldHash] = struct{}{}
}
// HashCleared returns if the "hash" field was cleared in this mutation.
func (m *HistoryMutation) HashCleared() bool {
_, ok := m.clearedFields[history.FieldHash]
return ok
}
// ResetHash resets all changes to the "hash" field.
func (m *HistoryMutation) ResetHash() {
m.hash = nil
delete(m.clearedFields, history.FieldHash)
}
// SetStatus sets the "status" field.
func (m *HistoryMutation) SetStatus(h history.Status) {
m.status = &h
@@ -3189,7 +3239,7 @@ func (m *HistoryMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *HistoryMutation) Fields() []string {
fields := make([]string, 0, 13)
fields := make([]string, 0, 14)
if m.media_id != nil {
fields = append(fields, history.FieldMediaID)
}
@@ -3223,6 +3273,9 @@ func (m *HistoryMutation) Fields() []string {
if m.link != nil {
fields = append(fields, history.FieldLink)
}
if m.hash != nil {
fields = append(fields, history.FieldHash)
}
if m.status != nil {
fields = append(fields, history.FieldStatus)
}
@@ -3259,6 +3312,8 @@ func (m *HistoryMutation) Field(name string) (ent.Value, bool) {
return m.IndexerID()
case history.FieldLink:
return m.Link()
case history.FieldHash:
return m.Hash()
case history.FieldStatus:
return m.Status()
case history.FieldSaved:
@@ -3294,6 +3349,8 @@ func (m *HistoryMutation) OldField(ctx context.Context, name string) (ent.Value,
return m.OldIndexerID(ctx)
case history.FieldLink:
return m.OldLink(ctx)
case history.FieldHash:
return m.OldHash(ctx)
case history.FieldStatus:
return m.OldStatus(ctx)
case history.FieldSaved:
@@ -3384,6 +3441,13 @@ func (m *HistoryMutation) SetField(name string, value ent.Value) error {
}
m.SetLink(v)
return nil
case history.FieldHash:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetHash(v)
return nil
case history.FieldStatus:
v, ok := value.(history.Status)
if !ok {
@@ -3521,6 +3585,9 @@ func (m *HistoryMutation) ClearedFields() []string {
if m.FieldCleared(history.FieldLink) {
fields = append(fields, history.FieldLink)
}
if m.FieldCleared(history.FieldHash) {
fields = append(fields, history.FieldHash)
}
if m.FieldCleared(history.FieldSaved) {
fields = append(fields, history.FieldSaved)
}
@@ -3556,6 +3623,9 @@ func (m *HistoryMutation) ClearField(name string) error {
case history.FieldLink:
m.ClearLink()
return nil
case history.FieldHash:
m.ClearHash()
return nil
case history.FieldSaved:
m.ClearSaved()
return nil
@@ -3600,6 +3670,9 @@ func (m *HistoryMutation) ResetField(name string) error {
case history.FieldLink:
m.ResetLink()
return nil
case history.FieldHash:
m.ResetHash()
return nil
case history.FieldStatus:
m.ResetStatus()
return nil

View File

@@ -23,7 +23,8 @@ func (History) Fields() []ent.Field {
field.Int("size").Default(0),
field.Int("download_client_id").Optional(),
field.Int("indexer_id").Optional(),
field.String("link").Optional(), //should be magnet link
field.String("link").Optional().Comment("deprecated, use hash instead"), //should be magnet link
field.String("hash").Optional().Comment("torrent hash"),
field.Enum("status").Values("running", "success", "fail", "uploading", "seeding"),
field.String("saved").Optional().Comment("deprecated"), //deprecated
}