Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional author and avatar params #5

Merged
merged 1 commit into from
Aug 3, 2021
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
27 changes: 20 additions & 7 deletions internal/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ const (
maxTitleLength = 90
defaultBgColor = "#FFFFFF"
avatarBorderColor = "#FFFFFF"
logoKey = "logo"
avaKey = "avatar"
bgKey = "bg"
)

var hexRe = regexp.MustCompile("^#(?:[0-9a-fA-F]{3}){1,2}$")

type getter interface {
GetAll(context.Context, []string) ([][]byte, error)
GetAll(context.Context, map[string]string) (map[string][]byte, error)
}

// Options defines a set of options required to draw a p.ctx.
Expand Down Expand Up @@ -87,13 +90,17 @@ func (p *Preview) Draw(ctx context.Context, opts Options) (image.Image, error) {
p.opts = &opts
p.ctx = gg.NewContext(opts.CanvasW, opts.CanvasH)
bgColor := defaultBgColor
urlsOrPaths := []string{opts.AvaURL, opts.LogoURL}
isBgHEX := hexRe.Match([]byte(p.opts.Bg))
urlsOrPaths := map[string]string{logoKey: p.opts.LogoURL}

if opts.AvaURL != "" {
urlsOrPaths[avaKey] = p.opts.AvaURL
}

if isBgHEX {
bgColor = p.opts.Bg
} else if p.opts.Bg != "" {
urlsOrPaths = append(urlsOrPaths, p.opts.Bg)
urlsOrPaths[bgKey] = p.opts.Bg
}

imgBufs, err := p.remote.GetAll(ctx, urlsOrPaths)
Expand All @@ -107,7 +114,7 @@ func (p *Preview) Draw(ctx context.Context, opts Options) (image.Image, error) {
return nil, err
}
} else {
if err := p.drawBackground(imgBufs[2], bgColor); err != nil {
if err := p.drawBackground(imgBufs[bgKey], bgColor); err != nil {
return nil, err
}
}
Expand All @@ -116,8 +123,10 @@ func (p *Preview) Draw(ctx context.Context, opts Options) (image.Image, error) {
return nil, err
}

if err := p.drawAvatar(imgBufs[0]); err != nil {
return nil, err
if _, exists := imgBufs[avaKey]; exists {
if err := p.drawAvatar(imgBufs[avaKey]); err != nil {
return nil, err
}
}

if err := p.drawAuthor(); err != nil {
Expand All @@ -128,7 +137,7 @@ func (p *Preview) Draw(ctx context.Context, opts Options) (image.Image, error) {
return nil, err
}

if err := p.drawLogo(imgBufs[1]); err != nil {
if err := p.drawLogo(imgBufs[logoKey]); err != nil {
return nil, err
}

Expand Down Expand Up @@ -199,6 +208,10 @@ func (p *Preview) drawAvatar(avaBuf []byte) error {
}

func (p *Preview) drawAuthor() error {
if p.opts.Author == "" {
return nil
}

font, err := loadFont(p.opts.AuthorSize)

if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ func (r *Remote) Get(ctx context.Context, urlOrPath string) (buf []byte, err err
}

// GetAll fetches remote resources concurrently using Get
func (r *Remote) GetAll(ctx context.Context, urlsOrPaths []string) ([][]byte, error) {
bufs := make([][]byte, len(urlsOrPaths))
func (r *Remote) GetAll(ctx context.Context, urlsOrPaths map[string]string) (map[string][]byte, error) {
bufs := make(map[string][]byte, len(urlsOrPaths))
errCh := make(chan error)
doneCh := make(chan bool)
var wg sync.WaitGroup

wg.Add(len(urlsOrPaths))

for i, urlOrPath := range urlsOrPaths {
go func(i int, urlOrPath string) {
for key, urlOrPath := range urlsOrPaths {
go func(key string, urlOrPath string) {
buf, err := r.Get(ctx, urlOrPath)

if err != nil {
errCh <- err
}

bufs[i] = buf
bufs[key] = buf

wg.Done()
}(i, urlOrPath)
}(key, urlOrPath)
}

go func() {
Expand Down
18 changes: 8 additions & 10 deletions internal/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ func getPreview(d drawer) http.HandlerFunc {

authorParam := r.URL.Query().Get("author")

if authorParam == "" {
handleBadRequest(w, errors.New("Missing required author parameter"))
return
if authorParam != "" {
opts.Author = authorParam
} else {
opts.AvaD = 0
}

opts.Author = authorParam

bgParam := r.URL.Query().Get("bg")

if bgParam != "" {
Expand All @@ -65,13 +64,12 @@ func getPreview(d drawer) http.HandlerFunc {

avaParam := r.URL.Query().Get("ava")

if avaParam == "" {
handleBadRequest(w, errors.New("Missing required ava parameter"))
return
if avaParam != "" {
opts.AvaURL = avaParam
} else {
opts.AuthorSize = 0
}

opts.AvaURL = avaParam

logoParam := r.URL.Query().Get("logo")

if logoParam == "" {
Expand Down
12 changes: 4 additions & 8 deletions internal/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestGetPreviewHandler_Success(t *testing.T) {
name: "basic",
req: "/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&author=%40Tester&ava=avatar.png&logo=logo.png",
expected: "./testdata/expected/basic.jpeg",
}, {
name: "no author",
req: "/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&logo=logo.png",
expected: "./testdata/expected/no-author.jpeg",
}, {
name: "opacity",
req: "/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&author=%40Tester&ava=avatar.png&logo=logo.png&op=0.5",
Expand Down Expand Up @@ -92,14 +96,6 @@ func TestGetPreviewHandler_Bad(t *testing.T) {
name: "title",
req: "/preview?author=%40Tester&ava=avatar.png&logo=logo.png",
expected: "Missing required title parameter",
}, {
name: "author",
req: "/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&ava=avatar.png&logo=logo.png",
expected: "Missing required author parameter",
}, {
name: "ava",
req: "/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&author=%40Tester&logo=logo.png",
expected: "Missing required ava parameter",
}, {
name: "logo",
req: "/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&author=%40Tester&ava=avatar.png",
Expand Down
Binary file added internal/server/testdata/expected/no-author.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ GET http://localhost:8201/preview?title=The%20quick%20brown%20fox%20jumps%20over

# only local and already resized images, color background
GET http://localhost:8201/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog.%20Sphinx%20of%20black%20quartz%2C%20judge%20my%20vow&author=%40DmitryNikitenko&bg=%23FFA&ava=avatar.png&logo=logo.png&op=0.5

###

# hidden author section
GET http://localhost:8201/preview?title=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog&logo=logo.png