Skip to content

mime: wrong extensions list for content type "image/jpeg" when using built-in types #36524

@junedev

Description

@junedev

What version of Go are you using (go version)?

$ go version
go version go1.13.6 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="xyz/.cache/go-build"
GOENV="xyz/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY="xyz"
GONOSUMDB="xyz"
GOOS="linux"
GOPATH="xyz/go"
GOPRIVATE="xyz"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build284189108=/tmp/go-build -gno-record-gcc-switches"
(Some values sanitized with "xyz".)

What did you do?

I was trying to retrieve file extensions for the content type "image/jpeg" with mime.ExtensionsByType("image/jpeg").
https://play.golang.org/p/gfVzW9oAdiq

What did you expect to see?

On systems where none of the following files are present, the built-in types list is used according to the documentation.

/etc/mime.types
/etc/apache2/mime.types
/etc/apache/mime.types

The built-in list can be viewed here: https://golang.org/src/mime/type.go#L59
It contains two entries for "image/jpeg". So I would expect to get these two entries as result of mime.ExtensionsByType("image/jpeg"): []string{".jpg", ".jpeg"}

There is also a code comment in that mentions this exact case: https://golang.org/src/mime/type.go#L19

What did you see instead?

On my machine either []string{".jpg"} or []string{".jpeg"} at random. On the playground always []string{".jpeg"}.

Here is a test case for the issue that can be run in "mime/type_test.go":

func TestExtensionsByType2(t *testing.T) {
	cleanup := setMimeInit(func() {
		clearMimeTypes()
                // Initialize built-in types like it is done in "type.go"
                // https://golang.org/src/mime/type.go#L84
		setMimeTypes(builtinTypesLower, builtinTypesLower)
	})
	defer cleanup()

	tests := []struct {
		typ     string
		want    []string
	}{
		{typ: "image/jpeg", want: []string{".jpg", ".jpeg"}},
	}

	for _, tt := range tests {
		got, err := ExtensionsByType(tt.typ)
		if err != nil {
			t.Error("error")
		}
		if !reflect.DeepEqual(got, tt.want) {
			t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions