🏷️ Removed bytes memory in Resource domain due stack overflow

This commit is contained in:
Maxim Lebedev 2023-11-19 13:41:53 +06:00
parent 22422d0ad2
commit c8c5a89269
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 4 additions and 27 deletions

View File

@ -1,7 +1,6 @@
package domain
import (
"bytes"
"image"
_ "image/gif"
_ "image/jpeg"
@ -19,7 +18,6 @@ type Resource struct {
File File
modTime time.Time
reader io.ReadSeeker
params map[string]any // TODO(toby3d): set from Page configuration
mediaType MediaType
key string
@ -29,7 +27,7 @@ type Resource struct {
image image.Config
}
func NewResource(modTime time.Time, content []byte, key string) *Resource {
func NewResource(modTime time.Time, r io.Reader, key string) *Resource {
mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key)))
out := &Resource{
File: NewFile(key),
@ -39,12 +37,11 @@ func NewResource(modTime time.Time, content []byte, key string) *Resource {
title: "", // TODO(toby3d): set from Page configuration
params: make(map[string]any), // TODO(toby3d): set from Page configuration
mediaType: NewMediaType(mediaType),
reader: bytes.NewReader(content),
}
switch path.Ext(key) {
default:
out.resourceType, _ = ParseResourceType(out.mediaType.mainType)
out.resourceType = ResourceType(out.mediaType.mainType)
case ".md":
out.resourceType = ResourceTypePage
case ".webmanifest":
@ -53,7 +50,7 @@ func NewResource(modTime time.Time, content []byte, key string) *Resource {
switch out.resourceType {
case ResourceTypeImage:
out.image, _, _ = image.DecodeConfig(out.reader)
out.image, _, _ = image.DecodeConfig(r)
}
return out
@ -85,20 +82,6 @@ func (r Resource) ResourceType() ResourceType {
return r.resourceType
}
func (r Resource) Content() []byte {
content, _ := io.ReadAll(r.reader)
return content
}
func (r Resource) Read(p []byte) (int, error) {
return r.reader.Read(p)
}
func (r Resource) Seek(offset int64, whence int) (int64, error) {
return r.reader.Seek(offset, whence)
}
func (f Resource) GoString() string {
return "domain.Resource(" + f.key + ")"
}

View File

@ -6,7 +6,6 @@ import (
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"io/fs"
_ "golang.org/x/image/bmp"
@ -38,12 +37,7 @@ func (repo *fileServerResourceRepository) Get(ctx context.Context, p string) (*d
}
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("cannot read resource content on path '%s': %w", p, err)
}
return domain.NewResource(info.ModTime(), content, p), nil
return domain.NewResource(info.ModTime(), f, p), nil
}
func (repo *fileServerResourceRepository) Fetch(ctx context.Context, pattern string) (domain.Resources, int, error) {