Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

refactor: address PR review feedback - rename AttachmentStorage to AttachmentRepository

Acho Arnoldb79a3e03d23a
diff --git a/api/go.mod b/api/go.mod
index cd9e6801..c7073253 100644
--- a/api/go.mod
+++ b/api/go.mod
@@ -4,6 +4,7 @@ go 1.25.0
require (
cloud.google.com/go/cloudtasks v1.14.0
+ cloud.google.com/go/storage v1.62.0
firebase.google.com/go v3.13.0+incompatible
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.55.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.31.0
@@ -50,6 +51,7 @@ require (
go.opentelemetry.io/otel/sdk v1.43.0
go.opentelemetry.io/otel/sdk/metric v1.43.0
go.opentelemetry.io/otel/trace v1.43.0
+ golang.org/x/sync v0.20.0
google.golang.org/api v0.274.0
google.golang.org/protobuf v1.36.11
gorm.io/driver/postgres v1.6.0
@@ -80,7 +82,6 @@ require (
cloud.google.com/go/iam v1.7.0 // indirect
cloud.google.com/go/longrunning v0.9.0 // indirect
cloud.google.com/go/monitoring v1.25.0 // indirect
- cloud.google.com/go/storage v1.62.0 // indirect
cloud.google.com/go/trace v1.12.0 // indirect
dario.cat/mergo v1.0.2 // indirect
filippo.io/edwards25519 v1.2.0 // indirect
@@ -190,7 +191,6 @@ require (
golang.org/x/mod v0.34.0 // indirect
golang.org/x/net v0.52.0 // indirect
golang.org/x/oauth2 v0.36.0 // indirect
- golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.35.0 // indirect
golang.org/x/time v0.15.0 // indirect
diff --git a/api/go.sum b/api/go.sum
index 3f908ec5..9156143b 100644
--- a/api/go.sum
+++ b/api/go.sum
@@ -22,8 +22,6 @@ cloud.google.com/go/longrunning v0.9.0 h1:0EzbDEGsAvOZNbqXopgniY0w0a1phvu5IdUFq8
cloud.google.com/go/longrunning v0.9.0/go.mod h1:pkTz846W7bF4o2SzdWJ40Hu0Re+UoNT6Q5t+igIcb8E=
cloud.google.com/go/monitoring v1.25.0 h1:HnsTIOxTN6BCSkt1P/Im23r1m7MHTTpmSYCzPkW7NK4=
cloud.google.com/go/monitoring v1.25.0/go.mod h1:wlj6rX+JGyusw/8+2duW4cJ6kmDHGmde3zMTJuG3Jpc=
-cloud.google.com/go/storage v1.61.3 h1:VS//ZfBuPGDvakfD9xyPW1RGF1Vy3BWUoVZXgW1KMOg=
-cloud.google.com/go/storage v1.61.3/go.mod h1:JtqK8BBB7TWv0HVGHubtUdzYYrakOQIsMLffZ2Z/HWk=
cloud.google.com/go/storage v1.62.0 h1:w2pQJhpUqVerMON45vatE2FpCYsNTf7OHjkn6ux5mMU=
cloud.google.com/go/storage v1.62.0/go.mod h1:T5hz3qzcpnxZ5LdKc7y8Tw7lh4v9zeeVyrD/cLJAzZU=
cloud.google.com/go/trace v1.12.0 h1:XvWHYfr9q88cX4pZyou6qCcSagnuASyUq2ej1dB6NzQ=
@@ -377,9 +375,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 h1:88Y4s2C8oTui1LGM6bT
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0/go.mod h1:Vl1/iaggsuRlrHf/hfPJPvVag77kKyvrLeD10kpMl+A=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0 h1:3iZJKlCZufyRzPzlQhUIWVmfltrXuGyfjREgGP3UUjc=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0/go.mod h1:/G+nUPfhq2e+qiXMGxMwumDrP5jtzU+mWN7/sjT2rak=
-go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.40.0 h1:ZrPRak/kS4xI3AVXy8F7pipuDXmDsrO8Lg+yQjBLjw0=
-go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.40.0/go.mod h1:3y6kQCWztq6hyW8Z9YxQDDm0Je9AJoFar2G0yDcmhRk=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.42.0 h1:lSZHgNHfbmQTPfuTmWVkEu8J8qXaQwuV30pjCcAUvP8=
+go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.42.0/go.mod h1:so9ounLcuoRDu033MW/E0AD4hhUjVqswrMF5FoZlBcw=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.43.0 h1:mS47AX77OtFfKG4vtp+84kuGSFZHTyxtXIN269vChY0=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.43.0/go.mod h1:PJnsC41lAGncJlPUniSwM81gc80GkgWJWr3cu2nKEtU=
go.opentelemetry.io/otel/log v0.19.0 h1:KUZs/GOsw79TBBMfDWsXS+KZ4g2Ckzksd1ymzsIEbo4=
diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go
index 86fa9347..de945d6c 100644
--- a/api/pkg/di/container.go
+++ b/api/pkg/di/container.go
@@ -88,7 +88,7 @@ type Container struct {
app *fiber.App
eventDispatcher *services.EventDispatcher
logger telemetry.Logger
- attachmentStorage repositories.AttachmentStorage
+ attachmentRepository repositories.AttachmentRepository
}
// NewLiteContainer creates a Container without any routes or listeners
@@ -1433,39 +1433,39 @@ func (container *Container) MessageService() (service *services.MessageService)
container.MessageRepository(),
container.EventDispatcher(),
container.PhoneService(),
- container.AttachmentStorage(),
+ container.AttachmentRepository(),
container.APIBaseURL(),
)
}
-// AttachmentStorage creates a cached AttachmentStorage based on configuration
-func (container *Container) AttachmentStorage() repositories.AttachmentStorage {
- if container.attachmentStorage != nil {
- return container.attachmentStorage
+// AttachmentRepository creates a cached AttachmentRepository based on configuration
+func (container *Container) AttachmentRepository() repositories.AttachmentRepository {
+ if container.attachmentRepository != nil {
+ return container.attachmentRepository
}
bucket := os.Getenv("GCS_BUCKET_NAME")
if bucket != "" {
- container.logger.Debug("creating GCSAttachmentStorage")
+ container.logger.Debug("creating GoogleCloudStorageAttachmentRepository")
client, err := storage.NewClient(context.Background())
if err != nil {
container.logger.Fatal(stacktrace.Propagate(err, "cannot create GCS client"))
}
- container.attachmentStorage = repositories.NewGCSAttachmentStorage(
+ container.attachmentRepository = repositories.NewGoogleCloudStorageAttachmentRepository(
container.Logger(),
container.Tracer(),
client,
bucket,
)
} else {
- container.logger.Debug("creating MemoryAttachmentStorage (GCS_BUCKET_NAME not set)")
- container.attachmentStorage = repositories.NewMemoryAttachmentStorage(
+ container.logger.Debug("creating MemoryAttachmentRepository (GCS_BUCKET_NAME not set)")
+ container.attachmentRepository = repositories.NewMemoryAttachmentRepository(
container.Logger(),
container.Tracer(),
)
}
- return container.attachmentStorage
+ return container.attachmentRepository
}
// APIBaseURL returns the API base URL derived from EVENTS_QUEUE_ENDPOINT
@@ -1480,7 +1480,7 @@ func (container *Container) AttachmentHandler() (handler *handlers.AttachmentHan
return handlers.NewAttachmentHandler(
container.Logger(),
container.Tracer(),
- container.AttachmentStorage(),
+ container.AttachmentRepository(),
)
}
diff --git a/api/pkg/handlers/attachment_handler.go b/api/pkg/handlers/attachment_handler.go
index 75406f34..e6ea4f85 100644
--- a/api/pkg/handlers/attachment_handler.go
+++ b/api/pkg/handlers/attachment_handler.go
@@ -1,7 +1,6 @@
package handlers
import (
- "errors"
"fmt"
"path/filepath"
@@ -16,14 +15,14 @@ type AttachmentHandler struct {
handler
logger telemetry.Logger
tracer telemetry.Tracer
- storage repositories.AttachmentStorage
+ storage repositories.AttachmentRepository
}
// NewAttachmentHandler creates a new AttachmentHandler
func NewAttachmentHandler(
logger telemetry.Logger,
tracer telemetry.Tracer,
- storage repositories.AttachmentStorage,
+ storage repositories.AttachmentRepository,
) (h *AttachmentHandler) {
return &AttachmentHandler{
logger: logger.WithService(fmt.Sprintf("%T", h)),
@@ -49,7 +48,7 @@ func (h *AttachmentHandler) RegisterRoutes(router fiber.Router) {
// @Success 200 {file} binary
// @Failure 404 {object} responses.NotFoundResponse
// @Failure 500 {object} responses.InternalServerError
-// @Router /attachments/{userID}/{messageID}/{attachmentIndex}/{filename} [get]
+// @Router /v1/attachments/{userID}/{messageID}/{attachmentIndex}/{filename} [get]
func (h *AttachmentHandler) GetAttachment(c *fiber.Ctx) error {
ctx, span := h.tracer.StartFromFiberCtx(c)
defer span.End()
@@ -69,7 +68,7 @@ func (h *AttachmentHandler) GetAttachment(c *fiber.Ctx) error {
if err != nil {
msg := fmt.Sprintf("cannot download attachment from path [%s]", path)
ctxLogger.Warn(stacktrace.Propagate(err, msg))
- if errors.Is(err, repositories.ErrAttachmentNotFound) {
+ if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
return h.responseNotFound(c, "attachment not found")
}
return h.responseInternalServerError(c)
diff --git a/api/pkg/repositories/attachment_storage.go b/api/pkg/repositories/attachment_repository.go
similarity index 87%
rename from api/pkg/repositories/attachment_storage.go
rename to api/pkg/repositories/attachment_repository.go
index b7b7bf4a..cbd77c12 100644
--- a/api/pkg/repositories/attachment_storage.go
+++ b/api/pkg/repositories/attachment_repository.go
@@ -7,10 +7,10 @@ import (
"strings"
)
-// AttachmentStorage is the interface for storing and retrieving message attachments
-type AttachmentStorage interface {
- // Upload stores attachment data at the given path
- Upload(ctx context.Context, path string, data []byte) error
+// AttachmentRepository is the interface for storing and retrieving message attachments
+type AttachmentRepository interface {
+ // Upload stores attachment data at the given path with the specified content type
+ Upload(ctx context.Context, path string, data []byte, contentType string) error
// Download retrieves attachment data from the given path
Download(ctx context.Context, path string) ([]byte, error)
// Delete removes an attachment at the given path
@@ -77,9 +77,6 @@ func ContentTypeFromExtension(ext string) string {
return "application/octet-stream"
}
-// ErrAttachmentNotFound is returned when an attachment is not found in storage
-var ErrAttachmentNotFound = fmt.Errorf("attachment not found")
-
// SanitizeFilename removes path separators and traversal sequences from a filename.
// Returns "attachment-{index}" if the sanitized name is empty.
func SanitizeFilename(name string, index int) string {
diff --git a/api/pkg/repositories/attachment_storage_test.go b/api/pkg/repositories/attachment_repository_test.go
similarity index 100%
rename from api/pkg/repositories/attachment_storage_test.go
rename to api/pkg/repositories/attachment_repository_test.go
diff --git a/api/pkg/repositories/gcs_attachment_storage.go b/api/pkg/repositories/gcs_attachment_repository.go
similarity index 51%
rename from api/pkg/repositories/gcs_attachment_storage.go
rename to api/pkg/repositories/gcs_attachment_repository.go
index 770f7aa9..d1e0eb92 100644
--- a/api/pkg/repositories/gcs_attachment_storage.go
+++ b/api/pkg/repositories/gcs_attachment_repository.go
@@ -2,6 +2,7 @@ package repositories
import (
"context"
+ "errors"
"fmt"
"io"
@@ -10,23 +11,23 @@ import (
"github.com/palantir/stacktrace"
)
-// GCSAttachmentStorage stores attachments in Google Cloud Storage
-type GCSAttachmentStorage struct {
+// GoogleCloudStorageAttachmentRepository stores attachments in Google Cloud Storage
+type GoogleCloudStorageAttachmentRepository struct {
logger telemetry.Logger
tracer telemetry.Tracer
client *storage.Client
bucket string
}
-// NewGCSAttachmentStorage creates a new GCSAttachmentStorage
-func NewGCSAttachmentStorage(
+// NewGoogleCloudStorageAttachmentRepository creates a new GoogleCloudStorageAttachmentRepository
+func NewGoogleCloudStorageAttachmentRepository(
logger telemetry.Logger,
tracer telemetry.Tracer,
client *storage.Client,
bucket string,
-) *GCSAttachmentStorage {
- return &GCSAttachmentStorage{
- logger: logger.WithService(fmt.Sprintf("%T", &GCSAttachmentStorage{})),
+) *GoogleCloudStorageAttachmentRepository {
+ return &GoogleCloudStorageAttachmentRepository{
+ logger: logger.WithService(fmt.Sprintf("%T", &GoogleCloudStorageAttachmentRepository{})),
tracer: tracer,
client: client,
bucket: bucket,
@@ -34,11 +35,13 @@ func NewGCSAttachmentStorage(
}
// Upload stores attachment data at the given path in GCS
-func (s *GCSAttachmentStorage) Upload(ctx context.Context, path string, data []byte) error {
- ctx, span := s.tracer.Start(ctx)
+func (s *GoogleCloudStorageAttachmentRepository) Upload(ctx context.Context, path string, data []byte, contentType string) error {
+ ctx, span, ctxLogger := s.tracer.StartWithLogger(ctx, s.logger)
defer span.End()
writer := s.client.Bucket(s.bucket).Object(path).NewWriter(ctx)
+ writer.ContentType = contentType
+
if _, err := writer.Write(data); err != nil {
return s.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot write attachment to GCS path [%s]", path)))
}
@@ -47,18 +50,22 @@ func (s *GCSAttachmentStorage) Upload(ctx context.Context, path string, data []b
return s.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot close GCS writer for path [%s]", path)))
}
- s.logger.Info(fmt.Sprintf("uploaded attachment to GCS path [%s/%s] with size [%d]", s.bucket, path, len(data)))
+ ctxLogger.Info(fmt.Sprintf("uploaded attachment to GCS path [%s/%s] with size [%d]", s.bucket, path, len(data)))
return nil
}
// Download retrieves attachment data from the given path in GCS
-func (s *GCSAttachmentStorage) Download(ctx context.Context, path string) ([]byte, error) {
- ctx, span := s.tracer.Start(ctx)
+func (s *GoogleCloudStorageAttachmentRepository) Download(ctx context.Context, path string) ([]byte, error) {
+ ctx, span, ctxLogger := s.tracer.StartWithLogger(ctx, s.logger)
defer span.End()
reader, err := s.client.Bucket(s.bucket).Object(path).NewReader(ctx)
if err != nil {
- return nil, s.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot open GCS reader for path [%s]", path)))
+ msg := fmt.Sprintf("cannot open GCS reader for path [%s]", path)
+ if errors.Is(err, storage.ErrObjectNotExist) {
+ return nil, s.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg))
+ }
+ return nil, s.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
defer reader.Close()
@@ -67,18 +74,19 @@ func (s *GCSAttachmentStorage) Download(ctx context.Context, path string) ([]byt
return nil, s.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot read attachment from GCS path [%s]", path)))
}
+ ctxLogger.Info(fmt.Sprintf("downloaded attachment from GCS path [%s/%s] with size [%d]", s.bucket, path, len(data)))
return data, nil
}
// Delete removes an attachment at the given path in GCS
-func (s *GCSAttachmentStorage) Delete(ctx context.Context, path string) error {
- ctx, span := s.tracer.Start(ctx)
+func (s *GoogleCloudStorageAttachmentRepository) Delete(ctx context.Context, path string) error {
+ ctx, span, ctxLogger := s.tracer.StartWithLogger(ctx, s.logger)
defer span.End()
if err := s.client.Bucket(s.bucket).Object(path).Delete(ctx); err != nil {
return s.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot delete GCS object at path [%s]", path)))
}
- s.logger.Info(fmt.Sprintf("deleted attachment from GCS path [%s/%s]", s.bucket, path))
+ ctxLogger.Info(fmt.Sprintf("deleted attachment from GCS path [%s/%s]", s.bucket, path))
return nil
}
diff --git a/api/pkg/repositories/memory_attachment_repository.go b/api/pkg/repositories/memory_attachment_repository.go
new file mode 100644
index 00000000..65eadf2f
--- /dev/null
+++ b/api/pkg/repositories/memory_attachment_repository.go
@@ -0,0 +1,60 @@
+package repositories
+
+import (
+ "context"
+ "fmt"
+ "sync"
+
+ "github.com/NdoleStudio/httpsms/pkg/telemetry"
+ "github.com/palantir/stacktrace"
+)
+
+// MemoryAttachmentRepository stores attachments in memory
+type MemoryAttachmentRepository struct {
+ logger telemetry.Logger
+ tracer telemetry.Tracer
+ data sync.Map
+}
+
+// NewMemoryAttachmentRepository creates a new MemoryAttachmentRepository
+func NewMemoryAttachmentRepository(
+ logger telemetry.Logger,
+ tracer telemetry.Tracer,
+) *MemoryAttachmentRepository {
+ return &MemoryAttachmentRepository{
+ logger: logger.WithService(fmt.Sprintf("%T", &MemoryAttachmentRepository{})),
+ tracer: tracer,
+ }
+}
+
+// Upload stores attachment data at the given path
+func (s *MemoryAttachmentRepository) Upload(ctx context.Context, path string, data []byte, _ string) error {
+ _, span, ctxLogger := s.tracer.StartWithLogger(ctx, s.logger)
+ defer span.End()
+
+ s.data.Store(path, data)
+ ctxLogger.Info(fmt.Sprintf("stored attachment at path [%s] with size [%d]", path, len(data)))
+ return nil
+}
+
+// Download retrieves attachment data from the given path
+func (s *MemoryAttachmentRepository) Download(ctx context.Context, path string) ([]byte, error) {
+ _, span, _ := s.tracer.StartWithLogger(ctx, s.logger)
+ defer span.End()
+
+ value, ok := s.data.Load(path)
+ if !ok {
+ return nil, s.tracer.WrapErrorSpan(span, stacktrace.NewErrorWithCode(ErrCodeNotFound, fmt.Sprintf("attachment not found at path [%s]", path)))
+ }
+ return value.([]byte), nil
+}
+
+// Delete removes an attachment at the given path
+func (s *MemoryAttachmentRepository) Delete(ctx context.Context, path string) error {
+ _, span, ctxLogger := s.tracer.StartWithLogger(ctx, s.logger)
+ defer span.End()
+
+ s.data.Delete(path)
+ ctxLogger.Info(fmt.Sprintf("deleted attachment at path [%s]", path))
+ return nil
+}
diff --git a/api/pkg/repositories/memory_attachment_storage.go b/api/pkg/repositories/memory_attachment_storage.go
deleted file mode 100644
index 602c2509..00000000
--- a/api/pkg/repositories/memory_attachment_storage.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package repositories
-
-import (
- "context"
- "fmt"
- "sync"
-
- "github.com/NdoleStudio/httpsms/pkg/telemetry"
-)
-
-// MemoryAttachmentStorage stores attachments in memory
-type MemoryAttachmentStorage struct {
- logger telemetry.Logger
- tracer telemetry.Tracer
- data sync.Map
-}
-
-// NewMemoryAttachmentStorage creates a new MemoryAttachmentStorage
-func NewMemoryAttachmentStorage(
- logger telemetry.Logger,
- tracer telemetry.Tracer,
-) *MemoryAttachmentStorage {
- return &MemoryAttachmentStorage{
- logger: logger.WithService(fmt.Sprintf("%T", &MemoryAttachmentStorage{})),
- tracer: tracer,
- }
-}
-
-// Upload stores attachment data at the given path
-func (s *MemoryAttachmentStorage) Upload(ctx context.Context, path string, data []byte) error {
- _, span := s.tracer.Start(ctx)
- defer span.End()
-
- s.data.Store(path, data)
- s.logger.Info(fmt.Sprintf("stored attachment at path [%s] with size [%d]", path, len(data)))
- return nil
-}
-
-// Download retrieves attachment data from the given path
-func (s *MemoryAttachmentStorage) Download(ctx context.Context, path string) ([]byte, error) {
- _, span := s.tracer.Start(ctx)
- defer span.End()
-
- value, ok := s.data.Load(path)
- if !ok {
- return nil, ErrAttachmentNotFound
- }
- return value.([]byte), nil
-}
-
-// Delete removes an attachment at the given path
-func (s *MemoryAttachmentStorage) Delete(ctx context.Context, path string) error {
- _, span := s.tracer.Start(ctx)
- defer span.End()
-
- s.data.Delete(path)
- s.logger.Info(fmt.Sprintf("deleted attachment at path [%s]", path))
- return nil
-}
diff --git a/api/pkg/requests/message_receive_request.go b/api/pkg/requests/message_receive_request.go
index be405101..b89cddfa 100644
--- a/api/pkg/requests/message_receive_request.go
+++ b/api/pkg/requests/message_receive_request.go
@@ -34,7 +34,7 @@ type MessageReceive struct {
// Timestamp is the time when the event was emitted, Please send the timestamp in UTC with as much precision as possible
Timestamp time.Time `json:"timestamp" example:"2022-06-05T14:26:09.527976+03:00"`
// Attachments is the list of MMS attachments received with the message
- Attachments []MessageAttachment `json:"attachments"`
+ Attachments []MessageAttachment `json:"attachments" validate:"optional"`
}
// Sanitize sets defaults to MessageReceive
diff --git a/api/pkg/services/message_service.go b/api/pkg/services/message_service.go
index 8ed4a1f3..c661da9e 100644
--- a/api/pkg/services/message_service.go
+++ b/api/pkg/services/message_service.go
@@ -36,8 +36,8 @@ type MessageService struct {
eventDispatcher *EventDispatcher
phoneService *PhoneService
repository repositories.MessageRepository
- attachmentStorage repositories.AttachmentStorage
- apiBaseURL string
+ attachmentRepository repositories.AttachmentRepository
+ apiBaseURL string
}
// NewMessageService creates a new MessageService
@@ -47,17 +47,17 @@ func NewMessageService(
repository repositories.MessageRepository,
eventDispatcher *EventDispatcher,
phoneService *PhoneService,
- attachmentStorage repositories.AttachmentStorage,
+ attachmentRepository repositories.AttachmentRepository,
apiBaseURL string,
) (s *MessageService) {
return &MessageService{
- logger: logger.WithService(fmt.Sprintf("%T", s)),
- tracer: tracer,
- repository: repository,
- phoneService: phoneService,
- eventDispatcher: eventDispatcher,
- attachmentStorage: attachmentStorage,
- apiBaseURL: apiBaseURL,
+ logger: logger.WithService(fmt.Sprintf("%T", s)),
+ tracer: tracer,
+ repository: repository,
+ phoneService: phoneService,
+ eventDispatcher: eventDispatcher,
+ attachmentRepository: attachmentRepository,
+ apiBaseURL: apiBaseURL,
}
}
@@ -612,11 +612,9 @@ func (service *MessageService) storeReceivedMessage(ctx context.Context, params
}
func (service *MessageService) uploadAttachments(ctx context.Context, userID entities.UserID, messageID uuid.UUID, attachments []ServiceAttachment) ([]string, error) {
- ctx, span := service.tracer.Start(ctx)
+ ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
- ctxLogger := service.tracer.CtxLogger(service.logger, span)
-
g, gCtx := errgroup.WithContext(ctx)
urls := make([]string, len(attachments))
paths := make([]string, len(attachments))
@@ -636,7 +634,7 @@ func (service *MessageService) uploadAttachments(ctx context.Context, userID ent
path := fmt.Sprintf("attachments/%s/%s/%d/%s", userID, messageID, i, filename)
paths[i] = path
- if err = service.attachmentStorage.Upload(gCtx, path, decoded); err != nil {
+ if err = service.attachmentRepository.Upload(gCtx, path, decoded, attachment.ContentType); err != nil {
return stacktrace.Propagate(err, fmt.Sprintf("cannot upload attachment [%d] to path [%s]", i, path))
}
@@ -649,7 +647,7 @@ func (service *MessageService) uploadAttachments(ctx context.Context, userID ent
if err := g.Wait(); err != nil {
for _, path := range paths {
if path != "" {
- _ = service.attachmentStorage.Delete(ctx, path)
+ _ = service.attachmentRepository.Delete(ctx, path)
}
}
return nil, stacktrace.Propagate(err, "cannot upload attachments")