mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 22:15:51 +01:00
(refs #28)Look up Gravatar if user icon is not configured.
This commit is contained in:
@@ -58,10 +58,10 @@ trait AccountControllerBase extends AccountManagementControllerBase with FlashMa
|
||||
getAccountByUserName(userName).flatMap(_.image).map { image =>
|
||||
contentType = FileUtil.getMimeType(image)
|
||||
new java.io.File(getUserUploadDir(userName), image)
|
||||
} getOrElse {
|
||||
contentType = "image/png"
|
||||
Thread.currentThread.getContextClassLoader.getResourceAsStream("noimage.png")
|
||||
}
|
||||
// } getOrElse {
|
||||
// contentType = "image/png"
|
||||
// Thread.currentThread.getContextClassLoader.getResourceAsStream("noimage.png")
|
||||
} getOrElse NotFound
|
||||
}
|
||||
|
||||
get("/:userName/_edit")(oneselfOnly {
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.apache.commons.io.FileUtils
|
||||
import model.Account
|
||||
import scala.Some
|
||||
import service.AccountService
|
||||
import javax.servlet.http.HttpServletRequest
|
||||
|
||||
/**
|
||||
* Provides generic features for controller implementations.
|
||||
@@ -22,7 +23,7 @@ abstract class ControllerBase extends ScalatraFilter
|
||||
/**
|
||||
* Returns the context object for the request.
|
||||
*/
|
||||
implicit def context: Context = Context(servletContext.getContextPath, LoginAccount, currentURL)
|
||||
implicit def context: Context = Context(servletContext.getContextPath, LoginAccount, currentURL, request)
|
||||
|
||||
private def currentURL: String = {
|
||||
val queryString = request.getQueryString
|
||||
@@ -94,7 +95,7 @@ abstract class ControllerBase extends ScalatraFilter
|
||||
/**
|
||||
* Context object for the current request.
|
||||
*/
|
||||
case class Context(path: String, loginAccount: Option[Account], currentUrl: String)
|
||||
case class Context(path: String, loginAccount: Option[Account], currentUrl: String, request: HttpServletRequest)
|
||||
|
||||
/**
|
||||
* Base trait for controllers which manages account information.
|
||||
|
||||
@@ -8,4 +8,10 @@ object StringUtil {
|
||||
md.digest.map(b => "%02x".format(b)).mkString
|
||||
}
|
||||
|
||||
def md5(value: String): String = {
|
||||
val md = java.security.MessageDigest.getInstance("MD5")
|
||||
md.update(value.getBytes)
|
||||
md.digest.map(b => "%02x".format(b)).mkString
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package view
|
||||
import java.util.Date
|
||||
import java.text.SimpleDateFormat
|
||||
import twirl.api.Html
|
||||
import util.StringUtil
|
||||
import service.AccountService
|
||||
|
||||
/**
|
||||
* Provides helper methods for Twirl templates.
|
||||
@@ -73,6 +75,29 @@ object helpers {
|
||||
// convert commit id to link
|
||||
.replaceAll("(^|\\W)([a-f0-9]{40})(\\W|$)", "$1<a href=\"%s/%s/%s/commit/$2\">$2</a>$3").format(context.path, repository.owner, repository.name))
|
||||
|
||||
/**
|
||||
* Returns <img> which displays the avatar icon.
|
||||
* Looks up Gravatar if avatar icon has not been configured in user settings.
|
||||
*/
|
||||
def avatar(userName: String, size: Int, tooltip: Boolean = false)(implicit context: app.Context): Html = {
|
||||
val account = Option(context.request.getAttribute("cache.account." + userName).asInstanceOf[model.Account]).orElse {
|
||||
new AccountService {}.getAccountByUserName(userName).map { account =>
|
||||
context.request.setAttribute("cache.account." + userName, account)
|
||||
account
|
||||
}
|
||||
}
|
||||
val src = account.collect { case account if(account.image.isEmpty) =>
|
||||
s"""http://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress)}?s=${size}"""
|
||||
} getOrElse {
|
||||
s"""${context.path}/${userName}/_avatar"""
|
||||
}
|
||||
if(tooltip){
|
||||
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}:px" data-toggle="tooltip" title=${userName}/>""")
|
||||
} else {
|
||||
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}:px" />""")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implicit conversion to add mkHtml() to Seq[Html].
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user