From 0ac4d318b735eef4f65efb0382c0862287c65dff Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 17 Feb 2024 04:41:12 -0500 Subject: [PATCH] refactor: use a lazy setter approach for storageProtocol --- protocols/s5/s5.go | 20 ++++++++++---------- protocols/s5/tus.go | 34 ++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/protocols/s5/s5.go b/protocols/s5/s5.go index bc5b51d..6710fe2 100644 --- a/protocols/s5/s5.go +++ b/protocols/s5/s5.go @@ -51,11 +51,10 @@ type S5ProtocolParams struct { type S5ProtocolResult struct { fx.Out - Protocol registry.Protocol `group:"protocol"` - S5Protocol *S5Protocol - S5NodeConfig *s5config.NodeConfig - Db *bolt.DB - StorageProtocol storage.StorageProtocol + Protocol registry.Protocol `group:"protocol"` + S5Protocol *S5Protocol + S5NodeConfig *s5config.NodeConfig + Db *bolt.DB } type S5ProviderStoreParams struct { @@ -93,11 +92,10 @@ func NewS5Protocol( } return S5ProtocolResult{ - Protocol: proto, - S5Protocol: proto, - S5NodeConfig: cfg, - Db: cfg.DB, - StorageProtocol: GetStorageProtocol(proto), + Protocol: proto, + S5Protocol: proto, + S5NodeConfig: cfg, + Db: cfg.DB, }, nil } @@ -182,6 +180,8 @@ func (s *S5Protocol) Init(args ...any) error { return err } + s.tusHandler.SetStorageProtocol(GetStorageProtocol(s)) + err = s.tusHandler.Init() if err != nil { return err diff --git a/protocols/s5/tus.go b/protocols/s5/tus.go index 6aaeffc..1cdf3b4 100644 --- a/protocols/s5/tus.go +++ b/protocols/s5/tus.go @@ -58,26 +58,24 @@ type TusHandler struct { type TusHandlerParams struct { fx.In - Config *viper.Viper - Logger *zap.Logger - Db *gorm.DB - Cron *cron.CronServiceDefault - Storage storage.StorageService - Accounts *account.AccountServiceDefault - Metadata metadata.MetadataService - StorageProtocol storage.StorageProtocol + Config *viper.Viper + Logger *zap.Logger + Db *gorm.DB + Cron *cron.CronServiceDefault + Storage storage.StorageService + Accounts *account.AccountServiceDefault + Metadata metadata.MetadataService } func NewTusHandler(params TusHandlerParams) *TusHandler { return &TusHandler{ - config: params.Config, - db: params.Db, - logger: params.Logger, - cron: params.Cron, - storage: params.Storage, - accounts: params.Accounts, - metadata: params.Metadata, - storageProtocol: params.StorageProtocol, + config: params.Config, + db: params.Db, + logger: params.Logger, + cron: params.Cron, + storage: params.Storage, + accounts: params.Accounts, + metadata: params.Metadata, } } @@ -316,6 +314,10 @@ func (t *TusHandler) GetUploadReader(ctx context.Context, hash []byte, start int return reader, nil } +func (t *TusHandler) SetStorageProtocol(storageProtocol storage.StorageProtocol) { + t.storageProtocol = storageProtocol +} + func (t *TusHandler) uploadTask(ctx context.Context, upload *models.TusUpload) error { tusUpload, err := t.tusStore.GetUpload(ctx, upload.UploadID) if err != nil {