|
| 1 | +package cloudwatcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sync/atomic" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox" |
| 10 | + "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/files" |
| 11 | + "github.com/minio/minio-go" |
| 12 | +) |
| 13 | + |
| 14 | +type DropboxWatcher struct { |
| 15 | + WatcherBase |
| 16 | + |
| 17 | + syncing uint32 |
| 18 | + |
| 19 | + ticker *time.Ticker |
| 20 | + stop chan bool |
| 21 | + config *DropboxConfiguration |
| 22 | + client *minio.Client |
| 23 | + cache map[string]*DropboxObject |
| 24 | +} |
| 25 | + |
| 26 | +type DropboxObject struct { |
| 27 | + Key string |
| 28 | + Size int64 |
| 29 | + LastModified time.Time |
| 30 | + Hash string |
| 31 | +} |
| 32 | + |
| 33 | +type DropboxConfiguration struct { |
| 34 | + Debug Bool `json:"debug"` |
| 35 | + Token string `json:"token"` |
| 36 | +} |
| 37 | + |
| 38 | +func newDropboxWatcher(dir string, interval time.Duration) (Watcher, error) { |
| 39 | + w := &DropboxWatcher{ |
| 40 | + cache: make(map[string]*DropboxObject), |
| 41 | + config: nil, |
| 42 | + stop: make(chan bool, 1), |
| 43 | + WatcherBase: WatcherBase{ |
| 44 | + Events: make(chan Event, 100), |
| 45 | + Errors: make(chan error, 100), |
| 46 | + watchDir: dir, |
| 47 | + pollingTime: interval, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + return w, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (w *DropboxWatcher) SetConfig(m map[string]string) error { |
| 55 | + j, err := json.Marshal(m) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + config := DropboxConfiguration{} |
| 61 | + if err := json.Unmarshal(j, &config); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + if config.Token == "" { |
| 66 | + return fmt.Errorf("token not specified") |
| 67 | + } |
| 68 | + w.config = &config |
| 69 | + |
| 70 | + |
| 71 | + fmt.Printf("%v", config) |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (w *DropboxWatcher) Start() error { |
| 76 | + if w.config == nil { |
| 77 | + return fmt.Errorf("configuration for Dropbox needed") |
| 78 | + } |
| 79 | + |
| 80 | + w.ticker = time.NewTicker(w.pollingTime) |
| 81 | + go func() { |
| 82 | + // launch synchronization also the first time |
| 83 | + w.sync() |
| 84 | + for { |
| 85 | + select { |
| 86 | + case <-w.ticker.C: |
| 87 | + w.sync() |
| 88 | + |
| 89 | + case <-w.stop: |
| 90 | + close(w.Events) |
| 91 | + close(w.Errors) |
| 92 | + return |
| 93 | + } |
| 94 | + } |
| 95 | + }() |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (w *DropboxWatcher) Close() { |
| 100 | + w.stop <- true |
| 101 | +} |
| 102 | + |
| 103 | +func (w *DropboxWatcher) sync() { |
| 104 | + // allow only one sync at same time |
| 105 | + if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) { |
| 106 | + return |
| 107 | + } |
| 108 | + defer atomic.StoreUint32(&w.syncing, 0) |
| 109 | + |
| 110 | + fileList := make(map[string]*DropboxObject, 0) |
| 111 | + |
| 112 | + err := w.enumerateFiles(w.watchDir, func(obj *DropboxObject) bool { |
| 113 | + // Store the files to check the deleted one |
| 114 | + fileList[obj.Key] = obj |
| 115 | + // Check if the object is cached by Key |
| 116 | + cached := w.getCachedObject(obj) |
| 117 | + // Object has been cached previously by Key |
| 118 | + if cached != nil { |
| 119 | + // Check if the LastModified has been changed |
| 120 | + if !cached.LastModified.Equal(obj.LastModified) || cached.Hash != obj.Hash { |
| 121 | + event := Event{ |
| 122 | + Key: obj.Key, |
| 123 | + Type: FileChanged, |
| 124 | + Object: obj, |
| 125 | + } |
| 126 | + w.Events <- event |
| 127 | + } |
| 128 | + } else { |
| 129 | + event := Event{ |
| 130 | + Key: obj.Key, |
| 131 | + Type: FileCreated, |
| 132 | + Object: obj, |
| 133 | + } |
| 134 | + w.Events <- event |
| 135 | + } |
| 136 | + w.cache[obj.Key] = obj |
| 137 | + return true |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + w.Errors <- err |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + for k, o := range w.cache { |
| 145 | + if _, found := fileList[k]; !found { |
| 146 | + // file not found in the list...deleting it |
| 147 | + delete(w.cache, k) |
| 148 | + event := Event{ |
| 149 | + Key: o.Key, |
| 150 | + Type: FileDeleted, |
| 151 | + Object: nil, |
| 152 | + } |
| 153 | + w.Events <- event |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func (w *DropboxWatcher) enumerateFiles(prefix string, callback func(object *DropboxObject) bool) error { |
| 159 | + logLevel := dropbox.LogOff |
| 160 | + if w.config.Debug { |
| 161 | + logLevel = dropbox.LogDebug |
| 162 | + } |
| 163 | + |
| 164 | + config := dropbox.Config{ |
| 165 | + Token: w.config.Token, |
| 166 | + LogLevel: logLevel, |
| 167 | + Logger: nil, |
| 168 | + AsMemberID: "", |
| 169 | + Domain: "", |
| 170 | + Client: nil, |
| 171 | + HeaderGenerator: nil, |
| 172 | + URLGenerator: nil, |
| 173 | + } |
| 174 | + dbx := files.New(config) |
| 175 | + arg := files.NewListFolderArg(prefix) |
| 176 | + arg.Recursive = true |
| 177 | + |
| 178 | + var entries []files.IsMetadata |
| 179 | + res, err := dbx.ListFolder(arg) |
| 180 | + if err != nil { |
| 181 | + listRevisionError, ok := err.(files.ListRevisionsAPIError) |
| 182 | + if ok { |
| 183 | + // Don't treat a "not_folder" error as fatal; recover by sending a |
| 184 | + // get_metadata request for the same path and using that response instead. |
| 185 | + if listRevisionError.EndpointError.Path.Tag == files.LookupErrorNotFolder { |
| 186 | + var metaRes files.IsMetadata |
| 187 | + metaRes, err = w.getFileMetadata(dbx, prefix) |
| 188 | + entries = []files.IsMetadata{metaRes} |
| 189 | + } else { |
| 190 | + // Return if there's an error other than "not_folder" or if the follow-up |
| 191 | + // metadata request fails. |
| 192 | + return err |
| 193 | + } |
| 194 | + } else { |
| 195 | + return err |
| 196 | + } |
| 197 | + } else { |
| 198 | + entries = res.Entries |
| 199 | + |
| 200 | + for res.HasMore { |
| 201 | + arg := files.NewListFolderContinueArg(res.Cursor) |
| 202 | + |
| 203 | + res, err = dbx.ListFolderContinue(arg) |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + |
| 208 | + entries = append(entries, res.Entries...) |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + for _, entry := range entries { |
| 213 | + o := &DropboxObject{} |
| 214 | + switch f := entry.(type) { |
| 215 | + case *files.FileMetadata: |
| 216 | + o.Key = f.PathDisplay |
| 217 | + o.Size = int64(f.Size) |
| 218 | + o.LastModified = f.ServerModified |
| 219 | + o.Hash = f.ContentHash |
| 220 | + callback(o) |
| 221 | + //case *files.FolderMetadata: |
| 222 | + // o.Key = f.PathDisplay |
| 223 | + // o.Size = 0 |
| 224 | + // o.LastModified = 0 |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return nil |
| 229 | +} |
| 230 | + |
| 231 | +func (w *DropboxWatcher) getFileMetadata(c files.Client, path string) (files.IsMetadata, error) { |
| 232 | + arg := files.NewGetMetadataArg(path) |
| 233 | + |
| 234 | + arg.IncludeDeleted = true |
| 235 | + |
| 236 | + res, err := c.GetMetadata(arg) |
| 237 | + if err != nil { |
| 238 | + return nil, err |
| 239 | + } |
| 240 | + |
| 241 | + return res, nil |
| 242 | +} |
| 243 | + |
| 244 | +func (w *DropboxWatcher) getCachedObject(o *DropboxObject) *DropboxObject { |
| 245 | + if cachedObject, ok := w.cache[o.Key]; ok { |
| 246 | + return cachedObject |
| 247 | + } |
| 248 | + return nil |
| 249 | +} |
| 250 | + |
| 251 | +func init() { |
| 252 | + supportedServices["dropbox"] = newDropboxWatcher |
| 253 | +} |
0 commit comments