62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package sriracha
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
type Extension interface {
|
|
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:]
|
|
}
|
|
if strings.HasPrefix(name, "extension.") {
|
|
name = name[10:]
|
|
}
|
|
return strings.ToUpper(name[0:1]) + name[1:]
|
|
}
|
|
|
|
func attach(file io.Reader, size int64, mime string) (*Attachment, error) {
|
|
for i, ext := range allExtensions {
|
|
log.Println("DEBUG ATTACH", allExtensionNames[i])
|
|
a, err := ext.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 {
|
|
log.Println("DEBUG POST", allExtensionNames[i])
|
|
err := ext.CreatePost(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|