diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-05 11:07:50 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-06 10:29:13 +0100 |
commit | f25d8a9e17fb65fa41dafdcbf0358853d68eaf45 (patch) | |
tree | fd8932fdf88b3774b5773866c5f8ceadea2cc15a /resource | |
parent | 54a89cde69c1fcda7afe0ba0f4e0de86f5f69aa5 (diff) |
Fix sub-folder baseURL handling for Page resources
I.e. images etc.
Fixes #4228
Diffstat (limited to 'resource')
-rw-r--r-- | resource/image.go | 10 | ||||
-rw-r--r-- | resource/resource.go | 23 | ||||
-rw-r--r-- | resource/resource_test.go | 31 | ||||
-rw-r--r-- | resource/testhelpers_test.go | 7 |
4 files changed, 52 insertions, 19 deletions
diff --git a/resource/image.go b/resource/image.go index 159161de2..8a65be1bf 100644 --- a/resource/image.go +++ b/resource/image.go @@ -108,7 +108,7 @@ type Image struct { configInit sync.Once configLoaded bool - copiedToDestinationInit sync.Once + copyToDestinationInit sync.Once imaging *Imaging @@ -206,7 +206,7 @@ func (i *Image) doWithImageConfig(action, spec string, f func(src image.Image, c conf.Filter = imageFilters[conf.FilterStr] } - key := i.relPermalinkForRel(i.filenameFromConfig(conf)) + key := i.relPermalinkForRel(i.filenameFromConfig(conf), false) return i.spec.imageCache.getOrCreate(i.spec, key, func(resourceCacheFilename string) (*Image, error) { ci := i.clone() @@ -232,7 +232,7 @@ func (i *Image) doWithImageConfig(action, spec string, f func(src image.Image, c ci.config = image.Config{Width: b.Max.X, Height: b.Max.Y} ci.configLoaded = true - return ci, i.encodeToDestinations(converted, conf, resourceCacheFilename, ci.RelPermalink()) + return ci, i.encodeToDestinations(converted, conf, resourceCacheFilename, ci.target()) }) } @@ -392,8 +392,8 @@ func (i *Image) decodeSource() (image.Image, error) { func (i *Image) copyToDestination(src string) error { var res error - i.copiedToDestinationInit.Do(func() { - target := filepath.Join(i.absPublishDir, i.RelPermalink()) + i.copyToDestinationInit.Do(func() { + target := filepath.Join(i.absPublishDir, i.target()) // Fast path: // This is a processed version of the original. diff --git a/resource/resource.go b/resource/resource.go index 19392f3d3..a9fa3df16 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -219,11 +219,11 @@ type genericResource struct { } func (l *genericResource) Permalink() string { - return l.spec.PermalinkForBaseURL(l.RelPermalink(), l.spec.BaseURL.String()) + return l.spec.PermalinkForBaseURL(l.relPermalinkForRel(l.rel, false), l.spec.BaseURL.String()) } func (l *genericResource) RelPermalink() string { - return l.relPermalinkForRel(l.rel) + return l.relPermalinkForRel(l.rel, true) } // Implement the Cloner interface. @@ -232,16 +232,21 @@ func (l genericResource) WithNewBase(base string) Resource { return &l } -func (l *genericResource) relPermalinkForRel(rel string) string { +func (l *genericResource) relPermalinkForRel(rel string, addBasePath bool) string { if l.link != nil { rel = l.link(rel) } if l.base != "" { rel = path.Join(l.base, rel) - if rel[0] != '/' { - rel = "/" + rel - } + } + + if addBasePath && l.spec.PathSpec.BasePath != "" { + rel = path.Join(l.spec.PathSpec.BasePath, rel) + } + + if rel[0] != '/' { + rel = "/" + rel } return l.spec.PathSpec.URLizeFilename(rel) @@ -262,11 +267,15 @@ func (l *genericResource) Publish() error { } defer f.Close() - target := filepath.Join(l.absPublishDir, l.RelPermalink()) + target := filepath.Join(l.absPublishDir, l.target()) return helpers.WriteToDisk(target, f, l.spec.Fs.Destination) } +func (l *genericResource) target() string { + return l.relPermalinkForRel(l.rel, false) +} + func (r *Spec) newGenericResource( linker func(base string) string, osFileInfo os.FileInfo, diff --git a/resource/resource_test.go b/resource/resource_test.go index 34d63cd60..847c26843 100644 --- a/resource/resource_test.go +++ b/resource/resource_test.go @@ -28,7 +28,7 @@ func TestGenericResource(t *testing.T) { r := spec.newGenericResource(nil, nil, "/public", "/a/foo.css", "foo.css", "css") assert.Equal("https://example.com/foo.css", r.Permalink()) - assert.Equal("foo.css", r.RelPermalink()) + assert.Equal("/foo.css", r.RelPermalink()) assert.Equal("css", r.ResourceType()) } @@ -60,7 +60,7 @@ func TestNewResourceFromFilename(t *testing.T) { assert.NoError(err) assert.NotNil(r) assert.Equal("image", r.ResourceType()) - assert.Equal("a/b/logo.png", r.RelPermalink()) + assert.Equal("/a/b/logo.png", r.RelPermalink()) assert.Equal("https://example.com/a/b/logo.png", r.Permalink()) r, err = spec.NewResourceFromFilename(nil, "/public", "/root/a/b/data.json", "a/b/data.json") @@ -74,6 +74,25 @@ func TestNewResourceFromFilename(t *testing.T) { assert.Equal("/aceof/a/b/data.json", cloned.RelPermalink()) } +func TestNewResourceFromFilenameSubPathInBaseURL(t *testing.T) { + assert := require.New(t) + spec := newTestResourceSpecForBaseURL(assert, "https://example.com/docs") + + writeSource(t, spec.Fs, "/project/a/b/logo.png", "image") + + r, err := spec.NewResourceFromFilename(nil, "/public", + filepath.FromSlash("/project/a/b/logo.png"), filepath.FromSlash("a/b/logo.png")) + + assert.NoError(err) + assert.NotNil(r) + assert.Equal("image", r.ResourceType()) + assert.Equal("/docs/a/b/logo.png", r.RelPermalink()) + assert.Equal("https://example.com/docs/a/b/logo.png", r.Permalink()) + img := r.(*Image) + assert.Equal("/a/b/logo.png", img.target()) + +} + func TestResourcesByType(t *testing.T) { assert := require.New(t) spec := newTestResourceSpec(assert) @@ -99,10 +118,10 @@ func TestResourcesGetByPrefix(t *testing.T) { spec.newGenericResource(nil, nil, "/public", "/b/foo3.css", "foo3.css", "css")} assert.Nil(resources.GetByPrefix("asdf")) - assert.Equal("logo1.png", resources.GetByPrefix("logo").RelPermalink()) - assert.Equal("foo2.css", resources.GetByPrefix("foo2").RelPermalink()) - assert.Equal("foo1.css", resources.GetByPrefix("foo1").RelPermalink()) - assert.Equal("foo1.css", resources.GetByPrefix("foo1").RelPermalink()) + assert.Equal("/logo1.png", resources.GetByPrefix("logo").RelPermalink()) + assert.Equal("/foo2.css", resources.GetByPrefix("foo2").RelPermalink()) + assert.Equal("/foo1.css", resources.GetByPrefix("foo1").RelPermalink()) + assert.Equal("/foo1.css", resources.GetByPrefix("foo1").RelPermalink()) assert.Nil(resources.GetByPrefix("asdfasdf")) } diff --git a/resource/testhelpers_test.go b/resource/testhelpers_test.go index 7ab210688..668885682 100644 --- a/resource/testhelpers_test.go +++ b/resource/testhelpers_test.go @@ -18,12 +18,17 @@ import ( ) func newTestResourceSpec(assert *require.Assertions) *Spec { + return newTestResourceSpecForBaseURL(assert, "https://example.com/") +} + +func newTestResourceSpecForBaseURL(assert *require.Assertions, baseURL string) *Spec { cfg := viper.New() - cfg.Set("baseURL", "https://example.com/") + cfg.Set("baseURL", baseURL) cfg.Set("resourceDir", "/res") fs := hugofs.NewMem(cfg) s, err := helpers.NewPathSpec(fs, cfg) + assert.NoError(err) spec, err := NewSpec(s, media.DefaultTypes) |