Implement basic skinstore plugin using filesystem
This commit is contained in:
		
							
								
								
									
										125
									
								
								core/skinstore.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								core/skinstore.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,125 @@
 | 
			
		||||
package core
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/ilyakaznacheev/cleanenv"
 | 
			
		||||
	"github.com/josephbmanley/OpenSkins-Common/datatypes"
 | 
			
		||||
	"os"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type standaloneConfig struct {
 | 
			
		||||
	SkinDirectory    string `yaml:"skin_directory" env:"SKIN_DIR" env-default:"skins"`
 | 
			
		||||
	WebserverDomain  string `yaml:"domain" env:"DOMAIN" env-default:"localhost"`
 | 
			
		||||
	WebserverSubpath string `yaml:"subpath" env:"SUBPATH" env-default:""`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const configFile = "standalone_config.yaml"
 | 
			
		||||
 | 
			
		||||
var config standaloneConfig
 | 
			
		||||
 | 
			
		||||
// SkinstoreStandalone is root of the skinstore plugin
 | 
			
		||||
type SkinstoreStandalone struct{}
 | 
			
		||||
 | 
			
		||||
// Initialize intializes the skinstore module
 | 
			
		||||
func (s *SkinstoreStandalone) Initialize() error {
 | 
			
		||||
 | 
			
		||||
	for _, w := range warnings {
 | 
			
		||||
		fmt.Printf("WARNING: %v\n", w)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if config file exists
 | 
			
		||||
	if _, err := os.Stat(configFile); err == nil {
 | 
			
		||||
 | 
			
		||||
		// Read config with config file & environment variables
 | 
			
		||||
		if err := cleanenv.ReadConfig(configFile, &config); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	} else if os.IsNotExist(err) {
 | 
			
		||||
 | 
			
		||||
		// Read config with only environment variables
 | 
			
		||||
		if err := cleanenv.ReadEnv(&config); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetSkin returns a skin object
 | 
			
		||||
func (s *SkinstoreStandalone) GetSkin(skinID string) (*datatypes.Skin, error) {
 | 
			
		||||
 | 
			
		||||
	skinPath := fmt.Sprintf("%v/%v", config.SkinDirectory, skinID)
 | 
			
		||||
 | 
			
		||||
	// Check if skin exists
 | 
			
		||||
	if _, err := os.Stat(skinPath); err != nil {
 | 
			
		||||
 | 
			
		||||
		// Catch not exist errors
 | 
			
		||||
		if os.IsNotExist(err) {
 | 
			
		||||
			return nil, nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	skin := datatypes.Skin{
 | 
			
		||||
		UID:      skinID,
 | 
			
		||||
		Name:     skinID,
 | 
			
		||||
		Location: fmt.Sprintf("%v/%v%v", config.WebserverDomain, config.WebserverSubpath, skinID),
 | 
			
		||||
		Metadata: map[string]string{},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &skin, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AddSkin returns a skin creates a new skin object
 | 
			
		||||
func (s *SkinstoreStandalone) AddSkin(skinID string, fileData []byte) error {
 | 
			
		||||
 | 
			
		||||
	skinPath := fmt.Sprintf("%v/%v", config.SkinDirectory, skinID)
 | 
			
		||||
 | 
			
		||||
	if _, err := os.Stat(skinPath); err != nil {
 | 
			
		||||
 | 
			
		||||
		// Check if error was not 404 error
 | 
			
		||||
		if !os.IsNotExist(err) {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Crate file if it didn't exist
 | 
			
		||||
		if _, err := os.Create(skinPath); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Open file with rw permissions
 | 
			
		||||
	file, err := os.OpenFile(skinPath, os.O_RDWR|os.O_CREATE, 0755)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Write data to file
 | 
			
		||||
	writer := bufio.NewWriter(file)
 | 
			
		||||
	if _, err = writer.Write(fileData); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteSkin deletes a skin object
 | 
			
		||||
func (s *SkinstoreStandalone) DeleteSkin(skinID string) error {
 | 
			
		||||
	skinPath := fmt.Sprintf("%v/%v", config.SkinDirectory, skinID)
 | 
			
		||||
 | 
			
		||||
	// Check if skin exists
 | 
			
		||||
	if _, err := os.Stat(skinPath); err != nil {
 | 
			
		||||
		// Check if error was not 404 error
 | 
			
		||||
		if !os.IsNotExist(err) {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return os.Remove(skinPath)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								core/warnings.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								core/warnings.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
			
		||||
package core
 | 
			
		||||
 | 
			
		||||
var warnings = []string{
 | 
			
		||||
	"Standalone plugin does not yet implement metadata feature",
 | 
			
		||||
	"Standalone plugin currently do not save a skin's name",
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
module github.com/josephbmanley/OpenSkins-Standalone-Plugin
 | 
			
		||||
 | 
			
		||||
go 1.13
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/cristalhq/aconfig v0.10.0
 | 
			
		||||
	github.com/cristalhq/aconfig/aconfigyaml v0.10.0
 | 
			
		||||
	github.com/ilyakaznacheev/cleanenv v1.2.5
 | 
			
		||||
	github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227061123-22199a5c0ab9
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										27
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 | 
			
		||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
			
		||||
github.com/cristalhq/aconfig v0.10.0-beta/go.mod h1:0ZBp7dUf0F2Jr7YbLjw8OVlAD0eeV2bU3NwmVgeUReo=
 | 
			
		||||
github.com/cristalhq/aconfig v0.10.0 h1:5dyXjRkUBDuueSHFYxlLdxpZrnzGHwlffhL16NiT+XQ=
 | 
			
		||||
github.com/cristalhq/aconfig v0.10.0/go.mod h1:0ZBp7dUf0F2Jr7YbLjw8OVlAD0eeV2bU3NwmVgeUReo=
 | 
			
		||||
github.com/cristalhq/aconfig/aconfigyaml v0.10.0 h1:6NuPBSbZ338FaF62pqDzFY/sBEfvBytgyJVS0feUAh0=
 | 
			
		||||
github.com/cristalhq/aconfig/aconfigyaml v0.10.0/go.mod h1:xzEaBQp+1noCICuXzwVAGj+IScIDi4GkLLeRrUhmi1o=
 | 
			
		||||
github.com/ilyakaznacheev/cleanenv v1.2.5 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM=
 | 
			
		||||
github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk=
 | 
			
		||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
 | 
			
		||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a h1:bW/ahOovqRP2r1Ynp4WXxrOCiHq1vqye8RFyYLIZ6hg=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227051624-01b75eb56779 h1:ArCBRyblBk/x0BGgNu6gJ7j7kNvn91K7OmpOHpHUwAs=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227051624-01b75eb56779/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227052902-2d6c9664ec1e h1:5nRhRlgg/IX2yp4R/NDG1ku65ldkl5/LKxZxg4UcWw4=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227052902-2d6c9664ec1e/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227060121-852c176c7219 h1:PHEi0kL7sC2ERY34grA1bsObqXRcZzMZVDisCB4xm3Q=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227060121-852c176c7219/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227061123-22199a5c0ab9 h1:rEfQZyiAKIKchefT4uUGojwLVPDEylORTOU1/l+VEQU=
 | 
			
		||||
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227061123-22199a5c0ab9/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
 | 
			
		||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
			
		||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
			
		||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
 | 
			
		||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
			
		||||
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 h1:sreVOrDp0/ezb0CHKVek/l7YwpxPJqv+jT3izfSphA4=
 | 
			
		||||
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
 | 
			
		||||
		Reference in New Issue
	
	Block a user