-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
160 lines (140 loc) · 4.24 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package service
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/bitxeno/atvloadly/internal/cfg"
"github.com/bitxeno/atvloadly/internal/db"
"github.com/bitxeno/atvloadly/internal/log"
"github.com/bitxeno/atvloadly/model"
"gorm.io/gorm"
)
func GetApp(id uint) (*model.InstalledApp, error) {
var apps model.InstalledApp
if result := db.Store().Where("id = ?", id).First(&apps); result.Error != nil {
return nil, result.Error
}
return &apps, nil
}
func GetAppList() ([]model.InstalledApp, error) {
var apps []model.InstalledApp
if result := db.Store().Order("created_at desc").Find(&apps); result.Error != nil {
return nil, result.Error
}
return apps, nil
}
func GetEnableAppList() ([]model.InstalledApp, error) {
var apps []model.InstalledApp
if result := db.Store().Where("enabled = ?", true).Order("created_at desc").Find(&apps); result.Error != nil {
return nil, result.Error
}
return apps, nil
}
func SaveApp(app model.InstalledApp) (*model.InstalledApp, error) {
// 查找之前的安装记录,存在记录直接更新旧的
var cur model.InstalledApp
result := db.Store().Where("udid=? and bundle_identifier=? and account=?", app.UDID, app.BundleIdentifier, app.Account).First(&cur)
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
log.Err(result.Error).Msg("保存安装记录时出错.")
return nil, result.Error
}
if result.Error == nil {
// 之前已安装过
app.ID = cur.ID
now := time.Now()
cur.IpaPath = app.IpaPath
cur.Icon = app.Icon
cur.Version = app.Version
cur.RefreshedDate = &now
cur.RefreshedResult = app.RefreshedResult
cur.Password = app.Password
// 把ipa/icon移动到id目录
saveDir := filepath.Join(cfg.Server.WorkDir, "ipa", fmt.Sprintf("%d", app.ID))
if cur.IpaPath != "" {
ipaPath := filepath.Join(saveDir, "app.ipa")
if err := os.Rename(cur.IpaPath, ipaPath); err != nil {
log.Err(err).Msgf("Can not move to %s", ipaPath)
} else {
cur.IpaPath = ipaPath
}
}
if cur.Icon != "" {
iconPath := filepath.Join(saveDir, "app.png")
if err := os.Rename(cur.Icon, iconPath); err != nil {
log.Err(err).Msgf("Can not move to %s", iconPath)
} else {
cur.Icon = iconPath
}
}
updateData := map[string]interface{}{
"ipa_path": cur.IpaPath,
"icon": cur.Icon,
"version": cur.Version,
"refreshed_date": cur.RefreshedDate,
"refreshed_result": cur.RefreshedResult,
"password": cur.Password,
}
if result := db.Store().Model(&cur).Updates(updateData); result.Error != nil {
return nil, result.Error
}
return &cur, nil
} else {
// 新安装
now := time.Now()
app.Enabled = true
app.InstalledDate = &now
if result := db.Store().Create(&app); result.Error != nil {
return nil, result.Error
}
// 把ipa/icon移动到id目录
saveDir := filepath.Join(cfg.Server.WorkDir, "ipa", fmt.Sprintf("%d", app.ID))
if err := os.MkdirAll(saveDir, os.ModePerm); err != nil {
panic("failed to create directory :" + saveDir)
}
if app.IpaPath != "" {
ipaPath := filepath.Join(saveDir, "app.ipa")
if err := os.Rename(app.IpaPath, ipaPath); err != nil {
log.Err(err).Msgf("Can not move to %s", ipaPath)
} else {
app.IpaPath = ipaPath
}
}
if app.Icon != "" {
iconPath := filepath.Join(saveDir, "app.png")
if err := os.Rename(app.Icon, iconPath); err != nil {
log.Err(err).Msgf("Can not move to %s", iconPath)
} else {
app.Icon = iconPath
}
}
updateData := map[string]interface{}{
"ipa_path": app.IpaPath,
"icon": app.Icon,
}
if result := db.Store().Model(&app).Updates(updateData); result.Error != nil {
return nil, result.Error
}
return &app, nil
}
}
func UpdateAppRefreshResult(app model.InstalledApp) error {
updateData := map[string]interface{}{
"refreshed_date": app.RefreshedDate,
"refreshed_result": app.RefreshedResult,
}
if result := db.Store().Model(&app).Updates(updateData); result.Error != nil {
return result.Error
}
return nil
}
func DeleteApp(id uint) (bool, error) {
if v, err := GetApp(id); err == nil {
if result := db.Store().Delete(&model.InstalledApp{}, id); result.Error != nil {
return false, result.Error
}
ipaDir := filepath.Dir(v.IpaPath)
_ = os.RemoveAll(ipaDir)
}
return true, nil
}