Exemplo n.º 1
0
func (c *Audio) HandleUpload() revel.Result {
	var files [][]byte

	c.Params.Bind(&files, "file")
	filesInfo := make([]FileInfo, len(files))
	for i, _ := range files {
		handler := helpers.GenerateHandler()
		filesInfo[i] = FileInfo{
			ContentType: c.Params.Files["file[]"][i].Header.Get("Content-Type"),
			Filename:    c.Params.Files["file[]"][i].Filename,
			Size:        len(files[i]),
			Handler:     handler,
		}
		var name = revel.BasePath + "/public/audio/" + handler + filesInfo[i].Filename[:len(filesInfo[i].Filename)-4] + filesInfo[i].Filename[len(filesInfo[i].Filename)-4:len(filesInfo[i].Filename)]
		fmt.Printf(name)
		fo, err := os.Create(name)
		if err != nil {
			panic(err)
		}
		fo.Write(files[i])

		defer func() {
			if err := fo.Close(); err != nil {
				panic(err)
			}
		}()
	}
	return c.RenderJson(map[string]interface{}{
		"Count":  len(files),
		"Files":  filesInfo,
		"Status": "Successfully uploaded",
	})
}
Exemplo n.º 2
0
func (m *UserModel) Authorize() UserResult {
	var results []UserModel
	fmt.Printf(m.Password)
	fmt.Printf("/\\")
	query("user", bson.M{"login": m.Login}, &results)
	if (len(results)) == 0 {
		error := Error{Msg: "User not found", Type: "login"}
		user := UserResult{Authorized: false, Errors: []Error{error}}
		return user
	}
	userRecord := results[0]
	compResult := bcrypt.CompareHashAndPassword([]byte(userRecord.Password), []byte(m.Password)) == nil
	if !compResult {
		error := Error{Msg: "Password does not match", Type: "password"}
		user := UserResult{Authorized: false, Errors: []Error{error}}
		return user
	}
	token := helpers.GenerateHandler()
	update("user", userRecord.Id, bson.M{"token": token})
	userRecord.Token = token
	userRes := UserResult{Authorized: true, User: &userRecord}
	return userRes
}