🚚 Renamed File and Files into Resource and Resources

This commit is contained in:
Maxim Lebedev 2023-11-10 04:34:37 +06:00
parent 11682b45a7
commit 7557c73e55
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
12 changed files with 51 additions and 49 deletions

View File

@ -3,9 +3,9 @@ package domain
import "golang.org/x/text/language"
type Page struct {
Language language.Tag
Params map[string]any
Title string
Content []byte
Files Files
Language language.Tag
Params map[string]any
Title string
Content []byte
Resources Resources
}

View File

@ -7,7 +7,7 @@ import (
"time"
)
type File struct {
type Resource struct {
Path string
Updated time.Time
Content []byte
@ -18,19 +18,19 @@ type File struct {
}
// LogicalName returns full file name without directory path.
func (f File) LogicalName() string {
func (f Resource) LogicalName() string {
return filepath.Base(f.Path)
}
// BaseFileName returns file name without extention and directory path.
func (f File) BaseFileName() string {
func (f Resource) BaseFileName() string {
base := filepath.Base(f.Path)
return strings.TrimSuffix(base, filepath.Ext(base))
}
// Ext returns file extention.
func (f File) Ext() string {
func (f Resource) Ext() string {
if ext := filepath.Ext(f.Path); len(ext) > 1 {
return ext[1:]
}
@ -39,14 +39,14 @@ func (f File) Ext() string {
}
// Dir returns file directory.
func (f File) Dir() string {
func (f Resource) Dir() string {
return filepath.Dir(f.Path)
}
func (f File) MediaType() string {
func (f Resource) MediaType() string {
return mime.TypeByExtension(filepath.Ext(f.Path))
}
func (f File) GoString() string {
return "domain.File(" + f.Path + ")"
func (f Resource) GoString() string {
return "domain.Resource(" + f.Path + ")"
}

View File

@ -3,9 +3,9 @@ package domain
import "path"
// TODO(toby3d): search by type or name/id.
type Files []*File
type Resources []*Resource
func (f Files) GetMatch(pattern string) *File {
func (f Resources) GetMatch(pattern string) *Resource {
for i := range f {
if matched, err := path.Match(pattern, f[i].Path); err != nil || !matched {
continue

View File

@ -8,10 +8,10 @@ import (
)
type Site struct {
Language language.Tag
BaseURL *url.URL
TimeZone *time.Location
Params map[string]any
Title string
Files Files
Language language.Tag
BaseURL *url.URL
TimeZone *time.Location
Params map[string]any
Title string
Resources Resources
}

View File

@ -57,10 +57,10 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang language.Tag
}
return &domain.Page{
Language: lang,
Title: data.Title,
Content: data.Content,
Params: data.Params,
Files: make([]*domain.File, 0),
Language: lang,
Title: data.Title,
Content: data.Content,
Params: data.Params,
Resources: make([]*domain.Resource, 0),
}, nil
}

View File

@ -33,23 +33,23 @@ func TestGet(t *testing.T) {
}{
"index": {
input: path.Join("index"),
expect: &domain.Page{Content: []byte("index.md"), Files: make([]*domain.File, 0)},
expect: &domain.Page{Content: []byte("index.md"), Resources: make([]*domain.Resource, 0)},
},
"file": {
input: path.Join("file"),
expect: &domain.Page{Content: []byte("file.md"), Files: make([]*domain.File, 0)},
expect: &domain.Page{Content: []byte("file.md"), Resources: make([]*domain.Resource, 0)},
},
"folder": {
input: path.Join("folder", "index"),
expect: &domain.Page{Content: []byte("folder/index.md"), Files: make([]*domain.File, 0)},
expect: &domain.Page{Content: []byte("folder/index.md"), Resources: make([]*domain.Resource, 0)},
},
"both-file": {
input: path.Join("both"),
expect: &domain.Page{Content: []byte("both.md"), Files: make([]*domain.File, 0)},
expect: &domain.Page{Content: []byte("both.md"), Resources: make([]*domain.Resource, 0)},
},
"both-folder": {
input: path.Join("both", "index"),
expect: &domain.Page{Content: []byte("both/index.md"), Files: make([]*domain.File, 0)},
expect: &domain.Page{Content: []byte("both/index.md"), Resources: make([]*domain.Resource, 0)},
},
} {
name, tc := name, tc

View File

@ -50,16 +50,16 @@ func (ucase *pageUseCase) Do(ctx context.Context, lang language.Tag, p string) (
continue
}
if out.Files, _, err = ucase.statics.Fetch(ctx, path.Dir(targets[i])); err != nil {
if out.Resources, _, err = ucase.statics.Fetch(ctx, path.Dir(targets[i])); err != nil {
return out, nil
}
for j := 0; j < len(out.Files); j++ {
if ext := out.Files[j].Ext(); ext != "html" && ext != "md" {
for j := 0; j < len(out.Resources); j++ {
if ext := out.Resources[j].Ext(); ext != "html" && ext != "md" {
continue
}
out.Files = slices.Delete(out.Files, j, j+1)
out.Resources = slices.Delete(out.Resources, j, j+1)
j--
}

View File

@ -30,13 +30,13 @@ func (ucase *siteUseCase) Do(ctx context.Context, lang language.Tag) (*domain.Si
return nil, fmt.Errorf("cannot find base site data: %w", err)
}
if out.Files, _, err = ucase.statics.Fetch(ctx, "."); err == nil {
for i := 0; i < len(out.Files); i++ {
if ext := out.Files[i].Ext(); ext != "html" && ext != "md" {
if out.Resources, _, err = ucase.statics.Fetch(ctx, "."); err == nil {
for i := 0; i < len(out.Resources); i++ {
if ext := out.Resources[i].Ext(); ext != "html" && ext != "md" {
continue
}
out.Files = slices.Delete(out.Files, i, i+1)
out.Resources = slices.Delete(out.Resources, i, i+1)
i--
}

View File

@ -8,8 +8,8 @@ import (
type (
Repository interface {
Get(ctx context.Context, path string) (*domain.File, error)
Fetch(ctx context.Context, dir string) (domain.Files, int, error)
Get(ctx context.Context, path string) (*domain.Resource, error)
Fetch(ctx context.Context, dir string) (domain.Resources, int, error)
}
dummyRepository struct{}
@ -19,8 +19,10 @@ func NewDummyRepository() dummyRepository {
return dummyRepository{}
}
func (dummyRepository) Get(ctx context.Context, path string) (*domain.File, error) { return nil, nil }
func (dummyRepository) Get(ctx context.Context, path string) (*domain.Resource, error) {
return nil, nil
}
func (dummyRepository) Fetch(ctx context.Context, dir string) (domain.Files, int, error) {
func (dummyRepository) Fetch(ctx context.Context, dir string) (domain.Resources, int, error) {
return nil, 0, nil
}

View File

@ -30,7 +30,7 @@ func NewFileServerStaticRepository(root fs.FS) static.Repository {
}
}
func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*domain.File, error) {
func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*domain.Resource, error) {
info, err := fs.Stat(repo.root, p)
if err != nil {
return nil, fmt.Errorf("cannot read static info on path '%s': %w", p, err)
@ -47,7 +47,7 @@ func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*dom
return nil, fmt.Errorf("cannot read static content on path '%s': %w", p, err)
}
out := &domain.File{
out := &domain.Resource{
Path: p,
Updated: info.ModTime(),
Content: content,
@ -68,13 +68,13 @@ func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*dom
return out, nil
}
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, d string) (domain.Files, int, error) {
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, d string) (domain.Resources, int, error) {
entries, err := fs.ReadDir(repo.root, d)
if err != nil {
return nil, 0, fmt.Errorf("cannot read directory on path '%s': %w", d, err)
}
out := make(domain.Files, 0, len(entries))
out := make(domain.Resources, 0, len(entries))
for _, entry := range entries {
if entry.IsDir() {

View File

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

View File

@ -21,7 +21,7 @@ func NewStaticUseCase(statics static.Repository) static.UseCase {
}
}
func (ucase *staticUseCase) Do(ctx context.Context, p string) (*domain.File, error) {
func (ucase *staticUseCase) Do(ctx context.Context, p string) (*domain.Resource, error) {
p = strings.TrimPrefix(path.Clean(p), "/")
if ext := path.Ext(p); ext == ".html" || ext == ".md" {