don't initialize avatar / transaction storage when they are not enabled

This commit is contained in:
MaysWind
2025-08-02 10:16:00 +08:00
parent 29a87dcfaf
commit 975a56e7d9
+55 -19
View File
@@ -11,8 +11,8 @@ const transactionPicturePathPrefix = "transaction"
// StorageContainer contains the current object storage // StorageContainer contains the current object storage
type StorageContainer struct { type StorageContainer struct {
AvatarCurrentStorage ObjectStorage avatarCurrentStorage ObjectStorage
TransactionPictureCurrentStorage ObjectStorage transactionPictureCurrentStorage ObjectStorage
} }
// Initialize a object storage container singleton instance // Initialize a object storage container singleton instance
@@ -22,63 +22,99 @@ var (
// InitializeStorageContainer initializes the current object storage according to the config // InitializeStorageContainer initializes the current object storage according to the config
func InitializeStorageContainer(config *settings.Config) error { func InitializeStorageContainer(config *settings.Config) error {
avatarStorage, err := newObjectStorage(config, avatarPathPrefix) if config.AvatarProvider == core.USER_AVATAR_PROVIDER_INTERNAL {
avatarStorage, err := newObjectStorage(config, avatarPathPrefix)
if err != nil { if err != nil {
return err return err
}
Container.avatarCurrentStorage = avatarStorage
} }
Container.AvatarCurrentStorage = avatarStorage if config.EnableTransactionPictures {
transactionPictureStorage, err := newObjectStorage(config, transactionPicturePathPrefix)
transactionPictureStorage, err := newObjectStorage(config, transactionPicturePathPrefix) if err != nil {
return err
}
if err != nil { Container.transactionPictureCurrentStorage = transactionPictureStorage
return err
} }
Container.TransactionPictureCurrentStorage = transactionPictureStorage
return nil return nil
} }
// ExistsAvatar returns whether the avatar file exists from the current avatar object storage // ExistsAvatar returns whether the avatar file exists from the current avatar object storage
func (s *StorageContainer) ExistsAvatar(ctx core.Context, path string) (bool, error) { func (s *StorageContainer) ExistsAvatar(ctx core.Context, path string) (bool, error) {
return s.AvatarCurrentStorage.Exists(ctx, path) if s.avatarCurrentStorage == nil {
return false, errs.ErrSystemError
}
return s.avatarCurrentStorage.Exists(ctx, path)
} }
// ReadAvatar returns the avatar file from the current avatar object storage // ReadAvatar returns the avatar file from the current avatar object storage
func (s *StorageContainer) ReadAvatar(ctx core.Context, path string) (ObjectInStorage, error) { func (s *StorageContainer) ReadAvatar(ctx core.Context, path string) (ObjectInStorage, error) {
return s.AvatarCurrentStorage.Read(ctx, path) if s.avatarCurrentStorage == nil {
return nil, errs.ErrSystemError
}
return s.avatarCurrentStorage.Read(ctx, path)
} }
// SaveAvatar returns whether save the avatar file into the current avatar object storage successfully // SaveAvatar returns whether save the avatar file into the current avatar object storage successfully
func (s *StorageContainer) SaveAvatar(ctx core.Context, path string, object ObjectInStorage) error { func (s *StorageContainer) SaveAvatar(ctx core.Context, path string, object ObjectInStorage) error {
return s.AvatarCurrentStorage.Save(ctx, path, object) if s.avatarCurrentStorage == nil {
return errs.ErrSystemError
}
return s.avatarCurrentStorage.Save(ctx, path, object)
} }
// DeleteAvatar returns whether delete the avatar file from the current avatar object storage successfully // DeleteAvatar returns whether delete the avatar file from the current avatar object storage successfully
func (s *StorageContainer) DeleteAvatar(ctx core.Context, path string) error { func (s *StorageContainer) DeleteAvatar(ctx core.Context, path string) error {
return s.AvatarCurrentStorage.Delete(ctx, path) if s.avatarCurrentStorage == nil {
return errs.ErrSystemError
}
return s.avatarCurrentStorage.Delete(ctx, path)
} }
// ExistsTransactionPicture returns whether the transaction picture file exists from the current transaction picture object storage // ExistsTransactionPicture returns whether the transaction picture file exists from the current transaction picture object storage
func (s *StorageContainer) ExistsTransactionPicture(ctx core.Context, path string) (bool, error) { func (s *StorageContainer) ExistsTransactionPicture(ctx core.Context, path string) (bool, error) {
return s.TransactionPictureCurrentStorage.Exists(ctx, path) if s.transactionPictureCurrentStorage == nil {
return false, errs.ErrSystemError
}
return s.transactionPictureCurrentStorage.Exists(ctx, path)
} }
// ReadTransactionPicture returns the transaction picture file from the current transaction picture object storage // ReadTransactionPicture returns the transaction picture file from the current transaction picture object storage
func (s *StorageContainer) ReadTransactionPicture(ctx core.Context, path string) (ObjectInStorage, error) { func (s *StorageContainer) ReadTransactionPicture(ctx core.Context, path string) (ObjectInStorage, error) {
return s.TransactionPictureCurrentStorage.Read(ctx, path) if s.transactionPictureCurrentStorage == nil {
return nil, errs.ErrSystemError
}
return s.transactionPictureCurrentStorage.Read(ctx, path)
} }
// SaveTransactionPicture returns whether save the transaction picture file into the current transaction picture object storage successfully // SaveTransactionPicture returns whether save the transaction picture file into the current transaction picture object storage successfully
func (s *StorageContainer) SaveTransactionPicture(ctx core.Context, path string, object ObjectInStorage) error { func (s *StorageContainer) SaveTransactionPicture(ctx core.Context, path string, object ObjectInStorage) error {
return s.TransactionPictureCurrentStorage.Save(ctx, path, object) if s.transactionPictureCurrentStorage == nil {
return errs.ErrSystemError
}
return s.transactionPictureCurrentStorage.Save(ctx, path, object)
} }
// DeleteTransactionPicture returns whether delete the transaction picture file from the current transaction picture object storage successfully // DeleteTransactionPicture returns whether delete the transaction picture file from the current transaction picture object storage successfully
func (s *StorageContainer) DeleteTransactionPicture(ctx core.Context, path string) error { func (s *StorageContainer) DeleteTransactionPicture(ctx core.Context, path string) error {
return s.TransactionPictureCurrentStorage.Delete(ctx, path) if s.transactionPictureCurrentStorage == nil {
return errs.ErrSystemError
}
return s.transactionPictureCurrentStorage.Delete(ctx, path)
} }
func newObjectStorage(config *settings.Config, pathPrefix string) (ObjectStorage, error) { func newObjectStorage(config *settings.Config, pathPrefix string) (ObjectStorage, error) {