Skip to content

Commit 512b184

Browse files
authored
Merge branch 'main' into renovate/gorm.io-driver-mysql-1.x
2 parents d4f6591 + 9469c95 commit 512b184

9 files changed

Lines changed: 117 additions & 29 deletions

File tree

cmd/admin.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"fmt"
8+
79
"github.com/OpenListTeam/OpenList/v4/internal/conf"
810
"github.com/OpenListTeam/OpenList/v4/internal/op"
911
"github.com/OpenListTeam/OpenList/v4/internal/setting"
@@ -24,10 +26,11 @@ var AdminCmd = &cobra.Command{
2426
if err != nil {
2527
utils.Log.Errorf("failed get admin user: %+v", err)
2628
} else {
27-
utils.Log.Infof("Admin user's username: %s", admin.Username)
28-
utils.Log.Infof("The password can only be output at the first startup, and then stored as a hash value, which cannot be reversed")
29-
utils.Log.Infof("You can reset the password with a random string by running [openlist admin random]")
30-
utils.Log.Infof("You can also set a new password by running [openlist admin set NEW_PASSWORD]")
29+
utils.Log.Infof("get admin user from CLI")
30+
fmt.Println("Admin user's username:", admin.Username)
31+
fmt.Println("The password can only be output at the first startup, and then stored as a hash value, which cannot be reversed")
32+
fmt.Println("You can reset the password with a random string by running [openlist admin random]")
33+
fmt.Println("You can also set a new password by running [openlist admin set NEW_PASSWORD]")
3134
}
3235
},
3336
}
@@ -36,6 +39,7 @@ var RandomPasswordCmd = &cobra.Command{
3639
Use: "random",
3740
Short: "Reset admin user's password to a random string",
3841
Run: func(cmd *cobra.Command, args []string) {
42+
utils.Log.Infof("reset admin user's password to a random string from CLI")
3943
newPwd := random.String(8)
4044
setAdminPassword(newPwd)
4145
},
@@ -44,12 +48,12 @@ var RandomPasswordCmd = &cobra.Command{
4448
var SetPasswordCmd = &cobra.Command{
4549
Use: "set",
4650
Short: "Set admin user's password",
47-
Run: func(cmd *cobra.Command, args []string) {
51+
RunE: func(cmd *cobra.Command, args []string) error {
4852
if len(args) == 0 {
49-
utils.Log.Errorf("Please enter the new password")
50-
return
53+
return fmt.Errorf("Please enter the new password")
5154
}
5255
setAdminPassword(args[0])
56+
return nil
5357
},
5458
}
5559

@@ -60,7 +64,8 @@ var ShowTokenCmd = &cobra.Command{
6064
Init()
6165
defer Release()
6266
token := setting.GetStr(conf.Token)
63-
utils.Log.Infof("Admin token: %s", token)
67+
utils.Log.Infof("show admin token from CLI")
68+
fmt.Println("Admin token:", token)
6469
},
6570
}
6671

@@ -77,9 +82,10 @@ func setAdminPassword(pwd string) {
7782
utils.Log.Errorf("failed update admin user: %+v", err)
7883
return
7984
}
80-
utils.Log.Infof("admin user has been updated:")
81-
utils.Log.Infof("username: %s", admin.Username)
82-
utils.Log.Infof("password: %s", pwd)
85+
utils.Log.Infof("admin user has been update from CLI")
86+
fmt.Println("admin user has been updated:")
87+
fmt.Println("username:", admin.Username)
88+
fmt.Println("password:", pwd)
8389
DelAdminCacheOnline()
8490
}
8591

cmd/cancel2FA.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"fmt"
8+
79
"github.com/OpenListTeam/OpenList/v4/internal/op"
810
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
911
"github.com/spf13/cobra"
@@ -24,7 +26,8 @@ var Cancel2FACmd = &cobra.Command{
2426
if err != nil {
2527
utils.Log.Errorf("failed to cancel 2FA: %+v", err)
2628
} else {
27-
utils.Log.Info("2FA canceled")
29+
utils.Log.Infof("2FA is canceled from CLI")
30+
fmt.Println("2FA canceled")
2831
DelAdminCacheOnline()
2932
}
3033
}

cmd/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ the address is defined in config file`,
6565
var httpSrv, httpsSrv, unixSrv *http.Server
6666
if conf.Conf.Scheme.HttpPort != -1 {
6767
httpBase := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.Scheme.HttpPort)
68+
fmt.Printf("start HTTP server @ %s\n", httpBase)
6869
utils.Log.Infof("start HTTP server @ %s", httpBase)
6970
httpSrv = &http.Server{Addr: httpBase, Handler: httpHandler}
7071
go func() {
@@ -76,6 +77,7 @@ the address is defined in config file`,
7677
}
7778
if conf.Conf.Scheme.HttpsPort != -1 {
7879
httpsBase := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.Scheme.HttpsPort)
80+
fmt.Printf("start HTTPS server @ %s\n", httpsBase)
7981
utils.Log.Infof("start HTTPS server @ %s", httpsBase)
8082
httpsSrv = &http.Server{Addr: httpsBase, Handler: r}
8183
go func() {
@@ -86,6 +88,7 @@ the address is defined in config file`,
8688
}()
8789
}
8890
if conf.Conf.Scheme.UnixFile != "" {
91+
fmt.Printf("start unix server @ %s\n", conf.Conf.Scheme.UnixFile)
8992
utils.Log.Infof("start unix server @ %s", conf.Conf.Scheme.UnixFile)
9093
unixSrv = &http.Server{Handler: httpHandler}
9194
go func() {
@@ -114,6 +117,7 @@ the address is defined in config file`,
114117
s3r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
115118
server.InitS3(s3r)
116119
s3Base := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.S3.Port)
120+
fmt.Printf("start S3 server @ %s\n", s3Base)
117121
utils.Log.Infof("start S3 server @ %s", s3Base)
118122
go func() {
119123
var err error
@@ -138,6 +142,7 @@ the address is defined in config file`,
138142
if err != nil {
139143
utils.Log.Fatalf("failed to start ftp driver: %s", err.Error())
140144
} else {
145+
fmt.Printf("start ftp server on %s\n", conf.Conf.FTP.Listen)
141146
utils.Log.Infof("start ftp server on %s", conf.Conf.FTP.Listen)
142147
go func() {
143148
ftpServer = ftpserver.NewFtpServer(ftpDriver)
@@ -156,6 +161,7 @@ the address is defined in config file`,
156161
if err != nil {
157162
utils.Log.Fatalf("failed to start sftp driver: %s", err.Error())
158163
} else {
164+
fmt.Printf("start sftp server on %s", conf.Conf.SFTP.Listen)
159165
utils.Log.Infof("start sftp server on %s", conf.Conf.SFTP.Listen)
160166
go func() {
161167
sftpServer = sftpd.NewSftpServer(sftpDriver)

cmd/storage.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"fmt"
78
"os"
89
"strconv"
910

1011
"github.com/OpenListTeam/OpenList/v4/internal/db"
11-
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1212
"github.com/charmbracelet/bubbles/table"
1313
tea "github.com/charmbracelet/bubbletea"
1414
"github.com/charmbracelet/lipgloss"
@@ -24,26 +24,26 @@ var storageCmd = &cobra.Command{
2424
var disableStorageCmd = &cobra.Command{
2525
Use: "disable",
2626
Short: "Disable a storage",
27-
Run: func(cmd *cobra.Command, args []string) {
27+
RunE: func(cmd *cobra.Command, args []string) error {
2828
if len(args) < 1 {
29-
utils.Log.Errorf("mount path is required")
30-
return
29+
return fmt.Errorf("mount path is required")
3130
}
3231
mountPath := args[0]
3332
Init()
3433
defer Release()
3534
storage, err := db.GetStorageByMountPath(mountPath)
3635
if err != nil {
37-
utils.Log.Errorf("failed to query storage: %+v", err)
36+
return fmt.Errorf("failed to query storage: %+v", err)
3837
} else {
3938
storage.Disabled = true
4039
err = db.UpdateStorage(storage)
4140
if err != nil {
42-
utils.Log.Errorf("failed to update storage: %+v", err)
41+
return fmt.Errorf("failed to update storage: %+v", err)
4342
} else {
44-
utils.Log.Infof("Storage with mount path [%s] have been disabled", mountPath)
43+
fmt.Printf("Storage with mount path [%s] have been disabled\n", mountPath)
4544
}
4645
}
46+
return nil
4747
},
4848
}
4949

@@ -88,14 +88,14 @@ var storageTableHeight int
8888
var listStorageCmd = &cobra.Command{
8989
Use: "list",
9090
Short: "List all storages",
91-
Run: func(cmd *cobra.Command, args []string) {
91+
RunE: func(cmd *cobra.Command, args []string) error {
9292
Init()
9393
defer Release()
9494
storages, _, err := db.GetStorages(1, -1)
9595
if err != nil {
96-
utils.Log.Errorf("failed to query storages: %+v", err)
96+
return fmt.Errorf("failed to query storages: %+v", err)
9797
} else {
98-
utils.Log.Infof("Found %d storages", len(storages))
98+
fmt.Printf("Found %d storages\n", len(storages))
9999
columns := []table.Column{
100100
{Title: "ID", Width: 4},
101101
{Title: "Driver", Width: 16},
@@ -138,10 +138,11 @@ var listStorageCmd = &cobra.Command{
138138

139139
m := model{t}
140140
if _, err := tea.NewProgram(m).Run(); err != nil {
141-
utils.Log.Errorf("failed to run program: %+v", err)
141+
fmt.Printf("failed to run program: %+v\n", err)
142142
os.Exit(1)
143143
}
144144
}
145+
return nil
145146
},
146147
}
147148

internal/bootstrap/data/user.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package data
22

33
import (
44
"fmt"
5-
"time"
65
"os"
76

87
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
@@ -40,18 +39,19 @@ func initUser() {
4039
if err := op.CreateUser(admin); err != nil {
4140
panic(err)
4241
} else {
43-
utils.Log.Infof("Successfully created the admin user and the initial password is: %s", adminPassword)
44-
fmt.Printf("\033[36mINFO\033[39m[%s] Successfully created the admin user and the initial password is: %s\n", time.Now().Format("2006-01-02 15:04:05"), adminPassword)
42+
// DO NOT output the password to log file. Only output to console.
43+
// utils.Log.Infof("Successfully created the admin user and the initial password is: %s", adminPassword)
44+
fmt.Printf("Successfully created the admin user and the initial password is: %s", adminPassword)
4545
}
4646
} else {
4747
utils.Log.Fatalf("[init user] Failed to get admin user: %v", err)
4848
}
4949
}
50-
guest, err := op.GetGuest()
50+
_, err = op.GetGuest()
5151
if err != nil {
5252
if errors.Is(err, gorm.ErrRecordNotFound) {
5353
salt := random.String(16)
54-
guest = &model.User{
54+
guest := &model.User{
5555
Username: "guest",
5656
PwdHash: model.TwoHashPwd("guest", salt),
5757
Salt: salt,

internal/bootstrap/patch/all.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v3_24_0"
55
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v3_32_0"
66
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v3_41_0"
7+
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v3_all"
78
)
89

910
type VersionPatches struct {
@@ -32,4 +33,10 @@ var UpgradePatches = []VersionPatches{
3233
v3_41_0.GrantAdminPermissions,
3334
},
3435
},
36+
{
37+
Version: "v3.0.0",
38+
Patches: []func(){
39+
v3_all.RenameAlistV3Driver,
40+
},
41+
},
3542
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package v3_all
2+
3+
import (
4+
"github.com/OpenListTeam/OpenList/v4/internal/db"
5+
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
6+
)
7+
8+
// Rename Alist V3 driver to OpenList
9+
func RenameAlistV3Driver() {
10+
storages, _, err := db.GetStorages(1, -1)
11+
if err != nil {
12+
utils.Log.Errorf("[RenameAlistV3Driver] failed to get storages: %s", err.Error())
13+
return
14+
}
15+
16+
updatedCount := 0
17+
for _, s := range storages {
18+
if s.Driver == "AList V3" {
19+
utils.Log.Warnf("[RenameAlistV3Driver] rename storage [%d]%s from Alist V3 to OpenList", s.ID, s.MountPath)
20+
s.Driver = "OpenList"
21+
err = db.UpdateStorage(&s)
22+
if err != nil {
23+
utils.Log.Errorf("[RenameAlistV3Driver] failed to update storage [%d]%s: %s", s.ID, s.MountPath, err.Error())
24+
} else {
25+
updatedCount++
26+
}
27+
}
28+
}
29+
30+
if updatedCount > 0 {
31+
utils.Log.Infof("[RenameAlistV3Driver] updated %d storages from Alist V3 to OpenList", updatedCount)
32+
}
33+
}

internal/op/storage.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package op
33
import (
44
"context"
55
"fmt"
6+
"reflect"
67
"runtime"
78
"sort"
89
"strings"
@@ -135,7 +136,11 @@ func initStorage(ctx context.Context, storage model.Storage, storageDriver drive
135136
}
136137
storagesMap.Store(driverStorage.MountPath, storageDriver)
137138
if err != nil {
138-
driverStorage.SetStatus(err.Error())
139+
if IsUseOnlineAPI(storageDriver) {
140+
driverStorage.SetStatus(utils.SanitizeHTML(err.Error()))
141+
} else {
142+
driverStorage.SetStatus(err.Error())
143+
}
139144
err = errors.Wrap(err, "failed init storage")
140145
} else {
141146
driverStorage.SetStatus(WORK)
@@ -144,6 +149,24 @@ func initStorage(ctx context.Context, storage model.Storage, storageDriver drive
144149
return err
145150
}
146151

152+
func IsUseOnlineAPI(storageDriver driver.Driver) bool {
153+
v := reflect.ValueOf(storageDriver.GetAddition())
154+
if v.Kind() == reflect.Ptr {
155+
v = v.Elem()
156+
}
157+
if !v.IsValid() || v.Kind() != reflect.Struct {
158+
return false
159+
}
160+
field_v := v.FieldByName("UseOnlineAPI")
161+
if !field_v.IsValid() {
162+
return false
163+
}
164+
if field_v.Kind() != reflect.Bool {
165+
return false
166+
}
167+
return field_v.Bool()
168+
}
169+
147170
func EnableStorage(ctx context.Context, id uint) error {
148171
storage, err := db.GetStorageById(id)
149172
if err != nil {

pkg/utils/html.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
import "github.com/microcosm-cc/bluemonday"
4+
5+
var htmlSanitizePolicy = bluemonday.StrictPolicy()
6+
7+
func SanitizeHTML(s string) string {
8+
return htmlSanitizePolicy.Sanitize(s)
9+
}

0 commit comments

Comments
 (0)