Add resources.Copy

Implemented by most Resource objects, but not Page (for now).

Fixes #9313
This commit is contained in:
Bjørn Erik Pedersen
2022-05-24 09:34:36 +02:00
parent 6f7fbe03b1
commit cd0112a05a
8 changed files with 169 additions and 13 deletions

View File

@@ -134,7 +134,7 @@ func (i *imageResource) getExif() *exif.ExifInfo {
return i.meta.Exif
}
// Cloneis for internal use.
// Clone is for internal use.
func (i *imageResource) Clone() resource.Resource {
gr := i.baseResource.Clone().(baseResource)
return &imageResource{
@@ -144,6 +144,15 @@ func (i *imageResource) Clone() resource.Resource {
}
}
func (i *imageResource) cloneTo(targetPath string) resource.Resource {
gr := i.baseResource.cloneTo(targetPath).(baseResource)
return &imageResource{
root: i.root,
Image: i.WithSpec(gr),
baseResource: gr,
}
}
func (i *imageResource) cloneWithUpdates(u *transformationUpdate) (baseResource, error) {
base, err := i.baseResource.cloneWithUpdates(u)
if err != nil {

View File

@@ -1,4 +1,4 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
// Copyright 2022 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.
@@ -120,8 +120,19 @@ func (t transformerNotAvailable) Key() internal.ResourceTransformationKey {
return t.key
}
// resourceCopier is for internal use.
type resourceCopier interface {
cloneTo(targetPath string) resource.Resource
}
// Copy copies r to the targetPath given.
func Copy(r resource.Resource, targetPath string) resource.Resource {
return r.(resourceCopier).cloneTo(targetPath)
}
type baseResourceResource interface {
resource.Cloner
resourceCopier
resource.ContentProvider
resource.Resource
resource.Identifier
@@ -225,6 +236,20 @@ func (l *genericResource) Clone() resource.Resource {
return l.clone()
}
func (l *genericResource) cloneTo(targetPath string) resource.Resource {
c := l.clone()
targetPath = helpers.ToSlashTrimLeading(targetPath)
dir, file := path.Split(targetPath)
c.resourcePathDescriptor = &resourcePathDescriptor{
relTargetDirFile: dirFile{dir: dir, file: file},
}
return c
}
func (l *genericResource) Content() (any, error) {
if err := l.initContent(); err != nil {
return nil, err

View File

@@ -26,8 +26,7 @@ var (
_ ResourceError = (*resourceError)(nil)
)
// Cloner is an internal template and not meant for use in the templates. It
// may change without notice.
// Cloner is for internal use.
type Cloner interface {
Clone() Resource
}

View File

@@ -51,6 +51,13 @@ func New(rs *resources.Spec) *Client {
}
}
// Copy copies r to the new targetPath.
func (c *Client) Copy(r resource.Resource, targetPath string) (resource.Resource, error) {
return c.rs.ResourceCache.GetOrCreate(resources.ResourceCacheKey(targetPath), func() (resource.Resource, error) {
return resources.Copy(r, targetPath), nil
})
}
// Get creates a new Resource by opening the given filename in the assets filesystem.
func (c *Client) Get(filename string) (resource.Resource, error) {
filename = filepath.Clean(filename)

View File

@@ -42,6 +42,7 @@ import (
var (
_ resource.ContentResource = (*resourceAdapter)(nil)
_ resourceCopier = (*resourceAdapter)(nil)
_ resource.ReadSeekCloserResource = (*resourceAdapter)(nil)
_ resource.Resource = (*resourceAdapter)(nil)
_ resource.Source = (*resourceAdapter)(nil)
@@ -175,6 +176,19 @@ func (r *resourceAdapter) Data() any {
return r.target.Data()
}
func (r resourceAdapter) cloneTo(targetPath string) resource.Resource {
newtTarget := r.target.cloneTo(targetPath)
newInner := &resourceAdapterInner{
spec: r.spec,
target: newtTarget.(transformableResource),
}
if r.resourceAdapterInner.publishOnce != nil {
newInner.publishOnce = &publishOnce{}
}
r.resourceAdapterInner = newInner
return &r
}
func (r *resourceAdapter) Crop(spec string) (images.ImageResource, error) {
return r.getImageOps().Crop(spec)
}
@@ -596,6 +610,7 @@ type transformableResource interface {
resource.ContentProvider
resource.Resource
resource.Identifier
resourceCopier
}
type transformationUpdate struct {