Improve handling of remote image/jpeg resources (#9278)
authorJoe Mooring <joe.mooring@veriphor.com>
Mon, 13 Dec 2021 07:55:15 +0000 (23:55 -0800)
committerGitHub <noreply@github.com>
Mon, 13 Dec 2021 07:55:15 +0000 (08:55 +0100)
Add jpe, jif, and jfif to image/jpeg extensions.
For remote image/jpeg without extension, always use jpg extension.

Closes #9275

media/mediaType.go
resources/images/config.go
resources/resource_factories/create/create.go

index f63d315d0cdfcdc72ab8dc95ae92e87572aec320..eec7a27a832b75a7819d3579e03fc2458424fa33 100644 (file)
@@ -177,7 +177,7 @@ var (
 
        // Common image types
        PNGType  = newMediaType("image", "png", []string{"png"})
-       JPEGType = newMediaType("image", "jpeg", []string{"jpg", "jpeg"})
+       JPEGType = newMediaType("image", "jpeg", []string{"jpg", "jpeg", "jpe", "jif", "jfif"})
        GIFType  = newMediaType("image", "gif", []string{"gif"})
        TIFFType = newMediaType("image", "tiff", []string{"tif", "tiff"})
        BMPType  = newMediaType("image", "bmp", []string{"bmp"})
index 88790e3c4724b8da44f2aa130001ed83cf7447df..c8990d5ca334f91317818be3d908cd811a942a71 100644 (file)
@@ -34,6 +34,9 @@ var (
        imageFormats = map[string]Format{
                ".jpg":  JPEG,
                ".jpeg": JPEG,
+               ".jpe":  JPEG,
+               ".jif":  JPEG,
+               ".jfif": JPEG,
                ".png":  PNG,
                ".tif":  TIFF,
                ".tiff": TIFF,
index f7bde9ee6bfa522b1dc6ff88dc46c8431f341f40..64e2f95a68298f4a389e1a5a2a2012809b860b0e 100644 (file)
@@ -226,28 +226,30 @@ func (c *Client) FromRemote(uri string, options map[string]interface{}) (resourc
                }
        }
 
-       var contentType string
+       var extension string
        if arr, _ := mime.ExtensionsByType(res.Header.Get("Content-Type")); len(arr) == 1 {
-               contentType = arr[0]
+               extension = arr[0]
        }
 
-       // If content type was not determined by header, look for a file extention
-       if contentType == "" {
+       // If extension was not determined by header, look for a file extention
+       if extension == "" {
                if ext := path.Ext(filename); ext != "" {
-                       contentType = ext
+                       extension = ext
                }
        }
 
-       // If content type was not determined by header or file extention, try using content itself
-       if contentType == "" {
+       // If extension was not determined by header or file extention, try using content itself
+       if extension == "" {
                if ct := http.DetectContentType(body); ct != "application/octet-stream" {
-                       if arr, _ := mime.ExtensionsByType(ct); arr != nil {
-                               contentType = arr[0]
+                       if ct == "image/jpeg" {
+                               extension = ".jpg"
+                       } else if arr, _ := mime.ExtensionsByType(ct); arr != nil {
+                               extension = arr[0]
                        }
                }
        }
 
-       resourceID = filename[:len(filename)-len(path.Ext(filename))] + "_" + resourceID + contentType
+       resourceID = filename[:len(filename)-len(path.Ext(filename))] + "_" + resourceID + extension
 
        return c.rs.New(
                resources.ResourceSourceDescriptor{