Ejemplo n.º 1
0
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)

}
Ejemplo n.º 2
0
func Test_SaveUserData(t *testing.T) {
	a := assert.New(t)
	info := &dropbox.AccountInfo{
		Email:       "*****@*****.**",
		Uid:         1234,
		DisplayName: "pippo",
	}
	token := dropbox.AccessToken{Key: "a", Secret: "b"}
	datastore.SaveUserData(info, token)

	at, _ := datastore.LoadUserToken("*****@*****.**")
	a.Equal(at, token)

	at, _ = datastore.LoadUserTokenByUID(1234)
	a.Equal(at, token)

	userData, _ := datastore.LoadUserData("*****@*****.**")
	a.Equal(userData, info)
}
Ejemplo n.º 3
0
// 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)
	})
}