From ffb1406fb8dbe11985c0a14a684e5d3ccd41685f Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 18 Oct 2025 15:30:59 +0300 Subject: [PATCH] replace domain for links --- api.env | 3 ++- cmd/main.go | 2 ++ config/config.go | 2 ++ internal/mediaStorage/handler.go | 3 ++- internal/mediaStorage/service.go | 23 +++++++++++++++++++---- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/api.env b/api.env index add3486..563fd50 100644 --- a/api.env +++ b/api.env @@ -9,9 +9,10 @@ GRPC_SERVER_PORT=9050 GRPC_CLIENT_PORT=9060 MEDIA_STORAGE_USER= -MEDIA_STORAGE_PASS= +MEDIA_STORAGE_PASSWORD= MEDIA_STORAGE_HOST= MEDIA_STORAGE_PORT= +MEDIA_STORAGE_DOMAIN= MEDIA_STORAGE_SECURE=false DB_HOST= diff --git a/cmd/main.go b/cmd/main.go index 8a2e607..d5e74cc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -56,6 +56,8 @@ func main() { Port: c.MediaConf.Port, User: c.MediaConf.User, Password: c.MediaConf.Password, + Domain: c.MediaConf.Domain, + Secure: c.MediaConf.Secure, }) log.WithFields(log.Fields{ "address": c.MediaConf.Host + ":" + c.MediaConf.Port, diff --git a/config/config.go b/config/config.go index 9ec076a..0e71b33 100644 --- a/config/config.go +++ b/config/config.go @@ -46,6 +46,7 @@ type MediaConfig struct { Port string User string Password string + Domain string Secure string } @@ -87,6 +88,7 @@ func NewConfig() *Config { Port: getEnv("MEDIA_STORAGE_PORT", ""), User: getEnv("MEDIA_STORAGE_USER", ""), Password: getEnv("MEDIA_STORAGE_PASSWORD", ""), + Domain: getEnv("MEDIA_STORAGE_DOMAIN", ""), Secure: getEnv("MEDIA_STORAGE_SECURE", ""), }, } diff --git a/internal/mediaStorage/handler.go b/internal/mediaStorage/handler.go index ab1331e..e6293ba 100644 --- a/internal/mediaStorage/handler.go +++ b/internal/mediaStorage/handler.go @@ -16,6 +16,7 @@ type Deps struct { Port string User string Password string + Domain string Secure string } @@ -40,6 +41,6 @@ func NewHandler(deps Deps) *Handler { }).Debug("Media storage | Created minio client") return &Handler{ - newService(minioClient), + newService(minioClient, deps.Domain, endpoint), } } diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index 3a5b7aa..a55b6fc 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -2,20 +2,25 @@ package mediaStorage import ( "context" + "fmt" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" "io" "net/url" + "strings" "time" ) type Service struct { - client *minio.Client + client *minio.Client + domain string + endpoint string } -func newService(client *minio.Client) *Service { +func newService(client *minio.Client, domain, endpoint string) *Service { return &Service{ client: client, + domain: domain, } } @@ -35,8 +40,18 @@ func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.R return err } -func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) { - return s.client.PresignedGetObject(ctx, bucket, object, expires, params) +func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) { + presigned, err := s.client.PresignedGetObject(ctx, bucket, object, expires, params) + if err != nil { + return "", err + } + + link := presigned.String() + if s.domain != "" { + link = strings.Replace(link, fmt.Sprintf("http://%s", s.endpoint), s.domain, 1) + } + + return link, nil } func (s *Service) Delete(ctx context.Context, bucket, object string) error {