forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
237 lines (185 loc) · 5.4 KB
/
cache.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"time"
. "github.com/claudetech/loggo/default"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"golang.org/x/oauth2"
)
// Cache is the cache
type Cache struct {
db *gorm.DB
dbAction chan cacheAction
tokenPath string
}
const (
// StoreAction stores an object in cache
StoreAction = iota
// DeleteAction deletes an object in cache
DeleteAction = iota
)
type cacheAction struct {
action int
object *APIObject
}
// APIObject is a Google Drive file object
type APIObject struct {
ObjectID string `gorm:"primary_key"`
Name string `gorm:"index"`
IsDir bool
Size uint64
LastModified time.Time
DownloadURL string
Parents string `gorm:"index"`
CreatedAt time.Time
}
// PageToken is the last change id
type PageToken struct {
gorm.Model
Token string
}
// NewCache creates a new cache instance
func NewCache(cacheBasePath string, sqlDebug bool) (*Cache, error) {
Log.Debugf("Opening cache connection")
db, err := gorm.Open("sqlite3", filepath.Join(cacheBasePath, "cache"))
if nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not open cache database")
}
Log.Debugf("Migrating cache schema")
db.AutoMigrate(&APIObject{})
db.AutoMigrate(&PageToken{})
db.LogMode(sqlDebug)
cache := Cache{
db: db,
dbAction: make(chan cacheAction),
tokenPath: filepath.Join(cacheBasePath, "token.json"),
}
go cache.startStoringQueue()
return &cache, nil
}
func (c *Cache) startStoringQueue() {
for {
action := <-c.dbAction
if nil != action.object {
if action.action == DeleteAction || action.action == StoreAction {
Log.Debugf("Deleting object %v", action.object.ObjectID)
c.db.Delete(action.object)
}
if action.action == StoreAction {
Log.Debugf("Storing object %v in cache", action.object.ObjectID)
c.db.Create(action.object)
}
}
}
}
// Close closes all handles
func (c *Cache) Close() error {
Log.Debugf("Closing cache connection")
close(c.dbAction)
if err := c.db.Close(); nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not close cache connection")
}
return nil
}
// LoadToken loads a token from cache
func (c *Cache) LoadToken() (*oauth2.Token, error) {
Log.Debugf("Loading token from cache")
tokenFile, err := ioutil.ReadFile(c.tokenPath)
if nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not read token file in %v", c.tokenPath)
}
var token oauth2.Token
json.Unmarshal(tokenFile, &token)
Log.Tracef("Got token from cache %v", token)
return &token, nil
}
// StoreToken stores a token in the cache or updates the existing token element
func (c *Cache) StoreToken(token *oauth2.Token) error {
Log.Debugf("Storing token to cache")
tokenJSON, err := json.Marshal(token)
if nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not generate token.json content")
}
if err := ioutil.WriteFile(c.tokenPath, tokenJSON, 0644); nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not generate token.json file")
}
return nil
}
// GetObject gets an object by id
func (c *Cache) GetObject(id string) (*APIObject, error) {
Log.Tracef("Getting object %v", id)
var object APIObject
c.db.Where(&APIObject{ObjectID: id}).First(&object)
Log.Tracef("Got object from cache %v", object)
if "" != object.ObjectID {
return &object, nil
}
return nil, fmt.Errorf("Could not find object %v in cache", id)
}
// GetObjectsByParent get all objects under parent id
func (c *Cache) GetObjectsByParent(parent string) ([]*APIObject, error) {
Log.Debugf("Getting children for %v", parent)
var objects []*APIObject
c.db.Where("parents LIKE ?", fmt.Sprintf("%%|%v|%%", parent)).Find(&objects)
Log.Tracef("Got objects from cache %v", objects)
if 0 != len(objects) {
return objects, nil
}
return nil, fmt.Errorf("Could not find children for parent %v in cache", parent)
}
// GetObjectByParentAndName finds a child element by name and its parent id
func (c *Cache) GetObjectByParentAndName(parent, name string) (*APIObject, error) {
Log.Tracef("Getting object %v in parent %v", name, parent)
var object APIObject
c.db.Where("parents LIKE ? AND name = ?", fmt.Sprintf("%%|%v|%%", parent), name).First(&object)
Log.Tracef("Got object from cache %v", object)
if "" != object.ObjectID {
return &object, nil
}
return nil, fmt.Errorf("Could not find object with name %v in parent %v", name, parent)
}
// DeleteObject deletes an object by id
func (c *Cache) DeleteObject(id string) error {
c.dbAction <- cacheAction{
action: DeleteAction,
object: &APIObject{ObjectID: id},
}
return nil
}
// UpdateObject updates an object
func (c *Cache) UpdateObject(object *APIObject) error {
c.dbAction <- cacheAction{
action: StoreAction,
object: object,
}
return nil
}
// StoreStartPageToken stores the page token for changes
func (c *Cache) StoreStartPageToken(token string) error {
Log.Debugf("Storing page token %v in cache", token)
c.db.Delete(&PageToken{})
c.db.Create(&PageToken{
Token: token,
})
return nil
}
// GetStartPageToken gets the start page token
func (c *Cache) GetStartPageToken() (string, error) {
Log.Debugf("Getting start page token from cache")
var pageToken PageToken
c.db.First(&pageToken)
Log.Tracef("Got start page token %v", pageToken.Token)
if "" == pageToken.Token {
return "", fmt.Errorf("Token not found in cache")
}
return pageToken.Token, nil
}