forked from alvinbaena/passkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
173 lines (140 loc) · 4.07 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package passkit
import (
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
const (
BundleIconRetinaHD = "[email protected]"
BundleIconRetina = "[email protected]"
BundleIcon = "icon.png"
BundleLogoRetinaHD = "[email protected]"
BundleLogoRetina = "[email protected]"
BundleLogo = "logo.png"
BundleThumbnailRetinaHD = "[email protected]"
BundleThumbnailRetina = "[email protected]"
BundleThumbnail = "thumbnail.png"
BundleStripRetinaHD = "[email protected]"
BundleStripRetina = "[email protected]"
BundleStrip = "strip.png"
BundleBackgroundRetinaHD = "[email protected]"
BundleBackgroundRetina = "[email protected]"
BundleBackground = "background.png"
BundleFooterRetinaHD = "[email protected]"
BundleFooterRetina = "[email protected]"
BundleFooter = "footer.png"
BundlePersonalizationLogoRetinaHD = "[email protected]"
BundlePersonalizationLogoRetina = "[email protected]"
BundlePersonalizationLogo = "personalizationLogo.png"
)
type PassTemplate interface {
ProvisionPassAtDirectory(tmpDirPath string) error
GetAllFiles() (map[string][]byte, error)
}
type folderPassTemplate struct {
templateDir string
}
func NewFolderPassTemplate(templateDir string) *folderPassTemplate {
return &folderPassTemplate{templateDir: templateDir}
}
func (f *folderPassTemplate) ProvisionPassAtDirectory(tmpDirPath string) error {
return copyDir(f.templateDir, tmpDirPath)
}
func (f *folderPassTemplate) GetAllFiles() (map[string][]byte, error) {
loaded, err := loadDir(f.templateDir)
if err != nil {
return nil, err
}
ret := make(map[string][]byte)
for name, data := range loaded {
ret[filepath.Base(name)] = data
}
return ret, err
}
type inMemoryPassTemplate struct {
files map[string][]byte
}
func NewInMemoryPassTemplate() *inMemoryPassTemplate {
return &inMemoryPassTemplate{files: make(map[string][]byte)}
}
func (m *inMemoryPassTemplate) ProvisionPassAtDirectory(tmpDirPath string) error {
dst := filepath.Clean(tmpDirPath)
_, err := os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
err = os.MkdirAll(dst, os.ModeDir)
if err != nil {
return nil
}
for file, d := range m.files {
err = ioutil.WriteFile(filepath.Join(dst, string(file)), d, 0644)
if err != nil {
os.RemoveAll(dst)
return err
}
}
return nil
}
func (m *inMemoryPassTemplate) GetAllFiles() (map[string][]byte, error) {
return m.files, nil
}
func (m *inMemoryPassTemplate) AddFileBytes(name string, data []byte) {
m.files[name] = data
}
func (m *inMemoryPassTemplate) AddFileBytesLocalized(name, locale string, data []byte) {
m.files[m.pathForLocale(name, locale)] = data
}
func (m *inMemoryPassTemplate) downloadFile(u url.URL) ([]byte, error) {
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
response, err := client.Get(u.String())
if err != nil {
return nil, err
}
defer response.Body.Close()
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (m *inMemoryPassTemplate) AddFileFromURL(name string, u url.URL) error {
b, err := m.downloadFile(u)
if err != nil {
return err
}
m.files[name] = b
return nil
}
func (m *inMemoryPassTemplate) AddFileFromURLLocalized(name, locale string, u url.URL) error {
b, err := m.downloadFile(u)
if err != nil {
return err
}
m.files[m.pathForLocale(name, locale)] = b
return nil
}
func (m *inMemoryPassTemplate) AddAllFiles(directoryWithFilesToAdd string) error {
src := filepath.Clean(directoryWithFilesToAdd)
loaded, err := loadDir(src)
if err != nil {
return err
}
for name, data := range loaded {
m.files[filepath.Base(name)] = data
}
return nil
}
func (m *inMemoryPassTemplate) pathForLocale(name string, locale string) string {
if strings.TrimSpace(locale) == "" {
return name
}
return filepath.Join(locale+".lproj", name)
}