Spaces:
Paused
Paused
File size: 1,791 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package repositories
import (
"context"
"fmt"
"sync"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/NdoleStudio/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.NewErrorWithCodef(ErrCodeNotFound, "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
}
|