Skip to content

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

feat: add MemoryAttachmentStorage implementation

Acho Arnoldee01c329c616
diff --git a/api/pkg/repositories/memory_attachment_storage.go b/api/pkg/repositories/memory_attachment_storage.go
new file mode 100644
index 00000000..93047887
--- /dev/null
+++ b/api/pkg/repositories/memory_attachment_storage.go
@@ -0,0 +1,60 @@
+package repositories
+
+import (
+ "context"
+ "fmt"
+ "sync"
+
+ "github.com/NdoleStudio/httpsms/pkg/telemetry"
+ "github.com/palantir/stacktrace"
+)
+
+// 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, stacktrace.NewError(fmt.Sprintf("attachment not found at path [%s]", path))
+ }
+ 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
+}