2017-08-14 23:36:22 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2020-01-02 02:12:45 +05:30
|
|
|
"github.com/elyby/chrly/http"
|
2017-08-14 23:36:22 +05:30
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/model"
|
2019-11-21 04:03:13 +05:30
|
|
|
"github.com/elyby/chrly/mojangtextures"
|
2017-08-14 23:36:22 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type FilesystemFactory struct {
|
2019-04-25 04:53:10 +05:30
|
|
|
BasePath string
|
2017-08-14 23:36:22 +05:30
|
|
|
CapesDirName string
|
|
|
|
}
|
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
func (f FilesystemFactory) CreateSkinsRepository() (http.SkinsRepository, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
panic("skins repository not supported for this storage type")
|
|
|
|
}
|
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
func (f FilesystemFactory) CreateCapesRepository() (http.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
|
|
|
|
}
|
|
|
|
|
2019-11-21 04:03:13 +05:30
|
|
|
func (f FilesystemFactory) CreateMojangUuidsRepository() (mojangtextures.UuidsStorage, error) {
|
2019-04-25 04:53:10 +05:30
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2017-08-14 23:36:22 +05:30
|
|
|
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 == "" {
|
2020-01-05 23:09:17 +05:30
|
|
|
return nil, &http.CapeNotFoundError{Who: username}
|
2017-08-14 23:36:22 +05:30
|
|
|
}
|
|
|
|
|
2019-04-25 04:53:10 +05:30
|
|
|
capePath := path.Join(repository.path, strings.ToLower(username)+".png")
|
2017-08-14 23:36:22 +05:30
|
|
|
file, err := os.Open(capePath)
|
|
|
|
if err != nil {
|
2020-01-05 23:09:17 +05:30
|
|
|
return nil, &http.CapeNotFoundError{Who: 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
|
|
|
}
|