implement download feature

This commit is contained in:
Simon Ding
2024-07-10 17:35:16 +08:00
parent d7d5c72518
commit e500aaed1e
43 changed files with 4903 additions and 227 deletions

View File

@@ -5,8 +5,34 @@ const (
SettingLanguage = "language"
SettingJacketUrl = "jacket_url"
SettingJacketApiKey = "jacket_api_key"
SettingDownloadDir = "download_dir"
)
const (
IndexerTorznabImpl = "torznab"
)
)
type ResolutionType string
const (
Any ResolutionType = "any"
R720p ResolutionType = "720p"
R1080p ResolutionType = "1080p"
R4k ResolutionType = "4k"
)
func (r ResolutionType) String() string {
return string(r)
}
const (
ImplLocal = "local"
ImplWebdav = "webdav"
)
func StorageImplementations() []string {
return []string{
ImplLocal,
ImplWebdav,
}
}

135
db/db.go
View File

@@ -6,10 +6,14 @@ import (
"fmt"
"polaris/ent"
"polaris/ent/downloadclients"
"polaris/ent/history"
"polaris/ent/indexers"
"polaris/ent/series"
"polaris/ent/settings"
"polaris/ent/storage"
"polaris/log"
"slices"
"time"
"entgo.io/ent/dialect"
tmdb "github.com/cyruzin/golang-tmdb"
@@ -65,19 +69,32 @@ func (c *Client) GetLanguage() string {
return lang
}
func (c *Client) AddWatchlist(path string, detail *tmdb.TVDetails, episodes []int) (*ent.Series, error) {
func (c *Client) AddWatchlist(storageId int, nameEn string, detail *tmdb.TVDetails, episodes []int, res ResolutionType) (*ent.Series, error) {
count := c.ent.Series.Query().Where(series.TmdbID(int(detail.ID))).CountX(context.Background())
if count > 0 {
return nil, fmt.Errorf("tv series %s already in watchlist", detail.Name)
}
if res == "" {
res = R1080p
}
if storageId == 0 {
r, err := c.ent.Storage.Query().Where(storage.Default(true)).First(context.TODO())
if err == nil {
log.Infof("use default storage: %v", r.Name)
storageId = r.ID
}
}
r, err := c.ent.Series.Create().
SetTmdbID(int(detail.ID)).
SetPath(path).
SetStorageID(storageId).
SetOverview(detail.Overview).
SetName(detail.Name).
SetNameEn(nameEn).
SetOriginalName(detail.OriginalName).
SetPosterPath(detail.PosterPath).
SetAirDate(detail.FirstAirDate).
SetResolution(res.String()).
AddEpisodeIDs(episodes...).
Save(context.TODO())
return r, err
@@ -206,3 +223,117 @@ func (c *Client) GetAllDonloadClients() []*ent.DownloadClients {
func (c *Client) DeleteDownloadCLient(id int) {
c.ent.DownloadClients.Delete().Where(downloadclients.ID(id)).Exec(context.TODO())
}
// Storage is the model entity for the Storage schema.
type StorageInfo struct {
Name string `json:"name"`
Implementation string `json:"implementation"`
Path string `json:"path"`
User string `json:"user"`
Password string `json:"password"`
Default bool `json:"default"`
}
func (c *Client) AddStorage(s StorageInfo) error {
if !slices.Contains(StorageImplementations(), s.Implementation) {
return fmt.Errorf("implementation not supported: %v", s.Implementation)
}
count := c.ent.Storage.Query().Where(storage.Name(s.Name)).CountX(context.TODO())
if count > 0 {
//storage already exist, edit exist one
return c.ent.Storage.Update().Where(storage.Name(s.Name)).
SetImplementation(s.Implementation).
SetPath(s.Path).
SetUser(s.User).
SetPassword(s.Password).Exec(context.TODO())
}
_, err := c.ent.Storage.Create().SetName(s.Name).
SetImplementation(s.Implementation).
SetPath(s.Path).
SetUser(s.User).
SetPassword(s.Password).Save(context.TODO())
if err != nil {
return err
}
if s.Default {
return c.SetDefaultStorageByName(s.Name)
}
return nil
}
func (c *Client) GetAllStorage() []*ent.Storage {
data, err := c.ent.Storage.Query().Where(storage.Deleted(false)).All(context.TODO())
if err != nil {
log.Errorf("get storage: %v", err)
return nil
}
return data
}
func (c *Client) GetStorage(id int) *ent.Storage {
r, err := c.ent.Storage.Query().Where(storage.ID(id)).First(context.TODO())
if err != nil {
//use default storage
return c.ent.Storage.Query().Where(storage.Default(true)).FirstX(context.TODO())
}
return r
}
func (c *Client) DeleteStorage(id int) error {
return c.ent.Storage.Update().Where(storage.ID(id)).SetDeleted(true).Exec(context.TODO())
}
func (c *Client) SetDefaultStorage(id int) error {
err := c.ent.Storage.Update().Where(storage.ID(id)).SetDefault(true).Exec(context.TODO())
if err != nil {
return err
}
err = c.ent.Storage.Update().Where(storage.Or(storage.ID(id))).SetDefault(false).Exec(context.TODO())
return err
}
func (c *Client) SetDefaultStorageByName(name string) error {
err := c.ent.Storage.Update().Where(storage.Name(name)).SetDefault(true).Exec(context.TODO())
if err != nil {
return err
}
err = c.ent.Storage.Update().Where(storage.Or(storage.Name(name))).SetDefault(false).Exec(context.TODO())
return err
}
func (c *Client) SaveHistoryRecord(h ent.History) (*ent.History,error) {
return c.ent.History.Create().SetSeriesID(h.SeriesID).SetEpisodeID(h.EpisodeID).SetDate(time.Now()).
SetCompleted(h.Completed).SetTargetDir(h.TargetDir).SetSourceTitle(h.SourceTitle).SetSaved(h.Saved).Save(context.TODO())
}
func (c *Client) SetHistoryComplete(id int) error {
return c.ent.History.Update().Where(history.ID(id)).SetCompleted(true).Exec(context.TODO())
}
func (c *Client) GetHistories() ent.Histories {
h, err := c.ent.History.Query().All(context.TODO())
if err != nil {
return nil
}
return h
}
func (c *Client) GetHistory(id int) *ent.History {
return c.ent.History.Query().Where(history.ID(id)).FirstX(context.TODO())
}
func (c *Client) DeleteHistory(id int) error {
_, err := c.ent.History.Delete().Where(history.ID(id)).Exec(context.Background())
return err
}
func (c *Client) GetDownloadDir() string {
r, err := c.ent.Settings.Query().Where(settings.Key(SettingDownloadDir)).First(context.TODO())
if err != nil {
return "/download"
}
return r.Value
}