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"
"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