From 95984bb37c31af4882ff2d2da6a5e69b0b43f3a9 Mon Sep 17 00:00:00 2001 From: DarkGopher Date: Wed, 18 Jun 2025 21:54:17 +0200 Subject: [PATCH] Read ... --- storage/files/files.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/storage/files/files.go b/storage/files/files.go index c6a62e8..b67d206 100644 --- a/storage/files/files.go +++ b/storage/files/files.go @@ -5,6 +5,7 @@ import ( "bytes" "container/list" "encoding/gob" + "errors" "fmt" "os" "sync" @@ -60,25 +61,36 @@ func (pder *ProviderFiles) SetParams(p any) (err error) { } // Init create empty session file if not exists and retturn *Session -func (pder *ProviderFiles) Init(sid string) (err error) { +func (pder *ProviderFiles) Init(sid string) (sess *SessionFile, err error) { pder.lock.Lock() defer pder.lock.Unlock() var fd *os.File ckf := ckdirpath(sid) if fd, err = os.Create(ckf); err != nil { - return fmt.Errorf("create session file: %s failed with err: %w", ckf, err) + return nil, fmt.Errorf("create session file: %s failed with err: %w", ckf, err) } + defer fd.Close() pder.list.PushBack(sid) - return fd.Close() + return &SessionFile{sid}, nil } // Read return existing session by sid or new session if not exists func (pder *ProviderFiles) Read(sid string) (sess *SessionFile, err error) { pder.lock.Lock() defer pder.lock.Unlock() - //ckf := ckdirpath(sid) - //os.Stat(sessfile)... TODO - return + if pder.Exists(sid) { + return &SessionFile{sid}, nil + } + return pder.Init(sid) +} + +// Exists check if session sid exists in storage +func (pder *ProviderFiles) Exists(sid string) bool { + ckf := ckdirpath(sid) + if _, err := os.Stat(ckf); errors.Is(err, os.ErrExist) { + return false + } + return true } // SessionFile save session data into files using gob encode/dacode