Пример #1
0
// Gallery handler
func GalleryHandler(res http.ResponseWriter, req *http.Request) {
	log.Println("GalleryHandler...")
	username := session.GetUser(req).Email
	giphyName := ""
	log.Println("req.Method:" + req.Method)
	_, header, err := req.FormFile("file")
	if header != nil {
		log.Println("header.Filename:" + header.Filename)
	}
	if req.Method == "POST" {
		file, header, err := req.FormFile("file")
		log.LogErrorWithMsg("Cannot read the file from the request", err)
		if err == nil {
			err = storage.Store(req, username, header.Filename, file)
			log.LogErrorWithMsg("Cannot store the uploaded file", err)
		}
		giphyName = header.Filename
		log.Println("giphyName:" + giphyName)
	}
	//Parsing the template
	tpl := template.Must(template.ParseFiles("template/gallery.html"))

	// Getting user's list of file
	fileList, err := storage.RetrieveFileList(req, username)
	log.LogErrorWithMsg("Cannot get user's list of files", err)

	files := createFiles(fileList)
	gt := GalleryTemp{
		GiphyName: getFileName(giphyName),
		Files:     files,
	}

	err = tpl.Execute(res, GetAPlusTemplateHeader(req, gt))
	log.LogError(err)
}
Пример #2
0
// Handles session related operation. Checks to see if a user is in session.
func Handle(res http.ResponseWriter, req *http.Request) bool {
	isUserInSession, _ := isUserInSession(req)
	if isUserInSession {
		log.Println("User is in session.")
	} else {
		log.Println("User is not in session, setting the SESSION-ID ...")
	}
	return isUserInSession
}
Пример #3
0
// Checks to see if the user is logged in by looking at the sessionID stored on the request cookie
func isUserInSession(req *http.Request) (bool, *User) {
	sessionIdCookie, err := req.Cookie(SESSION_ID)
	var user User
	if err != nil {
		log.Println("Error reading SESSIONID:" + err.Error())
		return false, &user
	}
	// Retrieve the item from memcache
	memcache.Retrieve(sessionIdCookie.Value, req, &user)
	if user.Email != "" {
		return true, &user
	}
	return false, &user
}
Пример #4
0
// Handler for downloading GIF from Giphy api
func DownloadGiphyHandler(res http.ResponseWriter, req *http.Request) {
	log.Println("DownloadGiphy Handler...")
	// Calling the API
	name := req.FormValue("name")
	c := appengine.NewContext(req)
	client := urlfetch.Client(c)
	resp, err := client.Get(getGiphyUrl(name))
	defer resp.Body.Close()
	log.LogErrorWithMsg("Cannot call URL:", err)
	body, err := ioutil.ReadAll(resp.Body)
	log.LogErrorWithMsg("Cannot read response:", err)
	var data Giphy
	err = json.Unmarshal(body, &data)
	log.LogErrorWithMsg("Cannot unmarshal", err)
	log.Println(data)

	// Downloading the image
	if len(data.Data) > 0 {
		resp, err = client.Get(data.Data[0].Images["fixed_width"].Url)
		log.LogErrorWithMsg("Cannot download image:", err)
		io.Copy(res, resp.Body)
	}
}
Пример #5
0
// Logout handler
func LogoutHandler(res http.ResponseWriter, req *http.Request) {
	// Clearing the session ID if its present and redirecting the user to font page.
	cookie, err := req.Cookie(session.SESSION_ID)
	if err == nil {
		// Clearing the cookie
		cookie.MaxAge = -1
		http.SetCookie(res, cookie)
		// Clears the sessionId given from sessions
		err := memcache.Delete(cookie.Value, req)
		log.LogErrorWithMsg("Cannot logout the user", err)

	} else {
		log.Println("The session is not set, skipping the logic.")
	}
	// Redirecting the user to front page.
	http.Redirect(res, req, URL_ROOT, http.StatusFound)
}
Пример #6
0
// Get's user's information from memcache, if it does not exists, it will look into datastore.
func GetUserWithEmail(email string, req *http.Request) datastore.User {
	// Getting the data from memcache
	var u datastore.User
	err := memcache.Retrieve(email, req, &u)
	if err != nil {
		log.Println("Cannot get the user from memcache", err)
		// Getting the user from datastore
		u, err = datastore.Retrieve(req, datastore.KIND_USER, email)
		if err == nil {
			// Trying to store the data into memcache
			err := memcache.Store(email, u, req)
			log.LogErrorWithMsg("Cannot store the data retreived from datastore into memcache", err)
		} else {
			log.LogErrorWithMsg("Cannot retreive the data from datastore", err)
		}
	}
	return u
}
Пример #7
0
// A handler for Ajax calls which writes a JSON on the response back, saying if the user is already taken or not.
func IsUserTaken(res http.ResponseWriter, req *http.Request) {
	userName := req.FormValue("username")
	log.Println("Checking to see if the user [" + userName + "] is already taken...")
	u := util.GetUserWithEmail(userName, req)
	json.NewEncoder(res).Encode(u.Email != "")
}