Example #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)
}
Example #2
0
// Stores the user in memcache and data store
func SaveUser(req *http.Request, u datastore.User) {
	// Storing into memcache
	err := memcache.Store(u.Email, u, req)
	log.LogErrorWithMsg("Cannot store the user into memcache:", err)
	// Storing into datastore
	err = datastore.Store(req, datastore.KIND_USER, u)
	log.LogErrorWithMsg("Cannot store the user into datastore:", err)
}
Example #3
0
// Retrieve the file stored for the given name
func Retrieve(req *http.Request, userName, fileName string) (*storage.Reader, 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()
	filePath := getFilePath(userName, fileName)
	return client.Bucket(BUCKET_NAME).Object(filePath).NewReader(ctx)
}
Example #4
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
}
Example #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()
	}
}
Example #6
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)
	}
}
Example #7
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)
}
Example #8
0
// 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
}