-
Notifications
You must be signed in to change notification settings - Fork 203
fix(send): generate JPEGThumbnail for images sent via /send/media #82
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
Open
fernandopaes0
wants to merge
1
commit into
evolution-foundation:main
Choose a base branch
from
fernandopaes0:pr/image-thumbnail
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package send_service | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "image" | ||
| "image/color" | ||
| "image/jpeg" | ||
| "image/png" | ||
| "testing" | ||
| ) | ||
|
|
||
| // encodePNG builds a simple solid-color PNG of the given size for testing. | ||
| func encodePNG(t *testing.T, w, h int) []byte { | ||
| t.Helper() | ||
| img := image.NewRGBA(image.Rect(0, 0, w, h)) | ||
| for y := 0; y < h; y++ { | ||
| for x := 0; x < w; x++ { | ||
| img.Set(x, y, color.RGBA{R: 10, G: 120, B: 200, A: 255}) | ||
| } | ||
| } | ||
| var buf bytes.Buffer | ||
| if err := png.Encode(&buf, img); err != nil { | ||
| t.Fatalf("failed to encode test PNG: %v", err) | ||
| } | ||
| return buf.Bytes() | ||
| } | ||
|
|
||
| func TestMakeJPEGThumbnail_ResizesLandscape(t *testing.T) { | ||
| src := encodePNG(t, 800, 400) | ||
|
|
||
| thumb := makeJPEGThumbnail(src, 72) | ||
| if thumb == nil { | ||
| t.Fatal("expected a thumbnail, got nil") | ||
| } | ||
|
|
||
| cfg, format, err := image.DecodeConfig(bytes.NewReader(thumb)) | ||
| if err != nil { | ||
| t.Fatalf("thumbnail is not a decodable image: %v", err) | ||
| } | ||
| if format != "jpeg" { | ||
| t.Fatalf("expected jpeg thumbnail, got %q", format) | ||
| } | ||
| if cfg.Width != 72 { | ||
| t.Fatalf("expected width 72, got %d", cfg.Width) | ||
| } | ||
| // Aspect ratio (2:1) must be preserved: 72 wide -> 36 tall. | ||
| if cfg.Height != 36 { | ||
| t.Fatalf("expected height 36 to preserve aspect ratio, got %d", cfg.Height) | ||
| } | ||
| } | ||
|
|
||
| func TestMakeJPEGThumbnail_DoesNotUpscaleSmallImages(t *testing.T) { | ||
| src := encodePNG(t, 40, 40) | ||
|
|
||
| thumb := makeJPEGThumbnail(src, 72) | ||
| if thumb == nil { | ||
| t.Fatal("expected a thumbnail, got nil") | ||
| } | ||
|
|
||
| cfg, _, err := image.DecodeConfig(bytes.NewReader(thumb)) | ||
| if err != nil { | ||
| t.Fatalf("thumbnail is not a decodable image: %v", err) | ||
| } | ||
| if cfg.Width != 40 || cfg.Height != 40 { | ||
| t.Fatalf("expected the thumbnail to keep the original 40x40 size, got %dx%d", cfg.Width, cfg.Height) | ||
| } | ||
| } | ||
|
|
||
| func TestMakeJPEGThumbnail_InvalidInputReturnsNil(t *testing.T) { | ||
| if thumb := makeJPEGThumbnail([]byte("not an image"), 72); thumb != nil { | ||
| t.Fatalf("expected nil for non-image input, got %d bytes", len(thumb)) | ||
| } | ||
| if thumb := makeJPEGThumbnail(nil, 72); thumb != nil { | ||
| t.Fatalf("expected nil for nil input, got %d bytes", len(thumb)) | ||
| } | ||
| } | ||
|
|
||
| func TestMakeJPEGThumbnail_DefaultsInvalidMaxWidth(t *testing.T) { | ||
| src := encodePNG(t, 800, 800) | ||
|
|
||
| thumb := makeJPEGThumbnail(src, 0) | ||
| if thumb == nil { | ||
| t.Fatal("expected a thumbnail with defaulted maxWidth, got nil") | ||
| } | ||
| cfg, _, err := image.DecodeConfig(bytes.NewReader(thumb)) | ||
| if err != nil { | ||
| t.Fatalf("thumbnail is not a decodable image: %v", err) | ||
| } | ||
| if cfg.Width != 72 { | ||
| t.Fatalf("expected defaulted width 72, got %d", cfg.Width) | ||
| } | ||
| } | ||
|
|
||
| // ensure the jpeg encoder import stays referenced even if the helper changes. | ||
| var _ = jpeg.DefaultQuality | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Also test negative maxWidth to match the helper’s documented behavior
This helper treats any
maxWidth < 1as invalid and defaults to 72, but this test only covers0. Please either add coverage for a negativemaxWidth(e.g.-5) asserting it still defaults to 72, or rename the test to indicate it specifically coversmaxWidth == 0.