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