Exemple #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)
}
Exemple #2
0
// 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)
}
Exemple #3
0
// 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)

}
Exemple #4
0
// Creates a template for the given data by including the standard header to it.
func GetAPlusTemplateHeader(req *http.Request, data interface{}) APlusTemplate {
	return APlusTemplate{
		Header: Header{
			IsLoggedIn: session.GetUser(req).Email != "",
		},
		Data: data,
	}
}
Exemple #5
0
// Download handler
func DownloadHandler(res http.ResponseWriter, req *http.Request) {
	fileName := req.URL.Query().Get("fileName")
	rc, err := storage.Retrieve(req, session.GetUser(req).Email, fileName)
	log.LogErrorWithMsg("Cannot retrieve the file name given", err)
	if err == nil {
		res.Header().Add("content-type", rc.ContentType())
		io.Copy(res, rc)
		defer rc.Close()
	}
}
Exemple #6
0
// 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)
}
Exemple #7
0
// Gets the user based on the logged in user in session
func GetUser(req *http.Request) datastore.User {
	// Getting user's email from session
	email := session.GetUser(req).Email
	return GetUserWithEmail(email, req)
}