This commit is contained in:
DarkGopher 2025-06-18 21:54:17 +02:00
parent f168c961dc
commit 95984bb37c

View file

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"container/list" "container/list"
"encoding/gob" "encoding/gob"
"errors"
"fmt" "fmt"
"os" "os"
"sync" "sync"
@ -60,25 +61,36 @@ func (pder *ProviderFiles) SetParams(p any) (err error) {
} }
// Init create empty session file if not exists and retturn *Session // 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() pder.lock.Lock()
defer pder.lock.Unlock() defer pder.lock.Unlock()
var fd *os.File var fd *os.File
ckf := ckdirpath(sid) ckf := ckdirpath(sid)
if fd, err = os.Create(ckf); err != nil { 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) pder.list.PushBack(sid)
return fd.Close() return &SessionFile{sid}, nil
} }
// Read return existing session by sid or new session if not exists // Read return existing session by sid or new session if not exists
func (pder *ProviderFiles) Read(sid string) (sess *SessionFile, err error) { func (pder *ProviderFiles) Read(sid string) (sess *SessionFile, err error) {
pder.lock.Lock() pder.lock.Lock()
defer pder.lock.Unlock() defer pder.lock.Unlock()
//ckf := ckdirpath(sid) if pder.Exists(sid) {
//os.Stat(sessfile)... TODO return &SessionFile{sid}, nil
return }
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 // SessionFile save session data into files using gob encode/dacode