...
1 package sriracha
2
3 import (
4 "io"
5 "log"
6 "reflect"
7 "strings"
8 )
9
10 type Extension interface {
11 Description() string
12 }
13
14 var (
15 allExtensions []Extension
16 allExtensionNames []string
17 )
18
19 func AddExtension(e Extension) {
20 for _, ext := range allExtensions {
21 if ext == e {
22 return
23 }
24 }
25 allExtensions = append(allExtensions, e)
26 allExtensionNames = append(allExtensionNames, extensionName(e))
27 }
28
29 func extensionName(e Extension) string {
30 name := reflect.ValueOf(e).Type().String()
31 if name[0] == '*' {
32 name = name[1:]
33 }
34 if strings.HasPrefix(name, "extension.") {
35 name = name[10:]
36 }
37 return strings.ToUpper(name[0:1]) + name[1:]
38 }
39
40 func attach(file io.Reader, size int64, mime string) (*Attachment, error) {
41 for i, ext := range allExtensions {
42 if extension, ok := ext.(ExtensionAttach); ok {
43 log.Println("DEBUG ATTACH", allExtensionNames[i])
44 a, err := extension.Attach(file, size, mime)
45 if err != nil {
46 return nil, err
47 } else if a != nil {
48 return a, nil
49 }
50 }
51
52 }
53 return nil, nil
54 }
55
56 func post(p *Post) error {
57 for i, ext := range allExtensions {
58 if extension, ok := ext.(ExtensionPost); ok {
59 log.Println("DEBUG POST", allExtensionNames[i])
60 err := extension.Post(p)
61 if err != nil {
62 return err
63 }
64 }
65 }
66 return nil
67 }
68
View as plain text