2017-08-14 23:36:22 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/interfaces"
|
|
|
|
"github.com/elyby/chrly/model"
|
2017-08-14 23:36:22 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type FilesystemFactory struct {
|
|
|
|
BasePath string
|
|
|
|
CapesDirName string
|
|
|
|
}
|
|
|
|
|
2017-08-18 03:20:23 +05:30
|
|
|
func (f FilesystemFactory) CreateSkinsRepository() (interfaces.SkinsRepository, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
panic("skins repository not supported for this storage type")
|
|
|
|
}
|
|
|
|
|
2017-08-18 03:20:23 +05:30
|
|
|
func (f FilesystemFactory) CreateCapesRepository() (interfaces.CapesRepository, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
if err := f.validateFactoryConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filesStorage{path: path.Join(f.BasePath, f.CapesDirName)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FilesystemFactory) validateFactoryConfig() error {
|
|
|
|
if f.BasePath == "" {
|
|
|
|
return &ParamRequired{"basePath"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.CapesDirName == "" {
|
|
|
|
f.CapesDirName = "capes"
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type filesStorage struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
func (repository *filesStorage) FindByUsername(username string) (*model.Cape, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
if username == "" {
|
2017-08-20 03:52:42 +05:30
|
|
|
return nil, &CapeNotFoundError{username}
|
2017-08-14 23:36:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
capePath := path.Join(repository.path, strings.ToLower(username) + ".png")
|
|
|
|
file, err := os.Open(capePath)
|
|
|
|
if err != nil {
|
2017-08-20 03:52:42 +05:30
|
|
|
return nil, &CapeNotFoundError{username}
|
2017-08-14 23:36:22 +05:30
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
return &model.Cape{
|
|
|
|
File: file,
|
|
|
|
}, nil
|
2017-08-14 23:36:22 +05:30
|
|
|
}
|