-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
162 lines (131 loc) · 3.52 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package entity
import (
"errors"
"github.com/gohugonet/hugoverse/internal/domain/resources"
"github.com/gohugonet/hugoverse/internal/domain/resources/valueobject"
"github.com/gohugonet/hugoverse/pkg/cache/stale"
"github.com/gohugonet/hugoverse/pkg/helpers"
pio "github.com/gohugonet/hugoverse/pkg/io"
"github.com/gohugonet/hugoverse/pkg/maps"
"github.com/gohugonet/hugoverse/pkg/media"
"github.com/gohugonet/hugoverse/pkg/paths"
"github.com/spf13/afero"
"io"
"strings"
)
var (
_ resources.Resource = (*Resource)(nil)
_ resources.ReadSeekCloserResource = (*Resource)(nil)
_ hashProvider = (*Resource)(nil)
)
type Resource struct {
stale.Staler
h *valueobject.ResourceHash // A hash of the source content. Is only calculated in caching situations.
title string
name string
params map[string]any
sd valueobject.ResourceSourceDescriptor
paths valueobject.ResourcePaths
publishFs afero.Fs
}
func (l *Resource) MediaType() media.Type {
return l.sd.MediaType
}
func (l *Resource) ResourceType() string {
return l.MediaType().MainType
}
func (l *Resource) RelPermalink() string {
return l.paths.BasePathNoTrailingSlash + paths.PathEscape(l.paths.TargetLink())
}
func (l *Resource) Permalink() string {
return l.paths.BasePathNoTrailingSlash + paths.PathEscape(l.paths.TargetPath())
}
func (l *Resource) Name() string {
return l.name
}
func (l *Resource) Title() string {
return l.title
}
func (l *Resource) Params() maps.Params {
return l.params
}
func (l *Resource) Data() any {
return l.sd.Data
}
func (l *Resource) Err() resources.ResourceError {
return nil
}
func (l *Resource) ReadSeekCloser() (pio.ReadSeekCloser, error) {
return l.sd.OpenReadSeekCloser()
}
func (l *Resource) Hash() string {
if err := l.h.Setup(l); err != nil {
panic(err)
}
return l.h.Value
}
func (l *Resource) Size() int64 {
l.Hash()
return l.h.Size
}
func (l *Resource) CloneTo(targetPath string) resources.Resource {
c := l.clone()
c.paths = c.paths.FromTargetPath(targetPath)
return c
}
func (l *Resource) clone() *Resource {
clone := *l
return &clone
}
func (l *Resource) Key() string {
basePath := l.paths.BasePathNoTrailingSlash
var key string
if basePath == "" {
key = l.RelPermalink()
} else {
key = strings.TrimPrefix(l.RelPermalink(), basePath)
}
return key
}
func (l *Resource) openPublishFileForWriting(relTargetPath string) (io.WriteCloser, error) {
filenames := l.paths.FromTargetPath(relTargetPath).TargetFilenames()
return helpers.OpenFilesForWriting(l.publishFs, filenames...)
}
func (l *Resource) cloneWithUpdates(u *valueobject.TransformationUpdate) (*Resource, error) {
r := l.clone()
if u.Content != nil {
r.sd.OpenReadSeekCloser = func() (pio.ReadSeekCloser, error) {
return pio.NewReadSeekerNoOpCloserFromString(*u.Content), nil
}
}
r.sd.MediaType = u.MediaType
if u.SourceFilename != nil {
if u.SourceFs == nil {
return nil, errors.New("sourceFs is nil")
}
r.sd.OpenReadSeekCloser = func() (pio.ReadSeekCloser, error) {
return u.SourceFs.Open(*u.SourceFilename)
}
} else if u.SourceFs != nil {
return nil, errors.New("sourceFs is set without sourceFilename")
}
if u.TargetPath == "" {
return nil, errors.New("missing targetPath")
}
r.paths = r.paths.FromTargetPath(u.TargetPath)
r.mergeData(u.Data)
return r, nil
}
func (l *Resource) mergeData(in map[string]any) {
if len(in) == 0 {
return
}
if l.sd.Data == nil {
l.sd.Data = make(map[string]any)
}
for k, v := range in {
if _, found := l.sd.Data[k]; !found {
l.sd.Data[k] = v
}
}
}