コード例 #1
0
ファイル: example_test.go プロジェクト: Juraldinio/vole
func ExampleGetAvatarURL() {
	// get URL to avatar image of size 256x256
	// fall back to "monster" generated avatar
	emailHash := gr.EmailHash("*****@*****.**")
	url := gr.GetAvatarURL("https", emailHash, gr.DefaultMonster, 256)
	fmt.Println(url.String())
}
コード例 #2
0
ファイル: account.go プロジェクト: jango2015/OJ
//generate gravatar url
func genGravatarUrl(email string) string {
	return gravatar.GetAvatarURL(
		"https",
		gravatar.EmailHash(email),
		gravatar.DefaultMonster,
		256).String()

}
コード例 #3
0
ファイル: example_test.go プロジェクト: Juraldinio/vole
func ExampleGetProfile() {
	// get profile using HTTPS transport
	emailHash := gr.EmailHash("*****@*****.**")
	profile, err := gr.GetProfile("https", emailHash)

	if err == nil {
		fmt.Println(profile.PreferredUsername)
		fmt.Println(profile.ProfileUrl)
	}
}
コード例 #4
0
ファイル: example_test.go プロジェクト: Juraldinio/vole
func ExampleSetAvatarURLOptions() {
	// get URL to avatar image of default size
	emailHash := gr.EmailHash("*****@*****.**")
	url := gr.GetAvatarURL("https", emailHash)
	fmt.Printf("default URL: %s", url.String())
	// set size to 256x256
	// fall back to "monster" generated avatar
	gr.SetAvatarURLOptions(url, gr.DefaultMonster, 256)
	fmt.Printf("modified URL: %s", url.String())
	// reset back to the default one
	gr.SetAvatarURLOptions(url)
	fmt.Printf("URL after reset: %s", url.String())
}
コード例 #5
0
ファイル: jacodoma.go プロジェクト: leandrosansilva/jacodoma
func gravatarImageProvider(email string, width, height int) image.Image {
	emailHash := gravatar.EmailHash(email)

	raw, err := gravatar.GetAvatar("https", emailHash, gravatar.DefaultMonster, width)

	if err != nil {
		panic(err)
	}

	if img, err := png.Decode(bytes.NewReader(raw)); err == nil {
		return img
	}

	if img, err := jpeg.Decode(bytes.NewReader(raw)); err == nil {
		return img
	}

	return nil
}
コード例 #6
0
ファイル: example_test.go プロジェクト: Juraldinio/vole
func ExampleGetAvatar() {
	// get avatar image (128x128) using HTTP transport
	emailHash := gr.EmailHash("*****@*****.**")
	raw, err := gr.GetAvatar("http", emailHash, 128)

	// get avatar image (32x32) using HTTP transport
	// allow images of any rating level
	raw, err = gr.GetAvatar("http", emailHash, gr.RatingX, 32)

	// get avatar image (default size, png format) with fallback to "retro"
	// generated avatar.
	// use HTTPS transport
	emailHash = "cfcd208495d565ef66e7dff9f98764da.png"
	raw, err = gr.GetAvatar("https", emailHash, gr.DefaultRetro)

	if err == nil {
		var cfg image.Config
		var format string

		rawb := bytes.NewReader(raw)
		cfg, format, err = image.DecodeConfig(rawb)
		fmt.Println(cfg, format)
	}
}
コード例 #7
0
func RPCFriendsGetUserAvatarMessage(conn net.Conn, connection_data *structs.ConnData, packet_data *structs.PacketData) error {
	// Unmarshal the message
	msg := new(protocol.FriendsGetUserAvatarMessage)
	err := proto.Unmarshal(packet_data.Content, msg)
	if err != nil {
		return err
	}

	mutexCreation.Lock()
	mutex, exists := downloadMutexes[msg.GetGuid()]
	if !exists {
		downloadMutexes[msg.GetGuid()] = new(sync.Mutex)
		mutex = downloadMutexes[msg.GetGuid()]
	}
	mutexCreation.Unlock()

	mutex.Lock()
	defer mutex.Unlock()

	// Check for data in the cache
	cached, ok := avatarCache[msg.GetGuid()]
	if ok {
		//logger.Errorf("Getting %d avatar from cache", msg.GetGuid())

		return reply.Reply(conn, packet_data.Header.Id, &protocol.FriendsGetUserAvatarResultMessage{
			Result:   proto.Int32(0),
			Guid:     msg.Guid,
			FileData: cached,
		})
	}

	// Query the database for the avatar info
	var rows []*struct {
		Email string
		Type  string
		Image string
	}

	err = environment.Env.Database.Query(`
SELECT
	email,
	avatar_type as type,
	avatar_image as image
FROM 
	misago_user
WHERE
	id = ?`, int(msg.GetGuid())).Rows(&rows)

	// Query error, something's wrong!
	if err != nil {
		return err
	}

	// No user found, might be a bug or invalid client request
	if len(rows) < 1 {
		return nil
	}

	// Load file from disk
	if rows[0].Type == "upload" {
		// Generate the filepath
		filename := filepath.Join(environment.Env.Config.NP.AvatarsPath, rows[0].Image)

		//logger.Errorf("Getting %d avatar from disk; %s", msg.GetGuid(), filename)

		// Ensure the file exists
		if !utility.FileExists(filename) {
			return nil
		}

		// Read file contents
		filecontents, err := ioutil.ReadFile(filename)
		if err != nil {
			return err
		}

		// Get the extension of the file
		ext := filepath.Ext(filename)

		// Convert from jpg to png
		if ext == ".jpg" || ext == ".jpeg" {
			rd := bytes.NewReader(filecontents)
			img, err := jpeg.Decode(rd)
			if err != nil {
				return err
			}

			buf := make([]byte, 0)
			wr := bytes.NewBuffer(buf)
			err = png.Encode(wr, img)
			if err != nil {
				return err
			}

			filecontents = wr.Bytes()
		}

		// Convert from gif to png
		if ext == ".gif" {
			rd := bytes.NewReader(filecontents)
			img, err := gif.Decode(rd)
			if err != nil {
				return err
			}

			buf := make([]byte, 0)
			wr := bytes.NewBuffer(buf)
			err = png.Encode(wr, img)
			if err != nil {
				return err
			}

			filecontents = wr.Bytes()
		}

		// Cache it
		avatarCache[msg.GetGuid()] = filecontents
		//avatarCache.Set(strconv.Itoa(int(msg.GetGuid())), filecontents, -1)

		// Return it to the client
		return reply.Reply(conn, packet_data.Header.Id, &protocol.FriendsGetUserAvatarResultMessage{
			Result:   proto.Int32(0),
			Guid:     msg.Guid,
			FileData: filecontents,
		})
	} else if rows[0].Type == "gravatar" {
		// Download the avatar
		data, err := gravatar.GetAvatar("http", gravatar.EmailHash(rows[0].Email), 96, gravatar.DefaultIdentIcon)
		if err != nil {
			return err
		}

		//logger.Errorf("Getting %d avatar from internet; %s", msg.GetGuid(), rows[0].Email)

		// Cache it
		//avatarCache.Set(strconv.Itoa(int(msg.GetGuid())), data, -1)
		avatarCache[msg.GetGuid()] = data

		// Return it to the client
		return reply.Reply(conn, packet_data.Header.Id, &protocol.FriendsGetUserAvatarResultMessage{
			Result:   proto.Int32(0),
			Guid:     msg.Guid,
			FileData: data,
		})
	}

	return nil
}
コード例 #8
0
ファイル: user.go プロジェクト: Manouel/go-viducha
func (user *User) CreateGravatarLink() {
	hashedMail := gr.EmailHash(user.Mail)
	user.GravatarLink = "http://www.gravatar.com/avatar.php?gravatar_id=" + string(hashedMail[:])
}
コード例 #9
0
ファイル: main.go プロジェクト: composehub/server
func gravatar(e, s string) string {
	emailHash := gr.EmailHash(e)
	url := "https://www.gravatar.com/avatar/" + emailHash + "?default=retro&amp;s=" + s
	return url
}
コード例 #10
0
ファイル: account.go プロジェクト: jango2015/baseapp
func (c Account) SaveUser(user models.User, username, name string) r.Result {

	// Lower case username
	username = strings.ToLower(username)
	lcPass := strings.ToLower(user.Password)

	// Validate User components
	models.ValidateUserEmail(c.Validation, user.Email).Key("user.Email")
	models.ValidateUserPassword(c.Validation, user.Password).Key("user.Password")

	// Additional user components verification
	c.Validation.Required(user.Password != user.Email).Message("Password cannot be the same as your email address").Key("user.Password")
	c.Validation.Required(lcPass != username).Message("Password cannot be the same as your user name").Key("user.Password")

	// Validate Profile components
	models.ValidateProfileUserName(c.Validation, username).Key("username")
	models.ValidateProfileName(c.Validation, name).Key("name")

	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Error("Registration failed.")
		return c.Redirect(routes.Account.Register())
	}

	userExists := c.getProfileByEmailAddress(user.Email)

	if userExists != nil {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Error("Email '" + user.Email + "' is already registered.")
		return c.Redirect(routes.Account.Register())
	}

	userExists = c.getProfileByUserName(username)

	if userExists != nil {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Error("User name '" + username + "' is already taken.")
		return c.Redirect(routes.Account.Register())
	}

	user.HashedPassword, _ = bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)

	user.Created = time.Now()

	user.Confirmed = false

	err := c.Txn.Insert(&user)
	if err != nil {
		panic(err)
	}

	// Create profile (and assign correct UserId)
	profile := &models.Profile{0, user.UserId, username, name, "", "", "", 0, 0, &user}

	// Get Gravatar Icon
	emailHash := gr.EmailHash(user.Email)
	gravatarUrl := gr.GetAvatarURL("https", emailHash, gr.DefaultIdentIcon, 128)

	if gravatarUrl != nil {
		profile.PhotoUrl = gravatarUrl.String()
	}

	err = c.Txn.Insert(profile)
	if err != nil {
		panic(err)
	}

	// Send out confirmation email
	err = c.sendAccountConfirmEmail(&user)

	if err != nil {
		c.Flash.Error("Could not send confirmation email")
		fmt.Println(err.Error())
	}

	c.Session["userEmail"] = string(user.Email)
	c.Flash.Success("Welcome, " + profile.Name)
	return c.Redirect(routes.Profile.Show(profile.UserName))
}