Skip to content

Commit

Permalink
v0.10.0
Browse files Browse the repository at this point in the history
- 配置文件版本号为`1.6.0`
- bolt数据自动迁移
  • Loading branch information
wetor committed Sep 27, 2023
1 parent 60820b9 commit b3f18a8
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 152 deletions.
25 changes: 4 additions & 21 deletions cmd/animego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func Main() {
// ===============================================================================================================
// 初始化renamer配置
renamer.Init(&renamer.Options{
WG: &WG,
UpdateDelaySecond: config.UpdateDelaySecond,
WG: &WG,
RefreshSecond: config.RefreshSecond,
})
// 第一个启用的rename插件
var rename api.RenamerPlugin
Expand All @@ -216,7 +216,7 @@ func Main() {
// 初始化database配置
database.Init(&database.Options{
DownloaderConf: database.DownloaderConf{
UpdateDelaySecond: config.UpdateDelaySecond,
RefreshSecond: config.RefreshSecond,
DownloadPath: xpath.P(config.DownloadPath),
SavePath: xpath.P(config.SavePath),
Category: config.Category,
Expand All @@ -235,7 +235,7 @@ func Main() {
// ===============================================================================================================
// 初始化downloader配置
downloader.Init(&downloader.Options{
RefreshSecond: config.UpdateDelaySecond,
RefreshSecond: config.RefreshSecond,
Category: config.Category,
Tag: config.Tag,
AllowDuplicateDownload: config.Download.AllowDuplicateDownload,
Expand All @@ -250,23 +250,6 @@ func Main() {
// 启动downloader
downloaderSrv.Start(ctx)

//manager.Init(&manager.Options{
// DownloaderConf: manager.DownloaderConf{
// UpdateDelaySecond: config.UpdateDelaySecond,
// DownloadPath: xpath.P(config.DownloadPath),
// SavePath: xpath.P(config.SavePath),
// Category: config.Category,
// Tag: config.Tag,
// AllowDuplicateDownload: config.Download.AllowDuplicateDownload,
// SeedingTimeMinute: config.Download.SeedingTimeMinute,
// Rename: config.Advanced.Download.Rename,
// },
// WG: &WG,
//})
//// 初始化manager
//managerSrv := manager.NewManager(qbittorrentSrv, bolt, renameSrv)
//// 启动manager
//managerSrv.Start(ctx)
// ===============================================================================================================
// 初始化parser配置
parser.Init(&parser.Options{
Expand Down
4 changes: 2 additions & 2 deletions configs/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const (
)

func (c *Config) Check() {
if c.Advanced.UpdateDelaySecond < UpdateDelaySecondMin {
if c.Advanced.RefreshSecond < UpdateDelaySecondMin {
log.Warnf("配置项advanced.update_delay_second值范围错误: %v, 已修改为: %v", UpdateDelaySecondMin)
c.Advanced.UpdateDelaySecond = UpdateDelaySecondMin
c.Advanced.RefreshSecond = UpdateDelaySecondMin
}
}
14 changes: 14 additions & 0 deletions configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/wetor/AnimeGo/configs"
"github.com/wetor/AnimeGo/pkg/log"
"github.com/wetor/AnimeGo/pkg/utils"
"github.com/wetor/AnimeGo/test"
)
Expand All @@ -15,7 +16,11 @@ const testdata = "config"

func TestMain(m *testing.M) {
_ = utils.CreateMutiDir("data")
log.Init(&log.Options{
File: "data/log.log",
})
m.Run()
log.Close()
_ = os.RemoveAll("data")
}

Expand Down Expand Up @@ -95,6 +100,15 @@ func TestUpdateConfig_152(t *testing.T) {
EqualFile(t, "data/animego.yaml", test.GetDataPath(testdata, "animego_152.yaml"))
}

func TestUpdateConfig_160(t *testing.T) {
configs.ConfigVersion = "1.6.0"
file, _ := test.GetData(testdata, "animego_152.yaml")
_ = os.WriteFile("data/animego.yaml", file, 0666)
configs.UpdateConfig("data/animego.yaml", false)

EqualFile(t, "data/animego.yaml", test.GetDataPath(testdata, "animego_160.yaml"))
}

func TestInitEnvConfig(t *testing.T) {
_ = os.Setenv("ANIMEGO_QBT_URL", "http://127.0.0.1:18080")
_ = os.Setenv("ANIMEGO_QBT_DOWNLOAD_PATH", "7766/download")
Expand Down
7 changes: 2 additions & 5 deletions configs/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ func defaultPlugin() {
}

func defaultAdvancedComment() {
configComment["update_delay_second_help"] = `每隔这一段时间,都会将下载队列添加到下载项、更新下载进度和重命名已完成下载项
等待过程是异步的,等待期间不影响操作
在下载项较多、等待时间过少时会出现请求超时,所以有个最小等待时间为2秒的限制
默认为10,最小值为2`
configComment["refresh_second_help"] = `下载器列表和重命名任务刷新间隔时间。默认为10,最小值为2`

configComment["rename_help"] = `下载状态顺序为: 创建下载项->下载->下载完成->做种->做种完成
可选值为: ['link', 'link_delete', 'move', 'wait_move']
Expand All @@ -111,7 +108,7 @@ func defaultAdvancedComment() {
}

func defaultAdvanced() {
defaultConfig.Advanced.UpdateDelaySecond = 10
defaultConfig.Advanced.RefreshSecond = 10

defaultConfig.Advanced.Request.TimeoutSecond = 5
defaultConfig.Advanced.Request.RetryNum = 3
Expand Down
2 changes: 1 addition & 1 deletion configs/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Setting struct {
}

type Advanced struct {
UpdateDelaySecond int `yaml:"update_delay_second" json:"update_delay_second" attr:"更新状态等待时间" comment_key:"update_delay_second_help"`
RefreshSecond int `yaml:"refresh_second" json:"refresh_second" attr:"刷新间隔时间" comment_key:"refresh_second_help"`

Redirect struct {
Mikan string `yaml:"mikan" json:"mikan" attr:"默认mikanani.me"`
Expand Down
129 changes: 104 additions & 25 deletions configs/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path"
"strings"
"time"

"github.com/jinzhu/copier"
"gopkg.in/yaml.v3"
Expand All @@ -19,9 +18,14 @@ import (
"github.com/wetor/AnimeGo/configs/version/v_141"
"github.com/wetor/AnimeGo/configs/version/v_150"
"github.com/wetor/AnimeGo/configs/version/v_151"
"github.com/wetor/AnimeGo/configs/version/v_152"
"github.com/wetor/AnimeGo/internal/animego/database"
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/models"
"github.com/wetor/AnimeGo/pkg/cache"
"github.com/wetor/AnimeGo/pkg/dirdb"
"github.com/wetor/AnimeGo/pkg/utils"
encoder "github.com/wetor/AnimeGo/third_party/yaml-encoder"
"github.com/wetor/AnimeGo/pkg/xpath"
)

type ConfigOnlyVersion struct {
Expand All @@ -44,6 +48,7 @@ var (
"1.5.0",
"1.5.1",
"1.5.2",
"1.6.0",
}
ConfigVersion = versions[len(versions)-1] // 当前配置文件版本

Expand Down Expand Up @@ -87,6 +92,11 @@ var (
Desc: "新增下载器独立下载路径设置",
UpdateFunc: update_151_152,
},
{
Name: versions[8],
Desc: "更改字段名,数据库迁移",
UpdateFunc: update_152_160,
},
}
)

Expand Down Expand Up @@ -429,7 +439,6 @@ func update_150_151(file string) {
}
// 强制写入
assets.WritePlugins(assets.Dir, path.Join(newConfig.DataPath, assets.Dir), false)

}

func update_151_152(file string) {
Expand All @@ -443,7 +452,7 @@ func update_151_152(file string) {
log.Fatal("配置文件加载错误:", err)
}

newConfig := DefaultConfig()
newConfig := &v_152.Config{}
err = copier.Copy(newConfig, oldConfig)
if err != nil {
log.Fatal("配置文件升级失败:", err)
Expand All @@ -463,38 +472,108 @@ func update_151_152(file string) {
}
// 强制写入
assets.WritePlugins(assets.Dir, path.Join(newConfig.DataPath, assets.Dir), false)

}

func encodeConfig(conf any) ([]byte, error) {
defaultSettingComment()
defaultAdvancedComment()
yml := encoder.NewEncoder(conf,
encoder.WithComments(encoder.CommentsOnHead),
encoder.WithCommentsMap(configComment),
)
content, err := yml.Encode()
func update_152_160(file string) {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
log.Fatal("配置文件加载错误:", err)
}
oldConfig := &v_152.Config{}
err = yaml.Unmarshal(data, oldConfig)
if err != nil {
log.Fatal("配置文件加载错误:", err)
}

newConfig := DefaultConfig()
err = copier.Copy(newConfig, oldConfig)
if err != nil {
log.Fatal("配置文件升级失败:", err)
}
newConfig.Version = "1.6.0"
constant.Init(&constant.Options{
DataPath: newConfig.DataPath,
})
log.Println("[变动] 配置项(advanced.update_delay_second) 变更为 advanced.refresh_second")
newConfig.Advanced.RefreshSecond = oldConfig.Advanced.UpdateDelaySecond

content, err := encodeConfig(newConfig)
if err != nil {
log.Fatal("配置文件升级失败:", err)
}
err = os.WriteFile(file, content, 0644)
if err != nil {
log.Fatal("配置文件升级失败:", err)
}
// 强制写入
assets.WritePlugins(assets.Dir, path.Join(newConfig.DataPath, assets.Dir), false)

log.Println("--------------------------------")
log.Println("[数据迁移] 数据库部分迁移到文件标记")

bolt2dirdb(constant.CacheFile, xpath.P(newConfig.Setting.SavePath))
}

func bolt2dirdb(boltPath, savePath string) {
if !utils.IsExist(boltPath) {
return
}
bolt := cache.NewBolt()
bolt.Open(boltPath)
defer bolt.Close()

keys := bolt.ListKey("name2status")
for _, key := range keys {

status := models.DownloadStatus{}
_ = bolt.Get("name2status", key, &status)
entity := models.AnimeEntity{}
_ = bolt.Get("name2entity", key, &entity)

base := models.BaseDBEntity{
Hash: status.Hash,
Name: key,
CreateAt: utils.Unix(),
UpdateAt: utils.Unix(),
}
if len(status.Path) > 0 {
animePath := xpath.Root(status.Path[0])
_ = write(path.Join(savePath, animePath, database.AnimeDBName), models.AnimeDBEntity{
BaseDBEntity: base,
})
}
for i, f := range status.Path {
file := path.Join(savePath, f)
if utils.IsExist(file) {
filename := fmt.Sprintf(database.EpisodeDBFmt, strings.TrimSuffix(f, path.Ext(f)))
_ = write(path.Join(savePath, filename), models.EpisodeDBEntity{
BaseDBEntity: base,
StateDB: models.StateDB{
Seeded: true,
Downloaded: true,
Renamed: true,
Scraped: true,
},
Season: entity.Season,
Ep: entity.Ep[i].Ep,
Type: entity.Ep[i].Type,
})
}
}
}
return content, nil
}

func BackupConfig(file string, version string) error {
dir, name := path.Split(file)
ext := path.Ext(name)
name = strings.TrimSuffix(name, ext)
timeStr := time.Now().Format("20060102150405")
name = fmt.Sprintf("%s-%s-%s%s", name, version, timeStr, ext)
oldFile, err := os.ReadFile(file)
func write(file string, data any) error {
f := dirdb.NewFile(file)
err := f.Open()
if err != nil {
return err
}
out := path.Join(dir, name)
err = os.WriteFile(out, oldFile, 0644)
defer f.Close()
err = f.DB.Marshal(data)
if err != nil {
return err
}
log.Printf("备份原配置文件到:'%s'\n", out)
log.Printf("write %s: %+v\n", file, data)
return nil
}
37 changes: 37 additions & 0 deletions configs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package configs
import (
"fmt"
"log"
"os"
"path"
"reflect"
"strings"
"time"

"github.com/jinzhu/copier"
"github.com/pkg/errors"
encoder "github.com/wetor/AnimeGo/third_party/yaml-encoder"

"github.com/wetor/AnimeGo/internal/models"
)
Expand Down Expand Up @@ -69,3 +73,36 @@ func Env2Config(env *Environment, conf *Config, prefix string) error {

return nil
}

func encodeConfig(conf any) ([]byte, error) {
defaultSettingComment()
defaultAdvancedComment()
yml := encoder.NewEncoder(conf,
encoder.WithComments(encoder.CommentsOnHead),
encoder.WithCommentsMap(configComment),
)
content, err := yml.Encode()
if err != nil {
return nil, err
}
return content, nil
}

func BackupConfig(file string, version string) error {
dir, name := path.Split(file)
ext := path.Ext(name)
name = strings.TrimSuffix(name, ext)
timeStr := time.Now().Format("20060102150405")
name = fmt.Sprintf("%s-%s-%s%s", name, version, timeStr, ext)
oldFile, err := os.ReadFile(file)
if err != nil {
return err
}
out := path.Join(dir, name)
err = os.WriteFile(out, oldFile, 0644)
if err != nil {
return err
}
log.Printf("备份原配置文件到:'%s'\n", out)
return nil
}
7 changes: 3 additions & 4 deletions configs/version/v_151/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ type Plugin struct {
type Setting struct {
Client struct {
QBittorrent struct {
Url string `yaml:"url" json:"url"`
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
DownloadPath string `yaml:"download_path" json:"download_path"`
Url string `yaml:"url" json:"url"`
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
} `yaml:"qbittorrent" json:"qbittorrent"`
} `yaml:"client" json:"client"`
DownloadPath string `yaml:"download_path" json:"download_path"`
Expand Down
Loading

0 comments on commit b3f18a8

Please sign in to comment.