change requests applied

This commit is contained in:
Berkant Ay
2023-06-07 08:34:25 +03:00
parent 8225ee4fc6
commit bae367bf31
2 changed files with 18 additions and 23 deletions

View File

@@ -1,21 +0,0 @@
package internal
import (
"os"
)
func IsValid(fp string) bool {
// Check if file already exists
if _, err := os.Stat(fp); err == nil {
return true
}
// Attempt to create it
var d []byte
if err := os.WriteFile(fp, d, 0644); err == nil {
os.Remove(fp) // And delete it
return true
}
return false
}

View File

@@ -4,10 +4,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"log" "log"
"os"
"time" "time"
"github.com/cockroachdb/pebble" "github.com/cockroachdb/pebble"
"github.com/gofiber/storage/pebble/internal"
) )
type Storage struct { type Storage struct {
@@ -24,7 +24,7 @@ type CacheType struct {
func New(config ...Config) *Storage { func New(config ...Config) *Storage {
cfg := configDefault(config...) cfg := configDefault(config...)
if !internal.IsValid(cfg.Path) { if !isValid(cfg.Path) {
panic(errors.New("invalid filepath")) panic(errors.New("invalid filepath"))
} }
@@ -117,3 +117,19 @@ func (s *Storage) Close() error {
func (s *Storage) Conn() *pebble.DB { func (s *Storage) Conn() *pebble.DB {
return s.db return s.db
} }
func isValid(fp string) bool {
// Check if file already exists
if _, err := os.Stat(fp); err == nil {
return true
}
// Attempt to create it
var d []byte
if err := os.WriteFile(fp, d, 0644); err == nil {
os.Remove(fp) // And delete it
return true
}
return false
}