summaryrefslogtreecommitdiffstats
path: root/resources
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-03-11 09:18:01 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-03-14 15:21:54 +0100
commitba1d0051b44fdd242b20899e195e37ab26501516 (patch)
tree646f815cf5eb027bae13e329696acf14d640d3f9 /resources
parent1b1dcf586e220c3a8ad5ecfa8e4c3dac97f0ab44 (diff)
media: Make Type comparable
So we can use it and output.Format as map key etc. This commit also fixes the media.Type implementation so it does not need to mutate itself to handle different suffixes for the same MIME type, e.g. jpg vs. jpeg. This means that there are no Suffix or FullSuffix on media.Type anymore. Fixes #8317 Fixes #8324
Diffstat (limited to 'resources')
-rw-r--r--resources/images/image.go2
-rw-r--r--resources/page/page_paths.go11
-rw-r--r--resources/page/page_paths_test.go11
-rw-r--r--resources/postpub/fields_test.go11
-rw-r--r--resources/resource_spec.go4
5 files changed, 19 insertions, 20 deletions
diff --git a/resources/images/image.go b/resources/images/image.go
index 3d28263c0..b71321244 100644
--- a/resources/images/image.go
+++ b/resources/images/image.go
@@ -265,7 +265,7 @@ func (f Format) SupportsTransparency() bool {
// DefaultExtension returns the default file extension of this format, starting with a dot.
// For example: .jpg for JPEG
func (f Format) DefaultExtension() string {
- return f.MediaType().FullSuffix()
+ return f.MediaType().FirstSuffix.FullSuffix
}
// MediaType returns the media type of this image, e.g. image/jpeg for JPEG
diff --git a/resources/page/page_paths.go b/resources/page/page_paths.go
index fc576f05a..3d34866d1 100644
--- a/resources/page/page_paths.go
+++ b/resources/page/page_paths.go
@@ -128,6 +128,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
}
pagePath := slash
+ fullSuffix := d.Type.MediaType.FirstSuffix.FullSuffix
var (
pagePathDir string
@@ -172,7 +173,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
hasSlash := strings.HasSuffix(d.URL, slash)
if hasSlash || !hasDot {
- pagePath = pjoin(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
+ pagePath = pjoin(pagePath, d.Type.BaseName+fullSuffix)
} else if hasDot {
pagePathDir = path.Dir(pagePathDir)
}
@@ -229,9 +230,9 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
linkDir = pagePathDir
if isUgly {
- pagePath = addSuffix(pagePath, d.Type.MediaType.FullSuffix())
+ pagePath = addSuffix(pagePath, fullSuffix)
} else {
- pagePath = pjoin(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
+ pagePath = pjoin(pagePath, d.Type.BaseName+fullSuffix)
}
if !isHtmlIndex(pagePath) {
@@ -267,9 +268,9 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
linkDir = pagePathDir
if base != "" {
- pagePath = path.Join(pagePath, addSuffix(base, d.Type.MediaType.FullSuffix()))
+ pagePath = path.Join(pagePath, addSuffix(base, fullSuffix))
} else {
- pagePath = addSuffix(pagePath, d.Type.MediaType.FullSuffix())
+ pagePath = addSuffix(pagePath, fullSuffix)
}
if !isHtmlIndex(pagePath) {
diff --git a/resources/page/page_paths_test.go b/resources/page/page_paths_test.go
index ab7164f46..28937899f 100644
--- a/resources/page/page_paths_test.go
+++ b/resources/page/page_paths_test.go
@@ -27,8 +27,7 @@ import (
func TestPageTargetPath(t *testing.T) {
pathSpec := newTestPathSpec()
- noExtNoDelimMediaType := media.TextType
- noExtNoDelimMediaType.Suffixes = []string{}
+ noExtNoDelimMediaType := media.WithDelimiterAndSuffixes(media.TextType, "", "")
noExtNoDelimMediaType.Delimiter = ""
// Netlify style _redirects
@@ -209,11 +208,11 @@ func TestPageTargetPath(t *testing.T) {
// TODO(bep) simplify
if test.d.Kind == KindPage && test.d.BaseName == test.d.Type.BaseName {
} else if test.d.Kind == KindHome && test.d.Type.Path != "" {
- } else if test.d.Type.MediaType.Suffix() != "" && (!strings.HasPrefix(expected.TargetFilename, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
+ } else if test.d.Type.MediaType.FirstSuffix.Suffix != "" && (!strings.HasPrefix(expected.TargetFilename, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
expected.TargetFilename = strings.Replace(expected.TargetFilename,
- "/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.Suffix(),
- "."+test.d.Type.MediaType.Suffix(), 1)
- expected.Link = strings.TrimSuffix(expected.Link, "/") + "." + test.d.Type.MediaType.Suffix()
+ "/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.FirstSuffix.Suffix,
+ "."+test.d.Type.MediaType.FirstSuffix.Suffix, 1)
+ expected.Link = strings.TrimSuffix(expected.Link, "/") + "." + test.d.Type.MediaType.FirstSuffix.Suffix
}
diff --git a/resources/postpub/fields_test.go b/resources/postpub/fields_test.go
index 10a68ac5a..19c3720f7 100644
--- a/resources/postpub/fields_test.go
+++ b/resources/postpub/fields_test.go
@@ -31,15 +31,14 @@ func TestCreatePlaceholders(t *testing.T) {
})
c.Assert(m, qt.DeepEquals, map[string]interface{}{
- "FullSuffix": "pre_foo.FullSuffix_post",
"IsZero": "pre_foo.IsZero_post",
- "Type": "pre_foo.Type_post",
- "MainType": "pre_foo.MainType_post",
- "Delimiter": "pre_foo.Delimiter_post",
"MarshalJSON": "pre_foo.MarshalJSON_post",
+ "Suffixes": "pre_foo.Suffixes_post",
+ "Delimiter": "pre_foo.Delimiter_post",
+ "FirstSuffix": "pre_foo.FirstSuffix_post",
"String": "pre_foo.String_post",
- "Suffix": "pre_foo.Suffix_post",
+ "Type": "pre_foo.Type_post",
+ "MainType": "pre_foo.MainType_post",
"SubType": "pre_foo.SubType_post",
- "Suffixes": "pre_foo.Suffixes_post",
})
}
diff --git a/resources/resource_spec.go b/resources/resource_spec.go
index dc13d16d9..156def363 100644
--- a/resources/resource_spec.go
+++ b/resources/resource_spec.go
@@ -268,10 +268,10 @@ func (r *Spec) newResource(sourceFs afero.Fs, fd ResourceSourceDescriptor) (reso
}
ext := strings.ToLower(filepath.Ext(fd.RelTargetFilename))
- mimeType, found := r.MediaTypes.GetFirstBySuffix(strings.TrimPrefix(ext, "."))
+ mimeType, suffixInfo, found := r.MediaTypes.GetFirstBySuffix(strings.TrimPrefix(ext, "."))
// TODO(bep) we need to handle these ambiguous types better, but in this context
// we most likely want the application/xml type.
- if mimeType.Suffix() == "xml" && mimeType.SubType == "rss" {
+ if suffixInfo.Suffix == "xml" && mimeType.SubType == "rss" {
mimeType, found = r.MediaTypes.GetByType("application/xml")
}