переименование store => LocalStorage
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
var _ http.FileSystem = (*HttpFS)(nil)
|
||||
|
||||
type HttpFS struct {
|
||||
store *Store
|
||||
localStorage *LocalStorage
|
||||
remoteStorage remote.Storage
|
||||
}
|
||||
|
||||
@@ -25,12 +25,13 @@ func WithRemoteStorage(storage remote.Storage) HttpFSOption {
|
||||
|
||||
// NewHttpFS
|
||||
func NewHttpFS(dir string, opts ...HttpFSOption) (*HttpFS, error) {
|
||||
store, err := NewStore(dir)
|
||||
localStorage, err := NewLocalStorage(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f := &HttpFS{store: store}
|
||||
f := &HttpFS{}
|
||||
f.localStorage = localStorage
|
||||
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
@@ -49,10 +50,11 @@ func (f *HttpFS) Open(name string) (http.File, error) {
|
||||
return f.remoteStorage.Open(name)
|
||||
}
|
||||
|
||||
return f.store.Open(n)
|
||||
return f.localStorage.Open(n)
|
||||
}
|
||||
|
||||
// LocalStorage
|
||||
func (f *HttpFS) LocalStorage() *LocalStorage { return f.localStorage }
|
||||
|
||||
// RemoteStorage
|
||||
func (f *HttpFS) RemoteStorage() remote.Storage {
|
||||
return f.remoteStorage
|
||||
}
|
||||
func (f *HttpFS) RemoteStorage() remote.Storage { return f.remoteStorage }
|
||||
|
||||
@@ -65,7 +65,7 @@ func (s *MinioStorage) NewStorage(ctx context.Context, connString string) (remot
|
||||
|
||||
// normSeparators will normalize all "\\" and "/" to the provided separator
|
||||
func (s *MinioStorage) normSeparators(str string) string {
|
||||
return strings.Replace(strings.Replace(str, "\\", s.separator, -1), "/", s.separator, -1)
|
||||
return strings.ReplaceAll(strings.ReplaceAll(str, "\\", s.separator), "/", s.separator)
|
||||
}
|
||||
|
||||
// Create
|
||||
|
||||
+18
-19
@@ -17,8 +17,8 @@ import (
|
||||
// tmpfileName используется в качестве имени временного файла при генерации ошибок
|
||||
const tmpfileName = "<temporary file>"
|
||||
|
||||
// Store описывает хранилище файлов.
|
||||
type Store struct {
|
||||
// LocalStorage описывает хранилище файлов.
|
||||
type LocalStorage struct {
|
||||
dir string
|
||||
permissions os.FileMode
|
||||
|
||||
@@ -29,12 +29,12 @@ type Store struct {
|
||||
}
|
||||
}
|
||||
|
||||
type StoreOption func(*Store)
|
||||
type LocalStorageOption func(*LocalStorage)
|
||||
|
||||
// WithPermissions
|
||||
func WithPermissions(permissions os.FileMode) StoreOption {
|
||||
return func(store *Store) {
|
||||
store.permissions = permissions
|
||||
func WithPermissions(permissions os.FileMode) LocalStorageOption {
|
||||
return func(storage *LocalStorage) {
|
||||
storage.permissions = permissions
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +48,11 @@ type FileInfo struct {
|
||||
MD5 string `json:"md5"`
|
||||
}
|
||||
|
||||
// NewStore открывает и возвращает хранилище файлов.
|
||||
func NewStore(dir string, opts ...StoreOption) (*Store, error) {
|
||||
s := &Store{
|
||||
dir: dir,
|
||||
permissions: 0700,
|
||||
}
|
||||
// NewLocalStorage открывает и возвращает хранилище файлов.
|
||||
func NewLocalStorage(dir string, opts ...LocalStorageOption) (*LocalStorage, error) {
|
||||
s := &LocalStorage{}
|
||||
s.dir = dir
|
||||
s.permissions = 0700
|
||||
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
@@ -70,7 +69,7 @@ func NewStore(dir string, opts ...StoreOption) (*Store, error) {
|
||||
}
|
||||
|
||||
// Create сохраняет файл в хранилище. В качестве имени файла используется комбинация из двух хешей.
|
||||
func (s *Store) Create(r io.Reader) (*FileInfo, error) {
|
||||
func (s *LocalStorage) Create(r io.Reader) (*FileInfo, error) {
|
||||
// Создаём временный файл в корневом каталоге
|
||||
tmpfile, err := os.CreateTemp(s.dir, "~tmp")
|
||||
if err != nil {
|
||||
@@ -145,7 +144,7 @@ func (s *Store) Create(r io.Reader) (*FileInfo, error) {
|
||||
}
|
||||
|
||||
// Open открывает файл из каталога.
|
||||
func (s *Store) Open(name string) (*os.File, error) {
|
||||
func (s *LocalStorage) Open(name string) (*os.File, error) {
|
||||
// Полное имя для доступа к файлу
|
||||
fullName := s.GetFullName(name)
|
||||
if fullName == "" {
|
||||
@@ -181,7 +180,7 @@ func (s *Store) Open(name string) (*os.File, error) {
|
||||
}
|
||||
|
||||
// Remove удаляет файл из хранилища.
|
||||
func (s *Store) Remove(name string) error {
|
||||
func (s *LocalStorage) Remove(name string) error {
|
||||
mu := s.getMutex(name)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
@@ -209,7 +208,7 @@ func (s *Store) Remove(name string) error {
|
||||
}
|
||||
|
||||
// Clean удаляет старые файлы, к которым не обращались больше заданного времени.
|
||||
func (s *Store) Clean(lifetime time.Duration) error {
|
||||
func (s *LocalStorage) Clean(lifetime time.Duration) error {
|
||||
// Удаляем вообще все файлы, если время жизни не задано
|
||||
if lifetime <= 0 {
|
||||
files, err := filepath.Glob(filepath.Join(s.dir, "*"))
|
||||
@@ -262,7 +261,7 @@ func (s *Store) Clean(lifetime time.Duration) error {
|
||||
}
|
||||
|
||||
// getMutex
|
||||
func (s *Store) getMutex(name string) *sync.Mutex {
|
||||
func (s *LocalStorage) getMutex(name string) *sync.Mutex {
|
||||
s.mutexes.Do(func() { s.mutexes.m = make(map[string]*sync.Mutex) })
|
||||
|
||||
s.mutexes.Lock()
|
||||
@@ -277,7 +276,7 @@ func (s *Store) getMutex(name string) *sync.Mutex {
|
||||
}
|
||||
|
||||
// GetFullName возвращает полный путь к файлу в хранилище.
|
||||
func (s *Store) GetFullName(name string) string {
|
||||
func (s *LocalStorage) GetFullName(name string) string {
|
||||
if len(name) < 27 {
|
||||
return ""
|
||||
}
|
||||
@@ -285,7 +284,7 @@ func (s *Store) GetFullName(name string) string {
|
||||
}
|
||||
|
||||
// IsExists проверяет: существует ли файл в хранилище?
|
||||
func (s *Store) IsExists(name string) bool {
|
||||
func (s *LocalStorage) IsExists(name string) bool {
|
||||
fullName := s.GetFullName(name)
|
||||
if fullName == "" {
|
||||
return false
|
||||
Reference in New Issue
Block a user