From 5676ad927d2762b7c5ef636745fc06e8e314e0c2 Mon Sep 17 00:00:00 2001 From: "S.Solodyagin" Date: Thu, 30 Oct 2025 10:55:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BE=D1=81=D0=BC?= =?UTF-8?q?=D1=8B=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- storage.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/storage.go b/storage.go index d76fb37..77844a8 100644 --- a/storage.go +++ b/storage.go @@ -41,12 +41,13 @@ func WithPermissions(perm os.FileMode) LocalStorageOption { // FileInfo описывает информацию о сохраненном файле. type FileInfo struct { - Location string `json:"location"` - Name string `json:"name"` - Mimetype string `json:"mimetype"` - Size int64 `json:"size"` - CRC32 uint32 `json:"crc32"` - MD5 string `json:"md5"` + Location string // @deprecated + Path string + Name string + Mimetype string + Size int64 + CRC32 uint32 + MD5 string } // NewLocalStorage открывает и возвращает хранилище файлов. @@ -102,7 +103,8 @@ func (s *LocalStorage) Create(r io.Reader) (*FileInfo, error) { sumMD5 := hashMD5.Sum(nil) name := base32.StdEncoding.EncodeToString(append(hashCRC32.Sum(nil), sumMD5...)) fi := &FileInfo{ - Location: s.GetFullName(name), + Location: s.GetPath(name), + Path: s.GetPath(name), Name: name, Mimetype: mimetype, Size: size, @@ -120,18 +122,18 @@ func (s *LocalStorage) Create(r io.Reader) (*FileInfo, error) { // Если файл уже существует, то просто обновляем его время создания now := time.Now() - if err := os.Chtimes(fi.Location, now, now); err == nil { + if err := os.Chtimes(fi.Path, now, now); err == nil { return fi, nil // Возвращаем информацию о файле, временный файл будет автоматически удален } // Если такого файла нет, то создаем для него каталог - if err := os.MkdirAll(filepath.Dir(fi.Location), s.perm); err != nil { + if err := os.MkdirAll(filepath.Dir(fi.Path), s.perm); err != nil { err.(*os.PathError).Path = fi.Name return nil, err } // Перемещаем временный файл в этот каталог - if err := os.Rename(tmpfile.Name(), fi.Location); err != nil { + if err := os.Rename(tmpfile.Name(), fi.Path); err != nil { if _, ok := err.(*os.PathError); ok { err.(*os.PathError).Path = fi.Name } @@ -145,7 +147,7 @@ func (s *LocalStorage) Create(r io.Reader) (*FileInfo, error) { // Open открывает файл из каталога. func (s *LocalStorage) Open(name string) (*os.File, error) { // Полное имя для доступа к файлу - fullName := s.GetFullName(name) + fullName := s.GetPath(name) if fullName == "" { return nil, os.ErrNotExist } @@ -185,7 +187,7 @@ func (s *LocalStorage) Remove(name string) error { defer mu.Unlock() // Полное имя для доступа к файлу - fullName := s.GetFullName(name) + fullName := s.GetPath(name) if fullName == "" { return os.ErrNotExist } @@ -275,7 +277,12 @@ func (s *LocalStorage) getMutex(name string) *sync.Mutex { } // GetFullName возвращает полный путь к файлу в хранилище. -func (s *LocalStorage) GetFullName(name string) string { +// +// Deprecated: GetFullName is no longer recommended. Use GetPath instead. +func (s *LocalStorage) GetFullName(name string) string { return s.GetPath(name) } + +// GetPath возвращает полный путь к файлу в хранилище. +func (s *LocalStorage) GetPath(name string) string { name = strings.TrimPrefix(name, "/") if len(name) < 27 { return "" @@ -285,7 +292,7 @@ func (s *LocalStorage) GetFullName(name string) string { // IsExists проверяет: существует ли файл в хранилище? func (s *LocalStorage) IsExists(name string) bool { - fullName := s.GetFullName(name) + fullName := s.GetPath(name) if fullName == "" { return false }