From 2c926df4f6e9022a85b968aa95e1b926689ac75d Mon Sep 17 00:00:00 2001 From: Mickael Kerjean Date: Wed, 23 Feb 2022 22:12:24 +1100 Subject: [PATCH] cleanup (samba): cleanup code in samba plugin --- server/model/files.go | 3 +++ server/plugin/plg_backend_samba/index.go | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/server/model/files.go b/server/model/files.go index 6a8b075c..0874cd44 100644 --- a/server/model/files.go +++ b/server/model/files.go @@ -51,6 +51,9 @@ func NewBackend(ctx *App, conn map[string]string) (IBackend, error) { } func GetHome(b IBackend, base string) (string, error) { + if strings.TrimSpace(base) == "" { + base = "/" + } home := "/" if obj, ok := b.(interface{ Home() (string, error) }); ok { tmp, err := obj.Home() diff --git a/server/plugin/plg_backend_samba/index.go b/server/plugin/plg_backend_samba/index.go index 7fe77ca5..804fc3b4 100644 --- a/server/plugin/plg_backend_samba/index.go +++ b/server/plugin/plg_backend_samba/index.go @@ -41,15 +41,18 @@ func (smb Samba) Init(params map[string]string, app *App) (IBackend, error) { if c != nil { return c.(*Samba), nil } - if params["port"] == "" { - params["port"] = "445" - } - if params["username"] == "" { - params["username"] = "Guest" - } conn, err := net.DialTimeout( "tcp", - fmt.Sprintf("%s:%s", params["host"], params["port"]), + fmt.Sprintf( + "%s:%s", + params["host"], + func() string { + if params["port"] == "" { + return "445" + } + return params["port"] + }(), + ), 10*time.Second, ) if err != nil { @@ -59,7 +62,12 @@ func (smb Samba) Init(params map[string]string, app *App) (IBackend, error) { s := &Samba{nil, make(map[string]*smb2.Share, 0)} s.session, err = (&smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ - User: params["username"], + User: func() string { + if params["username"] == "" { + return "Guest" + } + return params["username"] + }(), Password: params["password"], Domain: params["domain"], },