-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollections.go
159 lines (144 loc) · 3.82 KB
/
collections.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
package luminosity
import (
"bytes"
"database/sql"
"gopkg.in/guregu/null.v3"
)
type CollectionType int
const (
CollectionTypeStandard CollectionType = iota
CollectionTypeSmart
CollectionTypeGroup
)
func (c CollectionType) String() string {
switch c {
case CollectionTypeStandard:
return "standard"
case CollectionTypeSmart:
return "smart"
case CollectionTypeGroup:
return "group"
default:
return "unknown"
}
}
func (c CollectionType) MarshalJSON() ([]byte, error) {
buf := bytes.NewBufferString(`"`)
buf.WriteString(c.String())
buf.WriteString(`"`)
return buf.Bytes(), nil
}
type Collection struct {
Id string `json:"id"`
Name null.String `json:"name"`
ParentId null.String `json:"parent_id,omitempty"`
Type CollectionType `json:"type"`
Parent *Collection `json:"-"`
Children []*Collection `json:"children,omitempty"`
}
func (c *Collection) scan(row *sql.Rows) error {
var collectionType string
if err := row.Scan(&c.Id, &c.Name, &c.ParentId, &collectionType); err != nil {
return err
}
switch collectionType {
case "com.adobe.ag.library.smart_collection":
c.Type = CollectionTypeSmart
case "com.adobe.ag.library.group":
c.Type = CollectionTypeGroup
case "com.adobe.ag.library.collection":
fallthrough
default:
c.Type = CollectionTypeStandard
}
return nil
}
// GetCollections returns a flat list of the collections defined in
// the catalog, excluding collection nodes which are purely structural
// and do not contain photos (i.e. collection groups). System
// collections, such as the always present "Quick Collection", are
// also ignored.
func (c *Catalog) GetCollections() ([]*Collection, error) {
const query = `
SELECT id_local,
name,
parent,
creationId
FROM AgLibraryCollection
WHERE systemOnly = 0
AND creationId != 'com.adobe.ag.library.group'
ORDER BY creationId, name, parent
`
if c.Collections != nil {
return c.Collections, nil
}
if rows, err := c.db.query("get_collections", query); err != nil {
return nil, err
} else {
defer rows.Close()
var collections []*Collection
for rows.Next() {
c := &Collection{}
if err := c.scan(rows); err != nil {
return collections, err
}
collections = append(collections, c)
}
c.Collections = collections
return c.Collections, nil
}
}
// GetCollectionTree returns all collections in the catalog while
// maintaining the hierarchical relationship of grouped
// collections. Because there can be multiple collection tree roots in
// the Lightroom catalog, they are returned under a dummy root node.
func (c *Catalog) GetCollectionTree() (*Collection, error) {
const query = `
SELECT id_local,
name,
parent,
creationId
FROM AgLibraryCollection c
WHERE c.systemOnly = 0
ORDER BY parent, name
`
if c.CollectionTree != nil {
return c.CollectionTree, nil
}
if rows, err := c.db.query("get_collection_tree", query); err != nil {
return nil, err
} else {
defer rows.Close()
// Set up dummy root node
root := &Collection{
Name: null.StringFrom("Root"),
Children: []*Collection{},
Type: CollectionTypeGroup,
}
// Working storage to look up parent nodes based on the parent
// ID returned from the SQL query
collections := map[string]*Collection{}
// First pass - ensure all collections are instantiated and
// stored in the map
for rows.Next() {
c := &Collection{}
if err := c.scan(rows); err != nil {
return nil, err
}
collections[c.Id] = c
}
// Second pass - construct the tree, with back-links to
// parents
for _, c := range collections {
parent := root
parentid := c.ParentId.ValueOrZero()
if parentid != "" {
parent = collections[parentid]
}
c.Parent = parent
parent.Children = append(parent.Children, c)
}
c.CollectionTree = root
return c.CollectionTree, nil
}
}