2022-11-16 10:20:07 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-11-20 14:11:47 +00:00
|
|
|
"fmt"
|
2023-09-04 06:02:25 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-11-20 14:11:47 +00:00
|
|
|
"xorm.io/builder"
|
2022-11-16 10:20:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Subscriptions struct {
|
2023-04-22 15:20:10 +00:00
|
|
|
Id int `xorm:"pk autoincr INTEGER" json:"id"`
|
2023-09-04 06:02:25 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Schedule string `json:"schedule"`
|
2023-04-22 15:20:10 +00:00
|
|
|
IntervalSchedule map[string]any `xorm:"JSON" json:"interval_schedule"`
|
2023-09-04 06:02:25 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Whitelist string `json:"whitelist"`
|
|
|
|
Blacklist string `json:"blacklist"`
|
2023-04-22 15:20:10 +00:00
|
|
|
Status int `xorm:"INTEGER default(1)" json:"status"`
|
2023-09-04 06:02:25 +00:00
|
|
|
Dependences string `json:"dependences"`
|
|
|
|
Extensions string `json:"extensions"`
|
|
|
|
SubBefore string `json:"sub_before"`
|
|
|
|
SubAfter string `json:"sub_after"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
PullType string `json:"pull_type"`
|
2023-04-22 15:20:10 +00:00
|
|
|
PullOption string `xorm:"JSON" json:"pull_option"`
|
|
|
|
Pid int `xorm:"INTEGER" json:"pid"`
|
|
|
|
IsDisabled int `xorm:"INTEGER" json:"is_disabled"`
|
2023-09-04 06:02:25 +00:00
|
|
|
LogPath string `json:"log_path"`
|
|
|
|
ScheduleType string `json:"schedule_type"`
|
|
|
|
Alias string `xorm:"varchar(255) unique" json:"alias"`
|
2023-04-22 15:20:10 +00:00
|
|
|
Createdat string `xorm:"not null DATETIME created" json:"createdat"`
|
|
|
|
Updatedat string `xorm:"not null DATETIME updated" json:"updatedat"`
|
2023-09-04 06:02:25 +00:00
|
|
|
|
|
|
|
File io.WriteCloser `json:"-" xorm:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) Close() error {
|
|
|
|
return s.File.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) Write(p []byte) (n int, err error) {
|
|
|
|
if s.File == nil {
|
|
|
|
s.LogPath = "data/log/" + time.Now().Format("2006-01-02") + "/" + s.Alias + "_" + uuid.New().String() + ".log"
|
|
|
|
s.Status = 1
|
|
|
|
_ = UpdateSubscription(s)
|
|
|
|
_ = os.MkdirAll(filepath.Dir(s.LogPath), 0666)
|
|
|
|
s.File, _ = os.OpenFile(s.LogPath, os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
}
|
|
|
|
return s.File.Write(p)
|
|
|
|
}
|
|
|
|
func (s *Subscriptions) WriteString(data string) (n int, err error) {
|
|
|
|
p := []byte(data)
|
|
|
|
if s.File == nil {
|
|
|
|
s.LogPath = "data/log/" + time.Now().Format("2006-01-02") + "/" + s.Alias + "_" + uuid.New().String() + ".log"
|
|
|
|
s.Status = 1
|
|
|
|
_ = UpdateSubscription(s)
|
|
|
|
_ = os.MkdirAll(filepath.Dir(s.LogPath), 0666)
|
|
|
|
s.File, _ = os.OpenFile(s.LogPath, os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
}
|
|
|
|
return s.File.Write(p)
|
2022-11-20 14:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func QuerySubscription(searchValue string) ([]*Subscriptions, error) {
|
|
|
|
subscription := make([]*Subscriptions, 0)
|
|
|
|
session := engine.Table(new(Subscriptions)).
|
|
|
|
Where(
|
|
|
|
builder.Like{"name", "%" + searchValue + "%"}.
|
|
|
|
Or(builder.Like{"url", "%" + searchValue + "%"}))
|
|
|
|
err := session.Find(&subscription)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return subscription, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddSubscription(subscription *Subscriptions) (int, error) {
|
|
|
|
_, err := engine.Table(subscription).Insert(subscription)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
_, _ = engine.Where("name=?", subscription.Name).Get(subscription)
|
|
|
|
return subscription.Id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSubscription(id int) (*Subscriptions, error) {
|
|
|
|
env := new(Subscriptions)
|
|
|
|
_, err := engine.ID(id).Get(env)
|
|
|
|
return env, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateSubscription(subscription *Subscriptions) error {
|
|
|
|
_, err := engine.Table(subscription).ID(subscription.Id).AllCols().Update(subscription)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteSubscription(id int) error {
|
|
|
|
_, err := engine.Table(new(Subscriptions)).Delete(&Subscriptions{Id: id})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriptions) GetCron() string {
|
|
|
|
if s.ScheduleType == "interval" {
|
|
|
|
t := s.IntervalSchedule["type"].(string)
|
|
|
|
return fmt.Sprintf("@every %v%s", s.IntervalSchedule["value"], string(t[0]))
|
|
|
|
} else {
|
|
|
|
return s.Schedule
|
|
|
|
}
|
2022-11-16 10:20:07 +00:00
|
|
|
}
|