🐛 Fixed page resource loading

This commit is contained in:
Maxim Lebedev 2024-02-14 20:48:31 +06:00
parent 40c3565173
commit fd8140a86c
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 14 additions and 7 deletions

View File

@ -152,7 +152,9 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
// NOTE(toby3d): client request '/:lang/...', try to
// understand which language code in subdir is requested.
lang = domain.NewLanguage(head)
if lang = domain.NewLanguage(head); lang != domain.LanguageUnd {
r.URL.Path = tail
}
// NOTE(toby3d): get localized site config for requested
// subdir if exists.
@ -165,9 +167,9 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
if res, err := resourcer.Do(r.Context(), r.URL.Path); err == nil {
// TODO(toby3d) : ugly workaround, must be refactored
resFile, err := contentDir.Open(res.File.Path())
resFile, err := contentDir.Open(res.File.Filename())
if err != nil {
http.Error(w, "cannot open: "+err.Error(), http.StatusInternalServerError)
http.Error(w, "cannot open resource: "+err.Error(), http.StatusInternalServerError)
return
}
@ -175,7 +177,7 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
resBytes, err := io.ReadAll(resFile)
if err != nil {
http.Error(w, "cannot read all: "+err.Error(), http.StatusInternalServerError)
http.Error(w, "cannot read resource: "+err.Error(), http.StatusInternalServerError)
return
}

View File

@ -2,6 +2,7 @@ package resource
import (
"context"
"errors"
"source.toby3d.me/toby3d/home/internal/domain"
)
@ -13,3 +14,5 @@ type Repository interface {
// Fetch returns all resources from dir recursevly.
Fetch(ctx context.Context, pattern string) (domain.Resources, int, error)
}
var ErrIsDir error = errors.New("resource is a directory")

View File

@ -25,6 +25,10 @@ func (repo *fileServerResourceRepository) Get(ctx context.Context, p string) (*d
return nil, fmt.Errorf("cannot stat resource on path '%s': %w", p, err)
}
if info.IsDir() {
return nil, fmt.Errorf("cannot open resource on path '%s': %w", p, resource.ErrIsDir)
}
f, err := repo.root.Open(p)
if err != nil {
return nil, fmt.Errorf("cannot open resource on path '%s': %w", p, err)

View File

@ -21,9 +21,7 @@ func NewResourceUseCase(resources resource.Repository) resource.UseCase {
}
func (ucase *resourceUseCase) Do(ctx context.Context, p string) (*domain.Resource, error) {
p = strings.TrimPrefix(path.Clean(p), "/")
r, err := ucase.resources.Get(ctx, p)
r, err := ucase.resources.Get(ctx, strings.TrimPrefix(path.Clean(p), "/"))
if err != nil {
return nil, fmt.Errorf("cannot get resource file: %w", err)
}