♻️ Replaced Resource on Static in static module contracts

This commit is contained in:
Maxim Lebedev 2023-11-19 13:51:45 +06:00
parent 3a2515c255
commit 62bc90fd3e
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 17 additions and 16 deletions

View File

@ -8,11 +8,11 @@ import (
type (
Repository interface {
// Get returns Resource on path if exists
Get(ctx context.Context, path string) (*domain.Resource, error)
// Get returns Static on path if exists
Get(ctx context.Context, path string) (*domain.Static, error)
// Fetch returns all resources from dir recursevly.
Fetch(ctx context.Context, pattern string) (domain.Resources, int, error)
Fetch(ctx context.Context, pattern string) ([]*domain.Static, int, error)
}
dummyRepository struct{}
@ -22,10 +22,10 @@ func NewDummyRepository() dummyRepository {
return dummyRepository{}
}
func (dummyRepository) Get(ctx context.Context, path string) (*domain.Resource, error) {
func (dummyRepository) Get(ctx context.Context, path string) (*domain.Static, error) {
return nil, nil
}
func (dummyRepository) Fetch(ctx context.Context, pattern string) (domain.Resources, int, error) {
func (dummyRepository) Fetch(ctx context.Context, pattern string) ([]*domain.Static, int, error) {
return nil, 0, nil
}

View File

@ -1,6 +1,7 @@
package fs
import (
"bytes"
"context"
"fmt"
_ "image/gif"
@ -26,7 +27,7 @@ func NewFileServerStaticRepository(root fs.FS) static.Repository {
}
}
func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*domain.Resource, error) {
func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*domain.Static, error) {
info, err := fs.Stat(repo.root, p)
if err != nil {
return nil, fmt.Errorf("cannot stat static on path '%s': %w", p, err)
@ -40,13 +41,13 @@ func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*dom
content, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("cannot read static content on path '%s': %w", p, err)
return nil, fmt.Errorf("cannot copy opened '%s' static contents into buffer: %w", p, err)
}
return domain.NewResource(info.ModTime(), content, p), nil
return domain.NewStatic(bytes.NewReader(content), info.ModTime(), info.Name()), nil
}
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, pattern string) (domain.Resources, int, error) {
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, pattern string) ([]*domain.Static, int, error) {
var (
err error
matches []string
@ -74,11 +75,11 @@ func (repo *fileServerStaticRepository) Fetch(ctx context.Context, pattern strin
}
}
out := make(domain.Resources, 0, len(matches))
out := make([]*domain.Static, 0, len(matches))
for i := range matches {
if r, err := repo.Get(ctx, matches[i]); err == nil {
out = append(out, r)
if s, err := repo.Get(ctx, matches[i]); err == nil {
out = append(out, s)
}
}

View File

@ -7,5 +7,5 @@ import (
)
type UseCase interface {
Do(ctx context.Context, path string) (*domain.Resource, error)
Do(ctx context.Context, path string) (*domain.Static, error)
}

View File

@ -20,13 +20,13 @@ func NewStaticUseCase(statics static.Repository) static.UseCase {
}
}
func (ucase *staticUseCase) Do(ctx context.Context, p string) (*domain.Resource, error) {
func (ucase *staticUseCase) Do(ctx context.Context, p string) (*domain.Static, error) {
p = strings.TrimPrefix(path.Clean(p), "/")
f, err := ucase.statics.Get(ctx, p)
s, err := ucase.statics.Get(ctx, p)
if err != nil {
return nil, fmt.Errorf("cannot get static file: %w", err)
}
return f, nil
return s, rs, nil
}