diff options
Diffstat (limited to 'resources/resource_factories/create/remote.go')
-rw-r--r-- | resources/resource_factories/create/remote.go | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/resources/resource_factories/create/remote.go b/resources/resource_factories/create/remote.go index f6d3f13dd..f127f8edc 100644 --- a/resources/resource_factories/create/remote.go +++ b/resources/resource_factories/create/remote.go @@ -110,21 +110,30 @@ func (c *Client) FromRemote(uri string, optionsm map[string]interface{}) (resour } } - var extensionHint string - - if arr, _ := mime.ExtensionsByType(res.Header.Get("Content-Type")); len(arr) == 1 { - extensionHint = arr[0] + var extensionHints []string + + contentType := res.Header.Get("Content-Type") + + // mime.ExtensionsByType gives a long list of extensions for text/plain, + // just use ".txt". + if strings.HasPrefix(contentType, "text/plain") { + extensionHints = []string{".txt"} + } else { + exts, _ := mime.ExtensionsByType(contentType) + if exts != nil { + extensionHints = exts + } } - // Look for a file extention - if extensionHint == "" { + // Look for a file extention. If it's .txt, look for a more specific. + if extensionHints == nil || extensionHints[0] == ".txt" { if ext := path.Ext(filename); ext != "" { - extensionHint = ext + extensionHints = []string{ext} } } // Now resolve the media type primarily using the content. - mediaType := media.FromContent(c.rs.MediaTypes, extensionHint, body) + mediaType := media.FromContent(c.rs.MediaTypes, extensionHints, body) if mediaType.IsZero() { return nil, errors.Errorf("failed to resolve media type for remote resource %q", uri) } @@ -140,7 +149,6 @@ func (c *Client) FromRemote(uri string, optionsm map[string]interface{}) (resour }, RelTargetFilename: filepath.Clean(resourceID), }) - } func (c *Client) validateFromRemoteArgs(uri string, options fromRemoteOptions) error { @@ -213,7 +221,7 @@ func (o fromRemoteOptions) BodyReader() io.Reader { } func decodeRemoteOptions(optionsm map[string]interface{}) (fromRemoteOptions, error) { - var options = fromRemoteOptions{ + options := fromRemoteOptions{ Method: "GET", } @@ -224,5 +232,4 @@ func decodeRemoteOptions(optionsm map[string]interface{}) (fromRemoteOptions, er options.Method = strings.ToUpper(options.Method) return options, nil - } |