49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package fs
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
_ "image/gif"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"io"
|
|
"io/fs"
|
|
|
|
_ "golang.org/x/image/bmp"
|
|
_ "golang.org/x/image/webp"
|
|
|
|
"source.toby3d.me/toby3d/home/internal/domain"
|
|
"source.toby3d.me/toby3d/home/internal/static"
|
|
)
|
|
|
|
type fileServerStaticRepository struct {
|
|
root fs.FS
|
|
}
|
|
|
|
func NewFileServerStaticRepository(root fs.FS) static.Repository {
|
|
return &fileServerStaticRepository{
|
|
root: root,
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
f, err := repo.root.Open(p)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot open static on path '%s': %w", p, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
content, err := io.ReadAll(f)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot copy opened '%s' static contents into buffer: %w", p, err)
|
|
}
|
|
|
|
return domain.NewStatic(bytes.NewReader(content), info.ModTime(), info.Name()), nil
|
|
}
|