2017-08-14 23:36:22 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"elyby/minecraft-skinsystem/model"
|
2017-08-18 03:20:23 +05:30
|
|
|
"elyby/minecraft-skinsystem/interfaces"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository *filesStorage) FindByUsername(username string) (model.Cape, error) {
|
|
|
|
var record model.Cape
|
|
|
|
if username == "" {
|
|
|
|
return record, &CapeNotFoundError{username}
|
|
|
|
}
|
|
|
|
|
|
|
|
capePath := path.Join(repository.path, strings.ToLower(username) + ".png")
|
|
|
|
file, err := os.Open(capePath)
|
|
|
|
if err != nil {
|
|
|
|
return record, &CapeNotFoundError{username}
|
|
|
|
}
|
|
|
|
|
|
|
|
record.File = file
|
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|