Skip to content

Commit

Permalink
Merge branch 'alist-org:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
juejijianghuaa authored Sep 6, 2023
2 parents 2a95ad3 + f2f312b commit bc8979e
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var AdminCmd = &cobra.Command{
Short: "Show admin user's info and some operations about admin user's password",
Run: func(cmd *cobra.Command, args []string) {
Init()
defer Release()
admin, err := op.GetAdmin()
if err != nil {
utils.Log.Errorf("failed get admin user: %+v", err)
Expand Down Expand Up @@ -57,13 +58,15 @@ var ShowTokenCmd = &cobra.Command{
Short: "Show admin token",
Run: func(cmd *cobra.Command, args []string) {
Init()
defer Release()
token := setting.GetStr(conf.Token)
utils.Log.Infof("Admin token: %s", token)
},
}

func setAdminPassword(pwd string) {
Init()
defer Release()
admin, err := op.GetAdmin()
if err != nil {
utils.Log.Errorf("failed get admin user: %+v", err)
Expand Down
1 change: 1 addition & 0 deletions cmd/cancel2FA.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var Cancel2FACmd = &cobra.Command{
Short: "Delete 2FA of admin user",
Run: func(cmd *cobra.Command, args []string) {
Init()
defer Release()
admin, err := op.GetAdmin()
if err != nil {
utils.Log.Errorf("failed to get admin user: %+v", err)
Expand Down
5 changes: 5 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/alist-org/alist/v3/internal/bootstrap"
"github.com/alist-org/alist/v3/internal/bootstrap/data"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/pkg/utils"
log "github.com/sirupsen/logrus"
)
Expand All @@ -19,6 +20,10 @@ func Init() {
bootstrap.InitIndex()
}

func Release() {
db.Close()
}

var pid = -1
var pidFile string

Expand Down
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ the address is defined in config file`,
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
utils.Log.Println("Shutdown server...")

Release()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
var wg sync.WaitGroup
Expand Down
2 changes: 2 additions & 0 deletions cmd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var disableStorageCmd = &cobra.Command{
}
mountPath := args[0]
Init()
defer Release()
storage, err := db.GetStorageByMountPath(mountPath)
if err != nil {
utils.Log.Errorf("failed to query storage: %+v", err)
Expand Down Expand Up @@ -89,6 +90,7 @@ var listStorageCmd = &cobra.Command{
Short: "List all storages",
Run: func(cmd *cobra.Command, args []string) {
Init()
defer Release()
storages, _, err := db.GetStorages(1, -1)
if err != nil {
utils.Log.Errorf("failed to query storages: %+v", err)
Expand Down
14 changes: 14 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ func AutoMigrate(dst ...interface{}) error {
func GetDb() *gorm.DB {
return db
}

func Close() {
log.Info("closing db")
sqlDB, err := db.DB()
if err != nil {
log.Errorf("failed to get db: %s", err.Error())
return
}
err = sqlDB.Close()
if err != nil {
log.Errorf("failed to close db: %s", err.Error())
return
}
}
3 changes: 2 additions & 1 deletion internal/net/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ func RequestHttp(ctx context.Context, httpMethod string, headerOverride http.Hea
res.Header.Del("set-cookie")
if res.StatusCode >= 400 {
all, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
msg := string(all)
log.Debugln(msg)
return res, errors.New(msg)
return nil, fmt.Errorf("http request [%s] failure,status: %d response:%s", URL, res.StatusCode, msg)
}
return res, nil
}
Expand Down
10 changes: 10 additions & 0 deletions internal/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ type FileStream struct {
peekBuff *bytes.Reader
}

func (f *FileStream) GetSize() int64 {
if f.tmpFile != nil {
info, err := f.tmpFile.Stat()
if err == nil {
return info.Size()
}
}
return f.Obj.GetSize()
}

func (f *FileStream) GetMimetype() string {
return f.Mimetype
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func CreateTempFile(r io.Reader, size int64) (*os.File, error) {
_ = os.Remove(f.Name())
return nil, errs.NewErr(err, "CreateTempFile failed")
}
if size != 0 && readBytes != size {
if size > 0 && readBytes != size {
_ = os.Remove(f.Name())
return nil, errs.NewErr(err, "CreateTempFile failed, incoming stream actual size= %d, expect = %d ", readBytes, size)
}
Expand Down
2 changes: 1 addition & 1 deletion server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Init(e *gin.Engine) {
Cors(e)
g := e.Group(conf.URL.Path)
if conf.Conf.Scheme.HttpPort != -1 && conf.Conf.Scheme.HttpsPort != -1 && conf.Conf.Scheme.ForceHttps {
g.Use(middlewares.ForceHttps)
e.Use(middlewares.ForceHttps)
}
g.Any("/ping", func(c *gin.Context) {
c.String(200, "pong")
Expand Down

0 comments on commit bc8979e

Please sign in to comment.