Esempio n. 1
0
// For login authentication from picasa.
// TODO: Add error handling.
func AuthHandler(c http.ResponseWriter, r *http.Request) {
	// Get the token supplied in the URL.
	picasaLen := len("token=")
	url, _ := url.QueryUnescape(r.URL.RawQuery)
	token := url[picasaLen:]
	log.Println(token, r.URL.RawQuery)

	// Try to upgrade the token to a multi-use one. See
	// http://code.google.com/apis/accounts/docs/AuthSub.html
	req := picasa.NewRequest("https://www.google.com/accounts/accounts/AuthSubSessionToken", token, "GET")
	resp, e := picasa.Send(req)

	// Get the upgraded token value
	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		fmt.Println(e)
	}
	resp.Body.Close()
	if len(body) <= picasaLen {
		log.Println("Invalid or missing token! Response received was:", body)
		template.Error500(c, r, nil)
	}
	upgradedToken := body[picasaLen:]
	log.Println("Upgraded Token: ", string(upgradedToken))

	// Finally, save the upgraded token in the server-side session.
	u, _ := user.Get(r)
	u.Set("picasa-authsub-token", string(upgradedToken))
	http.Redirect(c, r, "/photos/upload", http.StatusFound)
}
Esempio n. 2
0
func UploaderHandler(c http.ResponseWriter, r *http.Request) {
	u, _ := user.Get(r)
	token := u.Get("picasa-authsub-token")
	fmt.Println("Host:", r.Host)
	albums := GetAlbums()

	data := new(UploaderData)
	data.Albums = albums
	data.PicasaAuthenticated = (token != "")
	// TODO: Make photos use the new login system.

	tmpl.Render(c, r, "Upload Photos", "uploader", data)
}
Esempio n. 3
0
// Handles requests to upload to picasa in multipart/form-data format. Normally bound to /picasa/upload.
func UploadHandler(c http.ResponseWriter, r *http.Request) {
	// Handles multipart/form-data requests
	albumName, contentType, fileName, contentLength, fileReader, e := multipartUploadHandler(r)

	if e != nil {
		log.Println("Not multipart")

		// Handle a normal POST request, likely from the html5 uploader.
		albumName = r.Header.Get("X-Album-Name")
		contentType = r.Header.Get("Content-Type")
		fileName = r.Header.Get("Slug")
		contentLength = r.Header.Get("Content-Length")
		fileReader = r.Body
		defer r.Body.Close()
	}

	//var s *session.Session
	log.Println("Handling upload request at /picasa/upload.")
	log.Println(r)
	s, e := user.Get(r)
	if e != nil {
		fmt.Fprintln(c, "Invaid session. Please login.")
		return
	}
	resp, e := uploadToPicasa(albumName, contentType, fileName, contentLength, s, fileReader)
	if e != nil {
		log.Println("Error uploading to picasa!", e)
		fmt.Fprintln(c, "Error uploading to picasa:", e)
	}
	handleError(resp, c)
	// For debugging.
	resp.Write(os.Stdout)
	return

	//TODO: Add Multipart/Form-Data support.
}