@@ -2,6 +2,7 @@ package repositories
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "io"
78
@@ -10,35 +11,37 @@ import (
1011 "github.com/palantir/stacktrace"
1112)
1213
13- // GCSAttachmentStorage stores attachments in Google Cloud Storage
14- type GCSAttachmentStorage struct {
14+ // GoogleCloudStorageAttachmentRepository stores attachments in Google Cloud Storage
15+ type GoogleCloudStorageAttachmentRepository struct {
1516 logger telemetry.Logger
1617 tracer telemetry.Tracer
1718 client * storage.Client
1819 bucket string
1920}
2021
21- // NewGCSAttachmentStorage creates a new GCSAttachmentStorage
22- func NewGCSAttachmentStorage (
22+ // NewGoogleCloudStorageAttachmentRepository creates a new GoogleCloudStorageAttachmentRepository
23+ func NewGoogleCloudStorageAttachmentRepository (
2324 logger telemetry.Logger ,
2425 tracer telemetry.Tracer ,
2526 client * storage.Client ,
2627 bucket string ,
27- ) * GCSAttachmentStorage {
28- return & GCSAttachmentStorage {
29- logger : logger .WithService (fmt .Sprintf ("%T" , & GCSAttachmentStorage {})),
28+ ) * GoogleCloudStorageAttachmentRepository {
29+ return & GoogleCloudStorageAttachmentRepository {
30+ logger : logger .WithService (fmt .Sprintf ("%T" , & GoogleCloudStorageAttachmentRepository {})),
3031 tracer : tracer ,
3132 client : client ,
3233 bucket : bucket ,
3334 }
3435}
3536
3637// Upload stores attachment data at the given path in GCS
37- func (s * GCSAttachmentStorage ) Upload (ctx context.Context , path string , data []byte ) error {
38- ctx , span := s .tracer .Start (ctx )
38+ func (s * GoogleCloudStorageAttachmentRepository ) Upload (ctx context.Context , path string , data []byte , contentType string ) error {
39+ ctx , span , ctxLogger := s .tracer .StartWithLogger (ctx , s . logger )
3940 defer span .End ()
4041
4142 writer := s .client .Bucket (s .bucket ).Object (path ).NewWriter (ctx )
43+ writer .ContentType = contentType
44+
4245 if _ , err := writer .Write (data ); err != nil {
4346 return s .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot write attachment to GCS path [%s]" , path )))
4447 }
@@ -47,18 +50,22 @@ func (s *GCSAttachmentStorage) Upload(ctx context.Context, path string, data []b
4750 return s .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot close GCS writer for path [%s]" , path )))
4851 }
4952
50- s . logger .Info (fmt .Sprintf ("uploaded attachment to GCS path [%s/%s] with size [%d]" , s .bucket , path , len (data )))
53+ ctxLogger .Info (fmt .Sprintf ("uploaded attachment to GCS path [%s/%s] with size [%d]" , s .bucket , path , len (data )))
5154 return nil
5255}
5356
5457// Download retrieves attachment data from the given path in GCS
55- func (s * GCSAttachmentStorage ) Download (ctx context.Context , path string ) ([]byte , error ) {
56- ctx , span := s .tracer .Start (ctx )
58+ func (s * GoogleCloudStorageAttachmentRepository ) Download (ctx context.Context , path string ) ([]byte , error ) {
59+ ctx , span , ctxLogger := s .tracer .StartWithLogger (ctx , s . logger )
5760 defer span .End ()
5861
5962 reader , err := s .client .Bucket (s .bucket ).Object (path ).NewReader (ctx )
6063 if err != nil {
61- return nil , s .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot open GCS reader for path [%s]" , path )))
64+ msg := fmt .Sprintf ("cannot open GCS reader for path [%s]" , path )
65+ if errors .Is (err , storage .ErrObjectNotExist ) {
66+ return nil , s .tracer .WrapErrorSpan (span , stacktrace .PropagateWithCode (err , ErrCodeNotFound , msg ))
67+ }
68+ return nil , s .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , msg ))
6269 }
6370 defer reader .Close ()
6471
@@ -67,18 +74,19 @@ func (s *GCSAttachmentStorage) Download(ctx context.Context, path string) ([]byt
6774 return nil , s .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot read attachment from GCS path [%s]" , path )))
6875 }
6976
77+ ctxLogger .Info (fmt .Sprintf ("downloaded attachment from GCS path [%s/%s] with size [%d]" , s .bucket , path , len (data )))
7078 return data , nil
7179}
7280
7381// Delete removes an attachment at the given path in GCS
74- func (s * GCSAttachmentStorage ) Delete (ctx context.Context , path string ) error {
75- ctx , span := s .tracer .Start (ctx )
82+ func (s * GoogleCloudStorageAttachmentRepository ) Delete (ctx context.Context , path string ) error {
83+ ctx , span , ctxLogger := s .tracer .StartWithLogger (ctx , s . logger )
7684 defer span .End ()
7785
7886 if err := s .client .Bucket (s .bucket ).Object (path ).Delete (ctx ); err != nil {
7987 return s .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot delete GCS object at path [%s]" , path )))
8088 }
8189
82- s . logger .Info (fmt .Sprintf ("deleted attachment from GCS path [%s/%s]" , s .bucket , path ))
90+ ctxLogger .Info (fmt .Sprintf ("deleted attachment from GCS path [%s/%s]" , s .bucket , path ))
8391 return nil
8492}
0 commit comments