Skip to content

Commit 382621b

Browse files
committed
Fix MediaType when reading images from cache
Fixes #8931
1 parent 0093eaa commit 382621b

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

hugolib/integrationtest_builder.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hugolib
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"fmt"
67
"io"
78
"os"
@@ -40,12 +41,13 @@ func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuild
4041
}
4142

4243
if conf.NeedsOsFS {
43-
doClean := true
4444
tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
4545
c.Assert(err, qt.IsNil)
4646
conf.WorkingDir = filepath.Join(tempDir, conf.WorkingDir)
47-
if doClean {
47+
if !conf.PrintAndKeepTempDir {
4848
c.Cleanup(clean)
49+
} else {
50+
fmt.Println("\nUsing WorkingDir dir:", conf.WorkingDir)
4951
}
5052
} else if conf.WorkingDir == "" {
5153
conf.WorkingDir = helpers.FilePathSeparator
@@ -278,10 +280,19 @@ func (s *IntegrationTestBuilder) initBuilder() {
278280

279281
logger := loggers.NewBasicLoggerForWriter(s.Cfg.LogLevel, &s.logBuff)
280282

283+
isBinaryRe := regexp.MustCompile(`^(.*)(\.png|\.jpg)$`)
284+
281285
for _, f := range s.data.Files {
282286
filename := filepath.Join(s.Cfg.WorkingDir, f.Name)
287+
data := bytes.TrimSuffix(f.Data, []byte("\n"))
288+
if isBinaryRe.MatchString(filename) {
289+
var err error
290+
data, err = base64.StdEncoding.DecodeString(string(data))
291+
s.Assert(err, qt.IsNil)
292+
293+
}
283294
s.Assert(afs.MkdirAll(filepath.Dir(filename), 0777), qt.IsNil)
284-
s.Assert(afero.WriteFile(afs, filename, bytes.TrimSuffix(f.Data, []byte("\n")), 0666), qt.IsNil)
295+
s.Assert(afero.WriteFile(afs, filename, data, 0666), qt.IsNil)
285296
}
286297

287298
cfg, _, err := LoadConfig(
@@ -297,6 +308,8 @@ func (s *IntegrationTestBuilder) initBuilder() {
297308
},
298309
)
299310

311+
s.Assert(err, qt.IsNil)
312+
300313
cfg.Set("workingDir", s.Cfg.WorkingDir)
301314

302315
fs := hugofs.NewFrom(afs, cfg)
@@ -457,6 +470,9 @@ type IntegrationTestConfig struct {
457470
// Whether it needs the real file system (e.g. for js.Build tests).
458471
NeedsOsFS bool
459472

473+
// Do not remove the temp dir after the test.
474+
PrintAndKeepTempDir bool
475+
460476
// Whether to run npm install before Build.
461477
NeedsNpmInstall bool
462478

resources/image_cache.go

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (c *imageCache) getOrCreate(
9595
rp := img.getResourcePaths()
9696
rp.relTargetDirFile.file = relTarget.file
9797
img.setSourceFilename(info.Name)
98+
img.setMediaType(conf.TargetFormat.MediaType())
9899

99100
if err := img.InitConfig(r); err != nil {
100101
return err

resources/integration_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2022 The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package resources_test
15+
16+
import (
17+
"strings"
18+
"testing"
19+
20+
"github.com/gohugoio/hugo/hugolib"
21+
)
22+
23+
// Issue 8931
24+
func TestImageCacheIssue9031(t *testing.T) {
25+
26+
files := `
27+
-- config.toml --
28+
baseURL = "https://example.org"
29+
-- content/mybundle/index.md --
30+
---
31+
title: "My Bundle"
32+
---
33+
-- content/mybundle/pixel.png --
34+
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
35+
-- layouts/foo.html --
36+
-- layouts/index.html --
37+
{{ $p := site.GetPage "mybundle"}}
38+
{{ $img := $p.Resources.Get "pixel.png" }}
39+
{{ $gif := $img.Resize "1x1 gif" }}
40+
{{ $bmp := $img.Resize "1x1 bmp" }}
41+
42+
gif: {{ $gif.RelPermalink }}|{{ $gif.MediaType }}|
43+
bmp: {{ $bmp.RelPermalink }}|{{ $bmp.MediaType }}|
44+
`
45+
46+
b := hugolib.NewIntegrationTestBuilder(
47+
hugolib.IntegrationTestConfig{
48+
T: t,
49+
TxtarString: files,
50+
NeedsOsFS: true,
51+
Running: true,
52+
}).Build()
53+
54+
assertImages := func() {
55+
b.AssertFileContent("public/index.html", `
56+
gif: /mybundle/pixel_hu8aa3346827e49d756ff4e630147c42b5_70_1x1_resize_box_3.gif|image/gif|
57+
bmp: /mybundle/pixel_hu8aa3346827e49d756ff4e630147c42b5_70_1x1_resize_box_3.bmp|image/bmp|
58+
59+
`)
60+
}
61+
62+
assertImages()
63+
64+
b.EditFileReplace("content/mybundle/index.md", func(s string) string { return strings.ReplaceAll(s, "Bundle", "BUNDLE") })
65+
b.Build()
66+
67+
assertImages()
68+
69+
}

0 commit comments

Comments
 (0)