♻️ Simplify Page translations searching by using Resource.File

This commit is contained in:
Maxim Lebedev 2023-11-13 05:34:21 +06:00
parent f3eca3dc73
commit 2ad44a03bc
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 5 additions and 14 deletions

View File

@ -16,6 +16,8 @@ import (
)
type Resource struct {
File File
modTime time.Time
reader io.ReadSeeker
params map[string]any // TODO(toby3d): set from Page configuration
@ -30,6 +32,7 @@ type Resource struct {
func NewResource(modTime time.Time, content []byte, key string) *Resource {
mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key)))
out := &Resource{
File: NewFile(key),
modTime: modTime,
key: key,
name: key, // TODO(toby3d): set from Page configuration

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"path"
"strings"
"source.toby3d.me/toby3d/home/internal/domain"
"source.toby3d.me/toby3d/home/internal/page"
@ -53,22 +52,11 @@ func (ucase *pageUseCase) Do(ctx context.Context, lang domain.Language, p string
}
for _, res := range out.Resources.GetType(domain.ResourceTypePage) {
// TODO(toby3d): simplify this, it's awful
resName := path.Base(res.Key())
resExt := path.Ext(resName)
resParts := strings.Split(resName[:len(resName)-len(resExt)], ".")
translationBaseName := strings.Join(resParts[:len(resParts)-1], ".")
if translationBaseName != out.File.TranslationBaseName() {
if res.File.TranslationBaseName() != out.File.TranslationBaseName() {
continue
}
resLang := domain.NewLanguage(resParts[len(resParts)-1])
if resLang == domain.LanguageUnd {
continue
}
translation, err := ucase.pages.Get(ctx, resLang, targets[i])
translation, err := ucase.pages.Get(ctx, res.File.Language, targets[i])
if err != nil {
continue
}