Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion router/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ func (EventRouter) UploadPhotos(ctx context.Context, input *UploadEventPhotosInp
if err := service.EventServiceApp.AddPhotos(&event, middleware.CreateIdentityFromAuth(auth), uploads); err != nil {
if errors.Is(err, service.ErrPhotoStorage) {
util.Logger.Error(err)
return nil, huma.Error500InternalServerError(service.ErrPhotoStorage.Error())
detail := service.ErrPhotoStorage.Error()
if code := util.PhotoErrorCode(err); code != "" {
detail += ": OSS " + code
}
return nil, huma.Error500InternalServerError(detail)
}
return nil, huma.Error422UnprocessableEntity(err.Error())
}
Expand Down
9 changes: 9 additions & 0 deletions util/photo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"crypto/rand"
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -68,6 +69,14 @@ func (s *ossPhotoStore) SignURL(key string) (string, error) {
return bucket.SignURL(key, oss.HTTPGet, 3600)
}

func PhotoErrorCode(err error) string {
var serviceErr oss.ServiceError
if errors.As(err, &serviceErr) {
return serviceErr.Code
}
return ""
}

func PhotoKey(eventId int64, contentType string) string {
ext := ".jpg"
if contentType == "image/webp" {
Expand Down
13 changes: 13 additions & 0 deletions util/photo_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package util_test

import (
"errors"
"fmt"
"regexp"
"testing"

"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/nbtca/saturday/util"
)

Expand All @@ -20,3 +23,13 @@ func TestPhotoKey(t *testing.T) {
t.Error("keys must be unique")
}
}

func TestPhotoErrorCode(t *testing.T) {
err := fmt.Errorf("failed to store photos: %w", oss.ServiceError{Code: "AccessDenied"})
if code := util.PhotoErrorCode(err); code != "AccessDenied" {
t.Errorf("got %q, want AccessDenied", code)
}
if code := util.PhotoErrorCode(errors.New("connection reset")); code != "" {
t.Errorf("got %q, want empty", code)
}
}
Loading