Skip to content

Commit e9cd66c

Browse files
committed
[attachments] minor fixes
1 parent cc75d74 commit e9cd66c

1 file changed

Lines changed: 41 additions & 36 deletions

File tree

internal/attachments/service.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ func (s *Service) InitUpload(ctx context.Context, input AttachmentInput) (*Uploa
5454
return nil, err
5555
}
5656

57-
if ok, err := s.tasksSvc.Exists(ctx, input.TaskID); err != nil || !ok {
58-
if !ok {
59-
return nil, ErrTaskNotFound
60-
}
57+
ok, err := s.tasksSvc.Exists(ctx, input.TaskID)
58+
if err != nil {
6159
return nil, fmt.Errorf("failed to check if task exists: %w", err)
6260
}
61+
if !ok {
62+
return nil, ErrTaskNotFound
63+
}
6364

6465
storageKey := s.buildStorageKey(input.TaskID, input.FileName)
6566
uploadURL, err := s.storageSvc.PresignedPutObject(ctx, storageKey)
@@ -78,21 +79,27 @@ func (s *Service) InitUpload(ctx context.Context, input AttachmentInput) (*Uploa
7879
}, nil
7980
}
8081

81-
func (s *Service) ConfirmUpload(ctx context.Context, id int64, uploaderID int64) (*Attachment, error) {
82-
attachment, err := s.attachments.GetByID(ctx, id)
82+
func (s *Service) ListByTask(ctx context.Context, taskID int64) ([]AttachmentWithURL, error) {
83+
items, err := s.attachments.ListByTask(ctx, taskID)
8384
if err != nil {
8485
return nil, err
8586
}
8687

87-
if attachment.UploadedBy != uploaderID {
88-
return nil, ErrUnauthorized
89-
}
88+
result := make([]AttachmentWithURL, 0, len(items))
89+
for _, item := range items {
90+
if item.Status != StatusUploaded {
91+
continue
92+
}
9093

91-
if confirmErr := s.attachments.Confirm(ctx, id); confirmErr != nil {
92-
return nil, confirmErr
94+
downloadURL, urlErr := s.storageSvc.PresignedGetObject(ctx, item.StorageKey)
95+
if urlErr != nil {
96+
return nil, fmt.Errorf("failed to create download url: %w", urlErr)
97+
}
98+
99+
result = append(result, AttachmentWithURL{Attachment: item, DownloadURL: downloadURL})
93100
}
94101

95-
return s.attachments.GetByID(ctx, id)
102+
return result, nil
96103
}
97104

98105
func (s *Service) GetDownloadURL(ctx context.Context, id int64) (string, error) {
@@ -113,6 +120,23 @@ func (s *Service) GetDownloadURL(ctx context.Context, id int64) (string, error)
113120
return downloadURL, nil
114121
}
115122

123+
func (s *Service) ConfirmUpload(ctx context.Context, id int64, uploaderID int64) (*Attachment, error) {
124+
attachment, err := s.attachments.GetByID(ctx, id)
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
if attachment.UploadedBy != uploaderID {
130+
return nil, ErrUnauthorized
131+
}
132+
133+
if confirmErr := s.attachments.Confirm(ctx, id); confirmErr != nil {
134+
return nil, confirmErr
135+
}
136+
137+
return s.attachments.GetByID(ctx, id)
138+
}
139+
116140
func (s *Service) Delete(ctx context.Context, user *users.User, id int64) error {
117141
attachment, err := s.attachments.GetByID(ctx, id)
118142
if err != nil {
@@ -128,34 +152,15 @@ func (s *Service) Delete(ctx context.Context, user *users.User, id int64) error
128152
return ErrUnauthorized
129153
}
130154

131-
if delErr := s.storageSvc.Delete(ctx, attachment.StorageKey); delErr != nil {
132-
return fmt.Errorf("failed to delete attachment: %w", delErr)
133-
}
134-
135-
return s.attachments.Delete(ctx, id)
136-
}
137-
138-
func (s *Service) ListByTask(ctx context.Context, taskID int64) ([]AttachmentWithURL, error) {
139-
items, err := s.attachments.ListByTask(ctx, taskID)
140-
if err != nil {
141-
return nil, err
155+
if delErr := s.attachments.Delete(ctx, id); delErr != nil {
156+
return delErr
142157
}
143158

144-
result := make([]AttachmentWithURL, 0, len(items))
145-
for _, item := range items {
146-
if item.Status != StatusUploaded {
147-
continue
148-
}
149-
150-
downloadURL, urlErr := s.storageSvc.PresignedGetObject(ctx, item.StorageKey)
151-
if urlErr != nil {
152-
return nil, fmt.Errorf("failed to create download url: %w", urlErr)
153-
}
154-
155-
result = append(result, AttachmentWithURL{Attachment: item, DownloadURL: downloadURL})
159+
if delErr := s.storageSvc.Delete(ctx, attachment.StorageKey); delErr != nil {
160+
return fmt.Errorf("attachment metadata deleted, but object cleanup failed: %w", delErr)
156161
}
157162

158-
return result, nil
163+
return nil
159164
}
160165

161166
func (s *Service) buildStorageKey(taskID int64, fileName string) string {

0 commit comments

Comments
 (0)