Skip to content

Commit 86cd183

Browse files
committed
tpl/reflect: Make the IsImageResource implementation less technical
And closer to the documentation. The old one was correct, but a bit too tied to the internal implementation.
1 parent 871da33 commit 86cd183

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

‎resources/images/config.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ var (
5858
".webp": WEBP,
5959
}
6060

61-
imageFormatsBySubType = map[string]Format{
61+
// These are the image types we can process.
62+
processableImageSubTypes = map[string]Format{
6263
media.Builtin.JPEGType.SubType: JPEG,
6364
media.Builtin.PNGType.SubType: PNG,
6465
media.Builtin.TIFFType.SubType: TIFF,
@@ -123,7 +124,7 @@ func ImageFormatFromExt(ext string) (Format, bool) {
123124
}
124125

125126
func ImageFormatFromMediaSubType(sub string) (Format, bool) {
126-
f, found := imageFormatsBySubType[sub]
127+
f, found := processableImageSubTypes[sub]
127128
return f, found
128129
}
129130

‎resources/transform.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,16 @@ func (r *resourceAdapter) getImageOps() images.ImageResourceOps {
402402

403403
// IsImage reports whether the given resource is an image that can be processed.
404404
func IsImage(v any) bool {
405-
r, ok := v.(*resourceAdapter)
406-
if ok {
407-
_, ok := r.target.(images.ImageResourceOps)
408-
return ok
405+
r, ok := v.(resource.Resource)
406+
if !ok {
407+
return false
409408
}
410-
return false
409+
mt := r.MediaType()
410+
if mt.MainType != "image" {
411+
return false
412+
}
413+
_, isImage := images.ImageFormatFromMediaSubType(mt.SubType)
414+
return isImage
411415
}
412416

413417
func (r *resourceAdapter) publish() {

0 commit comments

Comments
 (0)