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

}