use the request context

This commit is contained in:
MaysWind
2025-08-02 00:10:12 +08:00
parent 56a3905df1
commit cad53d0bfc
8 changed files with 59 additions and 54 deletions
+5 -4
View File
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/settings"
"github.com/mayswind/ezbookkeeping/pkg/utils"
)
@@ -28,17 +29,17 @@ func NewLocalFileSystemObjectStorage(config *settings.Config, pathPrefix string)
}
// Exists returns whether the file exists
func (s *LocalFileSystemObjectStorage) Exists(path string) (bool, error) {
func (s *LocalFileSystemObjectStorage) Exists(ctx core.Context, path string) (bool, error) {
return utils.IsExists(s.getFinalPath(path))
}
// Read returns the object instance according to specified the file path
func (s *LocalFileSystemObjectStorage) Read(path string) (ObjectInStorage, error) {
func (s *LocalFileSystemObjectStorage) Read(ctx core.Context, path string) (ObjectInStorage, error) {
return os.Open(s.getFinalPath(path))
}
// Save returns whether save the object instance successfully
func (s *LocalFileSystemObjectStorage) Save(path string, object ObjectInStorage) error {
func (s *LocalFileSystemObjectStorage) Save(ctx core.Context, path string, object ObjectInStorage) error {
finalPath := s.getFinalPath(path)
if err := os.MkdirAll(filepath.Dir(finalPath), os.ModePerm); err != nil {
@@ -59,7 +60,7 @@ func (s *LocalFileSystemObjectStorage) Save(path string, object ObjectInStorage)
}
// Delete returns whether delete the object according to specified the file path successfully
func (s *LocalFileSystemObjectStorage) Delete(path string) error {
func (s *LocalFileSystemObjectStorage) Delete(ctx core.Context, path string) error {
return os.Remove(s.getFinalPath(path))
}
+5 -8
View File
@@ -9,6 +9,7 @@ import (
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/settings"
)
@@ -64,8 +65,7 @@ func NewMinIOObjectStorage(config *settings.Config, pathPrefix string) (*MinIOOb
}
// Exists returns whether the file exists
func (s *MinIOObjectStorage) Exists(path string) (bool, error) {
ctx := context.Background()
func (s *MinIOObjectStorage) Exists(ctx core.Context, path string) (bool, error) {
objectInfo, err := s.minIOClient.StatObject(ctx, s.minIOConfig.Bucket, s.getFinalPath(path), minio.StatObjectOptions{})
if err == nil && !objectInfo.IsDeleteMarker {
@@ -76,22 +76,19 @@ func (s *MinIOObjectStorage) Exists(path string) (bool, error) {
}
// Read returns the object instance according to specified the file path
func (s *MinIOObjectStorage) Read(path string) (ObjectInStorage, error) {
ctx := context.Background()
func (s *MinIOObjectStorage) Read(ctx core.Context, path string) (ObjectInStorage, error) {
return s.minIOClient.GetObject(ctx, s.minIOConfig.Bucket, s.getFinalPath(path), minio.GetObjectOptions{})
}
// Save returns whether save the object instance successfully
func (s *MinIOObjectStorage) Save(path string, object ObjectInStorage) error {
ctx := context.Background()
func (s *MinIOObjectStorage) Save(ctx core.Context, path string, object ObjectInStorage) error {
_, err := s.minIOClient.PutObject(ctx, s.minIOConfig.Bucket, s.getFinalPath(path), object, -1, minio.PutObjectOptions{})
return err
}
// Delete returns whether delete the object according to specified the file path successfully
func (s *MinIOObjectStorage) Delete(path string) error {
ctx := context.Background()
func (s *MinIOObjectStorage) Delete(ctx core.Context, path string) error {
return s.minIOClient.RemoveObject(ctx, s.minIOConfig.Bucket, s.getFinalPath(path), minio.RemoveObjectOptions{})
}
+6 -4
View File
@@ -1,9 +1,11 @@
package storage
import "github.com/mayswind/ezbookkeeping/pkg/core"
// ObjectStorage represents an object storage to store file object
type ObjectStorage interface {
Exists(path string) (bool, error)
Read(path string) (ObjectInStorage, error)
Save(path string, object ObjectInStorage) error
Delete(path string) error
Exists(ctx core.Context, path string) (bool, error)
Read(ctx core.Context, path string) (ObjectInStorage, error)
Save(ctx core.Context, path string, object ObjectInStorage) error
Delete(ctx core.Context, path string) error
}
+17 -16
View File
@@ -1,6 +1,7 @@
package storage
import (
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/settings"
)
@@ -41,43 +42,43 @@ func InitializeStorageContainer(config *settings.Config) error {
}
// ExistsAvatar returns whether the avatar file exists from the current avatar object storage
func (s *StorageContainer) ExistsAvatar(path string) (bool, error) {
return s.AvatarCurrentStorage.Exists(path)
func (s *StorageContainer) ExistsAvatar(ctx core.Context, path string) (bool, error) {
return s.AvatarCurrentStorage.Exists(ctx, path)
}
// ReadAvatar returns the avatar file from the current avatar object storage
func (s *StorageContainer) ReadAvatar(path string) (ObjectInStorage, error) {
return s.AvatarCurrentStorage.Read(path)
func (s *StorageContainer) ReadAvatar(ctx core.Context, path string) (ObjectInStorage, error) {
return s.AvatarCurrentStorage.Read(ctx, path)
}
// SaveAvatar returns whether save the avatar file into the current avatar object storage successfully
func (s *StorageContainer) SaveAvatar(path string, object ObjectInStorage) error {
return s.AvatarCurrentStorage.Save(path, object)
func (s *StorageContainer) SaveAvatar(ctx core.Context, path string, object ObjectInStorage) error {
return s.AvatarCurrentStorage.Save(ctx, path, object)
}
// DeleteAvatar returns whether delete the avatar file from the current avatar object storage successfully
func (s *StorageContainer) DeleteAvatar(path string) error {
return s.AvatarCurrentStorage.Delete(path)
func (s *StorageContainer) DeleteAvatar(ctx core.Context, path string) error {
return s.AvatarCurrentStorage.Delete(ctx, path)
}
// ExistsTransactionPicture returns whether the transaction picture file exists from the current transaction picture object storage
func (s *StorageContainer) ExistsTransactionPicture(path string) (bool, error) {
return s.TransactionPictureCurrentStorage.Exists(path)
func (s *StorageContainer) ExistsTransactionPicture(ctx core.Context, path string) (bool, error) {
return s.TransactionPictureCurrentStorage.Exists(ctx, path)
}
// ReadTransactionPicture returns the transaction picture file from the current transaction picture object storage
func (s *StorageContainer) ReadTransactionPicture(path string) (ObjectInStorage, error) {
return s.TransactionPictureCurrentStorage.Read(path)
func (s *StorageContainer) ReadTransactionPicture(ctx core.Context, path string) (ObjectInStorage, error) {
return s.TransactionPictureCurrentStorage.Read(ctx, path)
}
// SaveTransactionPicture returns whether save the transaction picture file into the current transaction picture object storage successfully
func (s *StorageContainer) SaveTransactionPicture(path string, object ObjectInStorage) error {
return s.TransactionPictureCurrentStorage.Save(path, object)
func (s *StorageContainer) SaveTransactionPicture(ctx core.Context, path string, object ObjectInStorage) error {
return s.TransactionPictureCurrentStorage.Save(ctx, path, object)
}
// DeleteTransactionPicture returns whether delete the transaction picture file from the current transaction picture object storage successfully
func (s *StorageContainer) DeleteTransactionPicture(path string) error {
return s.TransactionPictureCurrentStorage.Delete(path)
func (s *StorageContainer) DeleteTransactionPicture(ctx core.Context, path string) error {
return s.TransactionPictureCurrentStorage.Delete(ctx, path)
}
func newObjectStorage(config *settings.Config, pathPrefix string) (ObjectStorage, error) {