feat: file size limiter

This commit is contained in:
Simon Ding
2024-08-03 12:31:53 +08:00
parent 16216fcc4f
commit 241e30152b
16 changed files with 280 additions and 22 deletions

View File

@@ -3,8 +3,10 @@
package ent
import (
"encoding/json"
"fmt"
"polaris/ent/media"
"polaris/ent/schema"
"strings"
"time"
@@ -43,6 +45,8 @@ type Media struct {
TargetDir string `json:"target_dir,omitempty"`
// tv series only
DownloadHistoryEpisodes bool `json:"download_history_episodes,omitempty"`
// Limiter holds the value of the "limiter" field.
Limiter *schema.MediaLimiter `json:"limiter,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the MediaQuery when eager-loading is set.
Edges MediaEdges `json:"edges"`
@@ -72,6 +76,8 @@ func (*Media) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case media.FieldLimiter:
values[i] = new([]byte)
case media.FieldDownloadHistoryEpisodes:
values[i] = new(sql.NullBool)
case media.FieldID, media.FieldTmdbID, media.FieldStorageID:
@@ -179,6 +185,14 @@ func (m *Media) assignValues(columns []string, values []any) error {
} else if value.Valid {
m.DownloadHistoryEpisodes = value.Bool
}
case media.FieldLimiter:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field limiter", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &m.Limiter); err != nil {
return fmt.Errorf("unmarshal field limiter: %w", err)
}
}
default:
m.selectValues.Set(columns[i], values[i])
}
@@ -258,6 +272,9 @@ func (m *Media) String() string {
builder.WriteString(", ")
builder.WriteString("download_history_episodes=")
builder.WriteString(fmt.Sprintf("%v", m.DownloadHistoryEpisodes))
builder.WriteString(", ")
builder.WriteString("limiter=")
builder.WriteString(fmt.Sprintf("%v", m.Limiter))
builder.WriteByte(')')
return builder.String()
}

View File

@@ -41,6 +41,8 @@ const (
FieldTargetDir = "target_dir"
// FieldDownloadHistoryEpisodes holds the string denoting the download_history_episodes field in the database.
FieldDownloadHistoryEpisodes = "download_history_episodes"
// FieldLimiter holds the string denoting the limiter field in the database.
FieldLimiter = "limiter"
// EdgeEpisodes holds the string denoting the episodes edge name in mutations.
EdgeEpisodes = "episodes"
// Table holds the table name of the media in the database.
@@ -70,6 +72,7 @@ var Columns = []string{
FieldStorageID,
FieldTargetDir,
FieldDownloadHistoryEpisodes,
FieldLimiter,
}
// ValidColumn reports if the column name is valid (part of the table columns).

View File

@@ -775,6 +775,16 @@ func DownloadHistoryEpisodesNotNil() predicate.Media {
return predicate.Media(sql.FieldNotNull(FieldDownloadHistoryEpisodes))
}
// LimiterIsNil applies the IsNil predicate on the "limiter" field.
func LimiterIsNil() predicate.Media {
return predicate.Media(sql.FieldIsNull(FieldLimiter))
}
// LimiterNotNil applies the NotNil predicate on the "limiter" field.
func LimiterNotNil() predicate.Media {
return predicate.Media(sql.FieldNotNull(FieldLimiter))
}
// HasEpisodes applies the HasEdge predicate on the "episodes" edge.
func HasEpisodes() predicate.Media {
return predicate.Media(func(s *sql.Selector) {

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"polaris/ent/episode"
"polaris/ent/media"
"polaris/ent/schema"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
@@ -155,6 +156,12 @@ func (mc *MediaCreate) SetNillableDownloadHistoryEpisodes(b *bool) *MediaCreate
return mc
}
// SetLimiter sets the "limiter" field.
func (mc *MediaCreate) SetLimiter(sl *schema.MediaLimiter) *MediaCreate {
mc.mutation.SetLimiter(sl)
return mc
}
// AddEpisodeIDs adds the "episodes" edge to the Episode entity by IDs.
func (mc *MediaCreate) AddEpisodeIDs(ids ...int) *MediaCreate {
mc.mutation.AddEpisodeIDs(ids...)
@@ -340,6 +347,10 @@ func (mc *MediaCreate) createSpec() (*Media, *sqlgraph.CreateSpec) {
_spec.SetField(media.FieldDownloadHistoryEpisodes, field.TypeBool, value)
_node.DownloadHistoryEpisodes = value
}
if value, ok := mc.mutation.Limiter(); ok {
_spec.SetField(media.FieldLimiter, field.TypeJSON, value)
_node.Limiter = value
}
if nodes := mc.mutation.EpisodesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,

View File

@@ -9,6 +9,7 @@ import (
"polaris/ent/episode"
"polaris/ent/media"
"polaris/ent/predicate"
"polaris/ent/schema"
"time"
"entgo.io/ent/dialect/sql"
@@ -249,6 +250,18 @@ func (mu *MediaUpdate) ClearDownloadHistoryEpisodes() *MediaUpdate {
return mu
}
// SetLimiter sets the "limiter" field.
func (mu *MediaUpdate) SetLimiter(sl *schema.MediaLimiter) *MediaUpdate {
mu.mutation.SetLimiter(sl)
return mu
}
// ClearLimiter clears the value of the "limiter" field.
func (mu *MediaUpdate) ClearLimiter() *MediaUpdate {
mu.mutation.ClearLimiter()
return mu
}
// AddEpisodeIDs adds the "episodes" edge to the Episode entity by IDs.
func (mu *MediaUpdate) AddEpisodeIDs(ids ...int) *MediaUpdate {
mu.mutation.AddEpisodeIDs(ids...)
@@ -401,6 +414,12 @@ func (mu *MediaUpdate) sqlSave(ctx context.Context) (n int, err error) {
if mu.mutation.DownloadHistoryEpisodesCleared() {
_spec.ClearField(media.FieldDownloadHistoryEpisodes, field.TypeBool)
}
if value, ok := mu.mutation.Limiter(); ok {
_spec.SetField(media.FieldLimiter, field.TypeJSON, value)
}
if mu.mutation.LimiterCleared() {
_spec.ClearField(media.FieldLimiter, field.TypeJSON)
}
if mu.mutation.EpisodesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -686,6 +705,18 @@ func (muo *MediaUpdateOne) ClearDownloadHistoryEpisodes() *MediaUpdateOne {
return muo
}
// SetLimiter sets the "limiter" field.
func (muo *MediaUpdateOne) SetLimiter(sl *schema.MediaLimiter) *MediaUpdateOne {
muo.mutation.SetLimiter(sl)
return muo
}
// ClearLimiter clears the value of the "limiter" field.
func (muo *MediaUpdateOne) ClearLimiter() *MediaUpdateOne {
muo.mutation.ClearLimiter()
return muo
}
// AddEpisodeIDs adds the "episodes" edge to the Episode entity by IDs.
func (muo *MediaUpdateOne) AddEpisodeIDs(ids ...int) *MediaUpdateOne {
muo.mutation.AddEpisodeIDs(ids...)
@@ -868,6 +899,12 @@ func (muo *MediaUpdateOne) sqlSave(ctx context.Context) (_node *Media, err error
if muo.mutation.DownloadHistoryEpisodesCleared() {
_spec.ClearField(media.FieldDownloadHistoryEpisodes, field.TypeBool)
}
if value, ok := muo.mutation.Limiter(); ok {
_spec.SetField(media.FieldLimiter, field.TypeJSON, value)
}
if muo.mutation.LimiterCleared() {
_spec.ClearField(media.FieldLimiter, field.TypeJSON)
}
if muo.mutation.EpisodesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,

View File

@@ -108,6 +108,7 @@ var (
{Name: "storage_id", Type: field.TypeInt, Nullable: true},
{Name: "target_dir", Type: field.TypeString, Nullable: true},
{Name: "download_history_episodes", Type: field.TypeBool, Nullable: true, Default: false},
{Name: "limiter", Type: field.TypeJSON, Nullable: true},
}
// MediaTable holds the schema information for the "media" table.
MediaTable = &schema.Table{

View File

@@ -13,6 +13,7 @@ import (
"polaris/ent/media"
"polaris/ent/notificationclient"
"polaris/ent/predicate"
"polaris/ent/schema"
"polaris/ent/settings"
"polaris/ent/storage"
"sync"
@@ -3600,6 +3601,7 @@ type MediaMutation struct {
addstorage_id *int
target_dir *string
download_history_episodes *bool
limiter **schema.MediaLimiter
clearedFields map[string]struct{}
episodes map[int]struct{}
removedepisodes map[int]struct{}
@@ -4268,6 +4270,55 @@ func (m *MediaMutation) ResetDownloadHistoryEpisodes() {
delete(m.clearedFields, media.FieldDownloadHistoryEpisodes)
}
// SetLimiter sets the "limiter" field.
func (m *MediaMutation) SetLimiter(sl *schema.MediaLimiter) {
m.limiter = &sl
}
// Limiter returns the value of the "limiter" field in the mutation.
func (m *MediaMutation) Limiter() (r *schema.MediaLimiter, exists bool) {
v := m.limiter
if v == nil {
return
}
return *v, true
}
// OldLimiter returns the old "limiter" field's value of the Media entity.
// If the Media 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 *MediaMutation) OldLimiter(ctx context.Context) (v *schema.MediaLimiter, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldLimiter is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldLimiter requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldLimiter: %w", err)
}
return oldValue.Limiter, nil
}
// ClearLimiter clears the value of the "limiter" field.
func (m *MediaMutation) ClearLimiter() {
m.limiter = nil
m.clearedFields[media.FieldLimiter] = struct{}{}
}
// LimiterCleared returns if the "limiter" field was cleared in this mutation.
func (m *MediaMutation) LimiterCleared() bool {
_, ok := m.clearedFields[media.FieldLimiter]
return ok
}
// ResetLimiter resets all changes to the "limiter" field.
func (m *MediaMutation) ResetLimiter() {
m.limiter = nil
delete(m.clearedFields, media.FieldLimiter)
}
// AddEpisodeIDs adds the "episodes" edge to the Episode entity by ids.
func (m *MediaMutation) AddEpisodeIDs(ids ...int) {
if m.episodes == nil {
@@ -4356,7 +4407,7 @@ func (m *MediaMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *MediaMutation) Fields() []string {
fields := make([]string, 0, 13)
fields := make([]string, 0, 14)
if m.tmdb_id != nil {
fields = append(fields, media.FieldTmdbID)
}
@@ -4396,6 +4447,9 @@ func (m *MediaMutation) Fields() []string {
if m.download_history_episodes != nil {
fields = append(fields, media.FieldDownloadHistoryEpisodes)
}
if m.limiter != nil {
fields = append(fields, media.FieldLimiter)
}
return fields
}
@@ -4430,6 +4484,8 @@ func (m *MediaMutation) Field(name string) (ent.Value, bool) {
return m.TargetDir()
case media.FieldDownloadHistoryEpisodes:
return m.DownloadHistoryEpisodes()
case media.FieldLimiter:
return m.Limiter()
}
return nil, false
}
@@ -4465,6 +4521,8 @@ func (m *MediaMutation) OldField(ctx context.Context, name string) (ent.Value, e
return m.OldTargetDir(ctx)
case media.FieldDownloadHistoryEpisodes:
return m.OldDownloadHistoryEpisodes(ctx)
case media.FieldLimiter:
return m.OldLimiter(ctx)
}
return nil, fmt.Errorf("unknown Media field %s", name)
}
@@ -4565,6 +4623,13 @@ func (m *MediaMutation) SetField(name string, value ent.Value) error {
}
m.SetDownloadHistoryEpisodes(v)
return nil
case media.FieldLimiter:
v, ok := value.(*schema.MediaLimiter)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetLimiter(v)
return nil
}
return fmt.Errorf("unknown Media field %s", name)
}
@@ -4634,6 +4699,9 @@ func (m *MediaMutation) ClearedFields() []string {
if m.FieldCleared(media.FieldDownloadHistoryEpisodes) {
fields = append(fields, media.FieldDownloadHistoryEpisodes)
}
if m.FieldCleared(media.FieldLimiter) {
fields = append(fields, media.FieldLimiter)
}
return fields
}
@@ -4660,6 +4728,9 @@ func (m *MediaMutation) ClearField(name string) error {
case media.FieldDownloadHistoryEpisodes:
m.ClearDownloadHistoryEpisodes()
return nil
case media.FieldLimiter:
m.ClearLimiter()
return nil
}
return fmt.Errorf("unknown Media nullable field %s", name)
}
@@ -4707,6 +4778,9 @@ func (m *MediaMutation) ResetField(name string) error {
case media.FieldDownloadHistoryEpisodes:
m.ResetDownloadHistoryEpisodes()
return nil
case media.FieldLimiter:
m.ResetLimiter()
return nil
}
return fmt.Errorf("unknown Media field %s", name)
}

View File

@@ -29,6 +29,7 @@ func (Media) Fields() []ent.Field {
field.Int("storage_id").Optional(),
field.String("target_dir").Optional(),
field.Bool("download_history_episodes").Optional().Default(false).Comment("tv series only"),
field.JSON("limiter", &MediaLimiter{}).Optional(),
}
}
@@ -38,3 +39,8 @@ func (Media) Edges() []ent.Edge {
edge.To("episodes", Episode.Type),
}
}
type MediaLimiter struct {
SizeMin int `json:"size_min"`
SizeMax int `json:"size_max"`
}