2019-07-27 12:16:13 -04:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/htesting"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2019-07-27 12:16:13 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// We have many tests for the different resize operations etc. in the resource package,
|
|
|
|
// this is an integration test.
|
|
|
|
func TestImageResize(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-07-27 12:16:13 -04:00
|
|
|
// Make this a real as possible.
|
|
|
|
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "image-resize")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
defer clean()
|
|
|
|
|
|
|
|
newBuilder := func() *sitesBuilder {
|
|
|
|
|
|
|
|
v := viper.New()
|
|
|
|
v.Set("workingDir", workDir)
|
|
|
|
v.Set("baseURL", "https://example.org")
|
|
|
|
|
|
|
|
b := newTestSitesBuilder(t).WithWorkingDir(workDir)
|
|
|
|
b.Fs = hugofs.NewDefault(v)
|
|
|
|
b.WithViper(v)
|
|
|
|
b.WithContent("mybundle/index.md", `
|
|
|
|
---
|
|
|
|
title: "My bundle"
|
|
|
|
---
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
b.WithTemplatesAdded("index.html", `
|
|
|
|
{{ $p := .Site.GetPage "mybundle" }}
|
|
|
|
{{ $img1 := resources.Get "images/sunset.jpg" }}
|
|
|
|
{{ $img2 := $p.Resources.GetMatch "sunset.jpg" }}
|
2019-08-13 12:45:08 -04:00
|
|
|
{{ $img3 := resources.GetMatch "images/*.jpg" }}
|
2019-07-27 12:16:13 -04:00
|
|
|
{{ $r := $img1.Resize "123x234" }}
|
|
|
|
{{ $r2 := $r.Resize "12x23" }}
|
|
|
|
{{ $b := $img2.Resize "345x678" }}
|
|
|
|
{{ $b2 := $b.Resize "34x67" }}
|
2019-08-13 12:45:08 -04:00
|
|
|
{{ $c := $img3.Resize "456x789" }}
|
2019-09-02 11:09:56 -04:00
|
|
|
{{ $fingerprinted := $img1.Resize "350x" | fingerprint }}
|
2019-07-27 12:16:13 -04:00
|
|
|
|
2019-09-02 11:09:56 -04:00
|
|
|
{{ $images := slice $r $r2 $b $b2 $c $fingerprinted }}
|
2019-07-27 12:16:13 -04:00
|
|
|
|
|
|
|
{{ range $i, $r := $images }}
|
|
|
|
{{ printf "Resized%d:" (add $i 1) }} {{ $r.Name }}|{{ $r.Width }}|{{ $r.Height }}|{{ $r.MediaType }}|{{ $r.RelPermalink }}|
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
imageDir := filepath.Join(workDir, "assets", "images")
|
|
|
|
bundleDir := filepath.Join(workDir, "content", "mybundle")
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(os.MkdirAll(imageDir, 0777), qt.IsNil)
|
|
|
|
c.Assert(os.MkdirAll(bundleDir, 0777), qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
src, err := os.Open("testdata/sunset.jpg")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
out, err := os.Create(filepath.Join(imageDir, "sunset.jpg"))
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
_, err = io.Copy(out, src)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
out.Close()
|
|
|
|
|
|
|
|
src.Seek(0, 0)
|
|
|
|
|
|
|
|
out, err = os.Create(filepath.Join(bundleDir, "sunset.jpg"))
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
_, err = io.Copy(out, src)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-07-27 12:16:13 -04:00
|
|
|
out.Close()
|
|
|
|
src.Close()
|
|
|
|
|
|
|
|
b := newBuilder()
|
|
|
|
b.Build(BuildCfg{})
|
|
|
|
|
2019-09-02 11:09:56 -04:00
|
|
|
imgExpect := `
|
|
|
|
Resized1: images/sunset.jpg|123|234|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg|
|
|
|
|
Resized2: images/sunset.jpg|12|23|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_ada4bb1a57f77a63306e3bd67286248e.jpg|
|
|
|
|
Resized3: sunset.jpg|345|678|image/jpg|/mybundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_345x678_resize_q75_box.jpg|
|
|
|
|
Resized4: sunset.jpg|34|67|image/jpg|/mybundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_44d8c928664d7c5a67377c6ec58425ce.jpg|
|
|
|
|
Resized5: images/sunset.jpg|456|789|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_456x789_resize_q75_box.jpg|
|
|
|
|
Resized6: images/sunset.jpg|350|219|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_350x0_resize_q75_box.a86fe88d894e5db613f6aa8a80538fefc25b20fa24ba0d782c057adcef616f56.jpg|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), imgExpect)
|
2019-09-03 04:36:09 -04:00
|
|
|
b.AssertImage(350, 219, "public/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_350x0_resize_q75_box.a86fe88d894e5db613f6aa8a80538fefc25b20fa24ba0d782c057adcef616f56.jpg")
|
2019-07-27 12:16:13 -04:00
|
|
|
|
|
|
|
// Build it again to make sure we read images from file cache.
|
|
|
|
b = newBuilder()
|
|
|
|
b.Build(BuildCfg{})
|
|
|
|
|
2019-09-02 11:09:56 -04:00
|
|
|
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), imgExpect)
|
2019-07-27 12:16:13 -04:00
|
|
|
|
|
|
|
}
|
2019-09-03 04:36:09 -04:00
|
|
|
|
|
|
|
func TestImageResizeMultilingual(t *testing.T) {
|
|
|
|
|
|
|
|
b := newTestSitesBuilder(t).WithConfigFile("toml", `
|
|
|
|
baseURL="https://example.org"
|
|
|
|
defaultContentLanguage = "en"
|
|
|
|
|
|
|
|
[languages]
|
|
|
|
[languages.en]
|
|
|
|
title = "Title in English"
|
|
|
|
languageName = "English"
|
|
|
|
weight = 1
|
|
|
|
[languages.nn]
|
|
|
|
languageName = "Nynorsk"
|
|
|
|
weight = 2
|
|
|
|
title = "Tittel på nynorsk"
|
|
|
|
[languages.nb]
|
|
|
|
languageName = "Bokmål"
|
|
|
|
weight = 3
|
|
|
|
title = "Tittel på bokmål"
|
|
|
|
[languages.fr]
|
|
|
|
languageName = "French"
|
|
|
|
weight = 4
|
|
|
|
title = "French Title"
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
pageContent := `---
|
|
|
|
title: "Page"
|
|
|
|
---
|
|
|
|
`
|
|
|
|
|
|
|
|
b.WithContent("bundle/index.md", pageContent)
|
|
|
|
b.WithContent("bundle/index.nn.md", pageContent)
|
|
|
|
b.WithContent("bundle/index.fr.md", pageContent)
|
|
|
|
b.WithSunset("content/bundle/sunset.jpg")
|
|
|
|
b.WithSunset("assets/images/sunset.jpg")
|
|
|
|
b.WithTemplates("index.html", `
|
|
|
|
{{ with (.Site.GetPage "bundle" ) }}
|
|
|
|
{{ $sunset := .Resources.GetMatch "sunset*" }}
|
|
|
|
{{ if $sunset }}
|
|
|
|
{{ $resized := $sunset.Resize "200x200" }}
|
|
|
|
SUNSET FOR: {{ $.Site.Language.Lang }}: {{ $resized.RelPermalink }}/{{ $resized.Width }}/Lat: {{ $resized.Exif.Lat }}
|
|
|
|
{{ end }}
|
|
|
|
{{ else }}
|
|
|
|
No bundle for {{ $.Site.Language.Lang }}
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
{{ $sunset2 := resources.Get "images/sunset.jpg" }}
|
|
|
|
{{ $resized2 := $sunset2.Resize "123x234" }}
|
|
|
|
SUNSET2: {{ $resized2.RelPermalink }}/{{ $resized2.Width }}/Lat: {{ $resized2.Exif.Lat }}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
b.Build(BuildCfg{})
|
|
|
|
|
|
|
|
b.AssertFileContent("public/index.html", "SUNSET FOR: en: /bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg/200/Lat: 36.59744166666667")
|
|
|
|
b.AssertFileContent("public/fr/index.html", "SUNSET FOR: fr: /fr/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg/200/Lat: 36.59744166666667")
|
|
|
|
b.AssertFileContent("public/index.html", " SUNSET2: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg/123/Lat: 36.59744166666667")
|
|
|
|
b.AssertFileContent("public/nn/index.html", " SUNSET2: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg/123/Lat: 36.59744166666667")
|
|
|
|
|
|
|
|
b.AssertImage(200, 200, "public/fr/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg")
|
|
|
|
b.AssertImage(200, 200, "public/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg")
|
|
|
|
|
|
|
|
// Check the file cache
|
|
|
|
b.AssertImage(200, 200, "resources/_gen/images/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg")
|
|
|
|
b.AssertFileContent("resources/_gen/images/bundle/sunset_17701188623491591036.json",
|
|
|
|
"DateTimeDigitized|time.Time", "PENTAX")
|
|
|
|
b.AssertImage(123, 234, "resources/_gen/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg")
|
|
|
|
b.AssertFileContent("resources/_gen/images/sunset_17701188623491591036.json",
|
|
|
|
"DateTimeDigitized|time.Time", "PENTAX")
|
|
|
|
|
|
|
|
}
|