/
apiControl-Photo.go
205 lines (166 loc) · 3.78 KB
/
apiControl-Photo.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package cmd
import (
"fmt"
"html/template"
"image"
"os"
"path/filepath"
"strings"
"encoding/json"
"github.com/tektsu/gedcom"
)
func (api *apiControl) addPhoto(o *gedcom.ObjectRecord) *photoResponse {
key := getPhotoKeyFromObject(o)
if _, ok := api.photos[key]; !ok {
api.photos[key] = &photoResponse{
ID: key,
File: filepath.Base(o.File.Name),
Title: o.File.Title,
}
if o.File.Description != nil {
api.photos[key].Description = o.File.Description.Note
}
for _, note := range o.Note {
api.photos[key].Notes = append(api.photos[key].Notes, note.Note)
}
file, err := os.Open(o.File.Name)
defer file.Close()
if err != nil {
fmt.Printf("%v\n", err)
return api.photos[key]
}
img, _, err := image.DecodeConfig(file) // Image Struct
if err != nil {
fmt.Printf("hsgdhajsgdjhas %s: %v\n", o.File.Name, err)
return api.photos[key]
}
api.photos[key].Width = img.Width
api.photos[key].Height = img.Height
}
return api.photos[key]
}
func (api *apiControl) addPhotoForIndividual(o *gedcom.ObjectRecord, i *individualResponse) *photoResponse {
response := api.addPhoto(o)
ir, err := api.getIndividualIndexEntry(i.ID)
if err != nil {
fmt.Printf("%s\n", err)
return response
}
response.People = append(response.People, ir)
return response
}
func (api *apiControl) addPhotoForFamily(o *gedcom.ObjectRecord, f *familyResponse) *photoResponse {
response := api.addPhoto(o)
fr, err := api.getFamilyIndexEntry(f.ID)
if err != nil {
fmt.Printf("%s\n", err)
return response
}
response.Families = append(response.Families, fr)
return response
}
func (api *apiControl) buildFromGedcom(g *gedcom.Gedcom) error {
api.gc = g
var err error
err = api.addSources()
if err != nil {
return err
}
err = api.addIndividuals()
if err != nil {
return err
}
err = api.addFamilies()
if err != nil {
return err
}
return nil
}
func (api *apiControl) exportPhotoAPI() error {
photoAPIDir := filepath.Join(api.cx.String("project"), "static", "api", "photo")
err := os.MkdirAll(photoAPIDir, 0777)
if err != nil {
return err
}
var photoIDs []string
for id, photo := range api.photos {
photoIDs = append(photoIDs, id)
file := filepath.Join(photoAPIDir, strings.ToLower(id+".json"))
fh, err := os.Create(file)
if err != nil {
return err
}
j, err := json.Marshal(photo)
if err != nil {
fh.Close()
return err
}
_, err = fh.Write(j)
if err != nil {
fh.Close()
return err
}
fh.Close()
}
file := filepath.Join(photoAPIDir, strings.ToLower("list.json"))
fh, err := os.Create(file)
if err != nil {
return err
}
j, err := json.Marshal(photoIDs)
if err != nil {
fh.Close()
return err
}
_, err = fh.Write(j)
if err != nil {
fh.Close()
return err
}
fh.Close()
return nil
}
func (api *apiControl) exportPhotoPages() error {
const photoPageTemplate = `---
url: "/{{ .ID }}/"
categories:
- Photo
lead_photo: {{ .File }}
photo_key: {{ .ID }}
---
<script src="/js/jquery.min.js"></script>
<script src="/js/photodisplay.js"></script>
<script>
$(document).ready(function(){
photodisplay("{{ .ID }}")
});
</script>
<div id="display"></div>
<div id="raw"></div>
`
photoDir := filepath.Join(api.cx.String("project"), "content", "media")
err := os.MkdirAll(photoDir, 0777)
if err != nil {
return err
}
for key, photo := range api.photos {
file := filepath.Join(photoDir, key+".md")
fh, err := os.Create(file)
if err != nil {
return err
}
defer fh.Close()
tpl := template.New("photo")
tpl, err = tpl.Parse(photoPageTemplate)
if err != nil {
return err
}
err = tpl.Execute(fh, photo)
}
return nil
}
// Local functions
func getPhotoKeyFromObject(o *gedcom.ObjectRecord) string {
key := "p" + strings.ToLower(strings.Replace(filepath.Base(o.File.Name), ".", "", -1))
return key
}