sriracha/extension.go

77 lines
1.4 KiB
Go

package sriracha
import (
"io"
"log"
"reflect"
"strings"
)
type ExtensionSettings struct {
// Hidden fields
}
func (s *ExtensionSettings) Get(name string) string {
return ""
}
func (s *ExtensionSettings) Set(name string, value string) {
return
}
type Extension interface {
Init(settings *ExtensionSettings) (description string)
}
var (
allExtensions []Extension
allExtensionNames []string
)
func AddExtension(e Extension) {
for _, ext := range allExtensions {
if ext == e {
return
}
}
allExtensions = append(allExtensions, e)
allExtensionNames = append(allExtensionNames, extensionName(e))
}
func extensionName(e Extension) string {
name := reflect.ValueOf(e).Type().String()
if name[0] == '*' {
name = name[1:]
}
name = strings.TrimPrefix(name, "extension.")
return strings.ToUpper(name[0:1]) + name[1:]
}
func attach(file io.Reader, size int64, mime string) (*Attachment, error) {
for i, ext := range allExtensions {
if extension, ok := ext.(ExtensionAttach); ok {
log.Println("DEBUG ATTACH", allExtensionNames[i])
a, err := extension.Attach(file, size, mime)
if err != nil {
return nil, err
} else if a != nil {
return a, nil
}
}
}
return nil, nil
}
func post(p *Post) error {
for i, ext := range allExtensions {
if extension, ok := ext.(ExtensionPost); ok {
log.Println("DEBUG POST", allExtensionNames[i])
err := extension.Post(p)
if err != nil {
return err
}
}
}
return nil
}