func processUserDelta(email string) { articlesCursor, _ := datastore.GetCurrentCursor(email, "/published") at, err := datastore.LoadUserToken(email) if err != nil { log.Fatal(err) return } dbc := dropbox.NewClient(at, config.AppToken) //process articles d, _ := dbc.GetDelta("/published", articlesCursor) for _, v := range d.Deleted { a, err := datastore.LoadArticle(email + ":article:" + v) if err == nil { a.Delete() log.Printf("deleted: %s", v) } } wait.Wait(len(d.Updated), func(index int) { entry, _ := dbc.GetMetadata(d.Updated[index], true) file, _ := dbc.GetFile(d.Updated[index]) content, _ := ioutil.ReadAll(file) article := datastore.ParseEntry(*entry, content) article.GenerateID(email) article.Save() log.Printf("updated: %s", article.Path) }) datastore.ArticlesReindex(email) datastore.SaveCurrentCursor(email, "/published", d.Cursor) //process images imageCursor, _ := datastore.GetCurrentCursor(email, "/images") d, err = dbc.GetDelta("/images", imageCursor) for _, v := range d.Deleted { err := os.Remove("./public" + v) if err != nil { log.Println(err) } log.Printf("deleted: %s", v) } wait.Wait(len(d.Updated), func(index int) { file, _ := dbc.GetFile(d.Updated[index]) content, _ := ioutil.ReadAll(file) imgPath, _ := filepath.Abs("./public" + d.Updated[index]) os.MkdirAll(filepath.Dir(imgPath), 0755) err = ioutil.WriteFile("./public"+d.Updated[index], content, 0644) if err != nil { log.Println(err) } log.Printf("updated: %s", d.Updated[index]) }) datastore.SaveCurrentCursor(email, "/images", d.Cursor) }
// saves the user id in session, save used data and access token in // db, creates the default folders func callback(w http.ResponseWriter, r *http.Request) { withSession(w, r, func(session *sessions.Session) { RequestToken := session.Values["RequestToken"].(dropbox.RequestToken) AccessToken, _ := dropbox.FinishAuth(config.AppToken, RequestToken) dbc := dropbox.NewClient(AccessToken, config.AppToken) info, err := dbc.GetAccountInfo() if err != nil { log.Println(err) } datastore.SaveUserData(info, AccessToken) session.Values["email"] = info.Email session.Save(r, w) dbc.CreateDir("drafts") dbc.CreateDir("published") http.Redirect(w, r, "/", 302) }) }
func handleCommands() { refresh := flag.Bool("refresh", false, "refresh articles for the provided email if present, otherwise refresh the default email") oauth := flag.Bool("oauth", false, "authorize oauth app from command line") flag.Parse() if *refresh { if len(flag.Args()) > 0 { refreshArticles(flag.Args()[0]) } else { refreshArticles(config.DefaultUserEmail) } os.Exit(1) } if *oauth { requestToken, _ := dropbox.StartAuth(config.AppToken) url, _ := url.Parse("") fmt.Println("open in a web browser the following url and authorize boxed app:") fmt.Println(dropbox.GetAuthorizeURL(requestToken, url)) fmt.Println("\n\npress enter when ready") reader := bufio.NewReader(os.Stdin) reader.ReadString('\n') accessToken, _ := dropbox.FinishAuth(config.AppToken, requestToken) dbc := dropbox.NewClient(accessToken, config.AppToken) info, err := dbc.GetAccountInfo() if err != nil { fmt.Println(err) } else { fmt.Printf("authorized: %s %s\n", info.DisplayName, info.Email) datastore.SaveUserData(info, accessToken) dbc.CreateDir("drafts") dbc.CreateDir("published") dbc.CreateDir("images") } os.Exit(1) } }
// handlers in this files are not actively in use, eventually will be removed func account(w http.ResponseWriter, r *http.Request) { withSession(w, r, func(session *sessions.Session) { var AccessToken dropbox.AccessToken if email := session.Values["email"]; email == nil { fmt.Fprint(w, "no email found") return } email := session.Values["email"].(string) AccessToken, _ = datastore.LoadUserToken(email) dbc := dropbox.NewClient(AccessToken, config.AppToken) info, err := dbc.GetAccountInfo() if err != nil { // access token is not valid anymore // reset session session.Values["email"] = "" session.Save(r, w) fmt.Fprint(w, "access token not valid") return } fmt.Fprintf(w, "info = %+v\n", info) }) }