// 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) }
// Profile handler func ProfileHandler(res http.ResponseWriter, req *http.Request) { var errs []string if req.Method == "POST" { // Validation comes here pass1 := req.FormValue("password1") pass2 := req.FormValue("password2") errs = validateProfile(pass1, pass2) if errs == nil { // Saving the form du := datastore.User{ Email: session.GetUser(req).Email, Password: Encrypt(pass1), FirstName: req.FormValue("fname"), LastName: req.FormValue("lname"), } // Store the user util.SaveUser(req, du) // Redirect the user to front page http.Redirect(res, req, URL_ROOT, http.StatusFound) } } //Parsing the template tpl := template.Must(template.ParseFiles("template/profile.html")) // Fetching user's data pt := ProfileTemp{ User: util.GetUser(req), Errors: errs, } err := tpl.Execute(res, GetAPlusTemplateHeader(req, pt)) log.LogError(err) }
// The main page handler func IndexHandler(res http.ResponseWriter, req *http.Request) { //Parsing the template tpl := template.Must(template.ParseFiles("template/index.html")) err := tpl.Execute(res, GetAPlusTemplateHeader(req, nil)) log.LogError(err) }
// Creates a session by creating a new UUID and setting it on cookie and sessions. func CreateSession(res *http.ResponseWriter, req *http.Request, user User) { newUuid, err := uuid.NewV4() sessionId := newUuid.String() log.LogError(err) memcache.Store(sessionId, user, req) createCookie(res, SESSION_ID, sessionId) }
// Login handler func LoginHandler(res http.ResponseWriter, req *http.Request) { if req.Method != "POST" && session.GetUser(req).Email != "" { // If user is already in session, we want to redirect him to front page. http.Redirect(res, req, URL_ROOT, http.StatusFound) return } invalidUser := false if req.Method == "POST" { email := req.FormValue("username") if isValidUser(email, req.FormValue("password"), req) { // Set the session session.CreateSession(&res, req, session.User{Email: email}) // Redirecting the user to profile page. http.Redirect(res, req, URL_ROOT, http.StatusFound) return } else { // Invalid User invalidUser = true } } //Parsing the template tpl := template.Must(template.ParseFiles("template/login.html")) err := tpl.Execute(res, invalidUser) log.LogError(err) }
// Signup handler func SignupHandler(res http.ResponseWriter, req *http.Request) { if req.Method != "POST" && session.GetUser(req).Email != "" { // If user is already in session, we want to redirect him to front page. http.Redirect(res, req, URL_ROOT, http.StatusFound) return } var errs []string if req.Method == "POST" { email := req.FormValue("email") pass1 := req.FormValue("password1") pass2 := req.FormValue("password2") // Validatation comes here errs = validate(email, pass1, pass2, req) if errs == nil { // Create the user du := datastore.User{ Email: email, Password: Encrypt(pass1), FirstName: req.FormValue("fname"), } // Store the user util.SaveUser(req, du) // Create session session.CreateSession(&res, req, session.User{Email: du.Email}) // Redirect the user to front page http.Redirect(res, req, URL_ROOT, http.StatusFound) } } //Parsing the template tpl := template.Must(template.ParseFiles("template/signup.html")) err := tpl.Execute(res, errs) log.LogError(err) }
// Retrieves the list of files for the given username func RetrieveFileList(req *http.Request, userName string) ([]string, error) { // Creating new context and client. ctx := appengine.NewContext(req) client, err := storage.NewClient(ctx) log.LogErrorWithMsg("Cannot create a new client", err) defer client.Close() query := &storage.Query{ Delimiter: "/", Prefix: userName + "/", } objs, err := client.Bucket(BUCKET_NAME).List(ctx, query) log.LogError(err) var names []string for _, result := range objs.Results { names = append(names, strings.Split(result.Name, "/")[1]) } return names, err }