diff options
Diffstat (limited to 'resources/resource.go')
-rw-r--r-- | resources/resource.go | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/resources/resource.go b/resources/resource.go index c120a8dd0..236ba8ac6 100644 --- a/resources/resource.go +++ b/resources/resource.go @@ -133,9 +133,13 @@ type ResourceSourceDescriptor struct { SourceFile source.File OpenReadSeekCloser resource.OpenReadSeekCloser + FileInfo os.FileInfo + // If OpenReadSeekerCloser is not set, we use this to open the file. SourceFilename string + Fs afero.Fs + // The relative target filename without any language code. RelTargetFilename string @@ -157,19 +161,11 @@ func (r ResourceSourceDescriptor) Filename() string { return r.SourceFilename } -func (r *Spec) sourceFs() afero.Fs { - return r.PathSpec.BaseFs.Content.Fs -} - func (r *Spec) New(fd ResourceSourceDescriptor) (resource.Resource, error) { - return r.newResourceForFs(r.sourceFs(), fd) -} - -func (r *Spec) NewForFs(sourceFs afero.Fs, fd ResourceSourceDescriptor) (resource.Resource, error) { - return r.newResourceForFs(sourceFs, fd) + return r.newResourceFor(fd) } -func (r *Spec) newResourceForFs(sourceFs afero.Fs, fd ResourceSourceDescriptor) (resource.Resource, error) { +func (r *Spec) newResourceFor(fd ResourceSourceDescriptor) (resource.Resource, error) { if fd.OpenReadSeekCloser == nil { if fd.SourceFile != nil && fd.SourceFilename != "" { return nil, errors.New("both SourceFile and AbsSourceFilename provided") @@ -187,15 +183,14 @@ func (r *Spec) newResourceForFs(sourceFs afero.Fs, fd ResourceSourceDescriptor) fd.TargetBasePaths = r.MultihostTargetBasePaths } - return r.newResource(sourceFs, fd) + return r.newResource(fd.Fs, fd) } func (r *Spec) newResource(sourceFs afero.Fs, fd ResourceSourceDescriptor) (resource.Resource, error) { - var fi os.FileInfo + fi := fd.FileInfo var sourceFilename string if fd.OpenReadSeekCloser != nil { - } else if fd.SourceFilename != "" { var err error fi, err = sourceFs.Stat(fd.SourceFilename) @@ -207,7 +202,6 @@ func (r *Spec) newResource(sourceFs afero.Fs, fd ResourceSourceDescriptor) (reso } sourceFilename = fd.SourceFilename } else { - fi = fd.SourceFile.FileInfo() sourceFilename = fd.SourceFile.Filename() } @@ -245,8 +239,6 @@ func (r *Spec) newResource(sourceFs afero.Fs, fd ResourceSourceDescriptor) (reso mimeType) if mimeType.MainType == "image" { - ext := strings.ToLower(helpers.Ext(sourceFilename)) - imgFormat, ok := imageFormats[ext] if !ok { // This allows SVG etc. to be used as resources. They will not have the methods of the Image, but @@ -376,7 +368,7 @@ type genericResource struct { // This may be set to tell us to look in another filesystem for this resource. // We, by default, use the sourceFs filesystem in the spec below. - overriddenSourceFs afero.Fs + sourceFs afero.Fs spec *Spec @@ -411,7 +403,8 @@ func (l *genericResource) ReadSeekCloser() (hugio.ReadSeekCloser, error) { if l.openReadSeekerCloser != nil { return l.openReadSeekerCloser() } - f, err := l.sourceFs().Open(l.sourceFilename) + + f, err := l.getSourceFs().Open(l.sourceFilename) if err != nil { return nil, err } @@ -497,11 +490,8 @@ func (l *genericResource) initContent() error { return err } -func (l *genericResource) sourceFs() afero.Fs { - if l.overriddenSourceFs != nil { - return l.overriddenSourceFs - } - return l.spec.sourceFs() +func (l *genericResource) getSourceFs() afero.Fs { + return l.sourceFs } func (l *genericResource) publishIfNeeded() { @@ -711,6 +701,10 @@ func (r *Spec) newGenericResourceWithBase( baseFilename string, mediaType media.Type) *genericResource { + if osFileInfo != nil && osFileInfo.IsDir() { + panic(fmt.Sprintf("dirs nto supported resource types: %v", osFileInfo)) + } + // This value is used both to construct URLs and file paths, but start // with a Unix-styled path. baseFilename = helpers.ToSlashTrimLeading(baseFilename) @@ -724,7 +718,7 @@ func (r *Spec) newGenericResourceWithBase( } pathDescriptor := resourcePathDescriptor{ - baseTargetPathDirs: helpers.UniqueStrings(targetPathBaseDirs), + baseTargetPathDirs: helpers.UniqueStringsReuse(targetPathBaseDirs), targetPathBuilder: targetPathBuilder, relTargetDirFile: dirFile{dir: fpath, file: fname}, } @@ -738,7 +732,7 @@ func (r *Spec) newGenericResourceWithBase( openReadSeekerCloser: openReadSeekerCloser, publishOnce: po, resourcePathDescriptor: pathDescriptor, - overriddenSourceFs: sourceFs, + sourceFs: sourceFs, osFileInfo: osFileInfo, sourceFilename: sourceFilename, mediaType: mediaType, |