func main() { err := rpio.Open() if err != nil { fmt.Println("rpio.Open()") fmt.Println(err) } // Unmap gpio memory when done defer rpio.Close() toggleLight(9) var db *dropbox.Dropbox db = dropbox.NewDropbox() db.SetAppInfo(clientid, clientsecret) db.SetAccessToken(token) toggleLight(10) shoot(db) toggleLight(11) }
// New creates a client to connect to Dropbox. func New(key, secret, token string) (db *Dropbox, err error) { if key == "" { err = errors.New("empty key") return } if secret == "" { err = errors.New("empty secret") return } if token == "" { err = errors.New("empty token") return } var ( client *dropbox.Dropbox ) client = dropbox.NewDropbox() client.SetAppInfo(key, secret) client.SetAccessToken(token) db = &Dropbox{ client: client, cwd: "/", } return }
func shoot(db *dropbox.Dropbox) { cmd := exec.Command("raspistill", "-n", "-o", "-") out, err := cmd.StdoutPipe() if err != nil { fmt.Println("cmd.StdoutPipe()") fmt.Println(err) } err = cmd.Start() if err != nil { fmt.Println("cmd.Start()") fmt.Println(err) } data, err := ioutil.ReadAll(out) if err != nil { fmt.Println("ioutil.ReadAll(out)") fmt.Println(err) } cmd.Wait() now := time.Now().Format("2006-01-02 150405") len := int64(len(data)) bytes := NopReadCloser(bytes.NewReader(data)) _, err = db.FilesPut(bytes, len, fmt.Sprintf("%s.jpg", now), true, "") if err != nil { fmt.Println("db.FilesPut(...)") fmt.Println(err) } }
/* Actually get a list of directories or files from the dropbox connection */ func get(cache *cache.Cache, db *dropbox.Dropbox, path string, directories bool) []dropbox.Entry { // Use caching to reduce calls to the Dropbox API var cache_descriptor string if directories { cache_descriptor = "dirs:" } else { cache_descriptor = "files:" } s := []string{} s = append(s, cache_descriptor) s = append(s, path) cache_path := strings.Join(s, "") data, found := cache.Get(cache_path) if found { if cached_paths, ok := data.([]dropbox.Entry); ok { return (cached_paths) } else { log.Println("Error: Unable to retrieve from cache") } } entry, err := db.Metadata(path, true, false, "", "", 500) if err != nil { log.Println(err) return []dropbox.Entry{} } paths := make([]dropbox.Entry, 0) for i := 0; i < len(entry.Contents); i++ { entry := entry.Contents[i] if directories { if entry.IsDir { paths = append(paths, entry) } } else { if !entry.IsDir { include := true for _, lookup := range do_not_include { if strings.Contains(entry.Path, lookup) { include = false } } if include { paths = append(paths, entry) } } } } cache.Set(cache_path, paths, 0) return paths }
/* Retrieves files from the users Dropbox. Returns a byte * array of the files contents. */ func loadDropboxFile(path string, db *dropbox.Dropbox) ([]byte, error) { closer, _, err := db.Download(path, "", 0) if err != nil { fmt.Printf("Error loading dropbox file: %v\n", err) return nil, err } file, err := ioutil.ReadAll(closer) return file, err }
func main() { configFile, err := ioutil.ReadFile("config.json") if err != nil { panic("Error opening config file: " + err.Error()) } var c config err = json.Unmarshal(configFile, &c) if err != nil { panic(err) } var db *dropbox.Dropbox // 1. Create a new dropbox object. db = dropbox.NewDropbox() // 2. Provide your clientid and clientsecret (see prerequisite). db.SetAppInfo(c.Clientid, c.Clientsecret) // 3. Provide the user token. db.SetAccessToken(c.Token) fmt.Println(c.DropboxPath) err = db.DownloadToFile(c.DropboxPath, c.Destination, "") if err != nil { panic(err) } }
func handler(w http.ResponseWriter, r *http.Request) { var err error var db *dropbox.Dropbox var input io.ReadCloser var length int64 var ok bool var cachedItem interface{} var s string if r.URL.Path[1:] == "favicon.ico" { return } cachedItem, ok = Lru.Get(r.URL.Path[1:]) if ok { item := cachedItem.(cacheItem) s = item.Value length = item.Length } else { var clientid, clientsecret string var token string clientid = os.Getenv("CLIENTID") clientsecret = os.Getenv("CLIENTSECRET") token = os.Getenv("TOKEN") // 1. Create a new dropbox object. db = dropbox.NewDropbox() // 2. Provide your clientid and clientsecret (see prerequisite). db.SetAppInfo(clientid, clientsecret) // 3. Provide the user token. db.SetAccessToken(token) // 4. Send your commands. // In this example, you will create a new folder named "demo". if input, length, _, err = db.Thumbnails("/"+r.URL.Path[2:], "jpeg", r.URL.Path[1:2]); err != nil { fmt.Printf("Error: %s Url: %s Size: \n", err, r.URL.Path[2:], r.URL.Path[1:2]) } else { //fmt.Printf("Error %s\n", input) buf := new(bytes.Buffer) buf.ReadFrom(input) s = buf.String() Lru.Add(r.URL.Path[1:], cacheItem{Value: s, Length: length}) } } w.Header().Set("Content-Length", strconv.FormatInt(length, 10)) fmt.Fprintf(w, "%s", s) }
/* Returns a shared link to dropbox file */ func get_link(cache *cache.Cache, db *dropbox.Dropbox, path string) string { // Use caching to reduce calls to the Dropbox API cache_path := strings.Join([]string{"link", path}, ":") data, found := cache.Get(cache_path) if found { if cached, ok := data.(string); ok { return cached } else { log.Println("Error: Unable to retrieve from cache") } } link, err := db.Shares(path, false) if err != nil { log.Println(err) return "" } cache.Set(cache_path, link.URL, 0) return link.URL }
func WriteFile(config *DropboxConfig, file string, content string) error { var db *dropbox.Dropbox db = dropbox.NewDropbox() db.SetAppInfo(config.AppKey, config.AppSecret) db.SetAccessToken(config.Token) in := ioutil.NopCloser(bytes.NewBufferString(content)) _, err := db.UploadByChunk(in, 1024, file, true, "") return err }
func ReadFile(config *DropboxConfig, file string) (string, error) { var err error var db *dropbox.Dropbox var s string db = dropbox.NewDropbox() db.SetAppInfo(config.AppKey, config.AppSecret) db.SetAccessToken(config.Token) rd, _, err := db.Download(file, "", 0) if err != nil { return s, err } buf := new(bytes.Buffer) buf.ReadFrom(rd) s = buf.String() return s, nil }
func handleOriginalFile(w http.ResponseWriter, r *http.Request) { var err error var db *dropbox.Dropbox // var input io.ReadCloser var length int64 var ok bool var cachedItem interface{} var s string if r.URL.Path[1:] == "favicon.ico" { return } cachedItem, ok = Lru.Get(r.URL.Path[5:]) if ok { item := cachedItem.(cacheItem) s = item.Value length = item.Length } else { var clientid, clientsecret string var token, rev string clientid = os.Getenv("CLIENTID") clientsecret = os.Getenv("CLIENTSECRET") token = os.Getenv("TOKEN") // 1. Create a new dropbox object. db = dropbox.NewDropbox() // 2. Provide your clientid and clientsecret (see prerequisite). db.SetAppInfo(clientid, clientsecret) // 3. Provide the user token. db.SetAccessToken(token) rev = "" //h := md5.New() //io.WriteString(h, r.URL.Path[5:]) //b := h.Sum(nil) str := strings.Replace(r.URL.Path[5:], "/", "-", -1) err = db.DownloadToFile("/"+r.URL.Path[5:], "/tmp/"+str, rev) if err != nil { fmt.Printf("123 Error: %s Url: %s Size: \n", err, r.URL.Path[5:], r.URL.Path[1:2]) } else { dat, err := ioutil.ReadFile("/tmp/" + str) if err != nil { } else { f, err := os.Open("/tmp/" + str) if err != nil { } fi, err := f.Stat() if err != nil { // Could not obtain stat, handle error } length = fi.Size() s = string(dat[:]) // w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10)) fmt.Fprintf(w, "%s", dat) Lru.Add(r.URL.Path[5:], cacheItem{Value: s, Length: length}) } } } w.Header().Set("Content-Length", strconv.FormatInt(length, 10)) fmt.Fprintf(w, "%s", s) }