mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
(refs #28)Avatar image can be uploaded at the account editing page.
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package app
|
||||
|
||||
import service._
|
||||
import util.OneselfAuthenticator
|
||||
import util.{FileUtil, FileUploadUtil, OneselfAuthenticator}
|
||||
import util.StringUtil._
|
||||
import util.Directory._
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
import org.apache.commons.io.FileSystemUtils
|
||||
import org.apache.commons.io.FileUtils
|
||||
|
||||
class AccountController extends AccountControllerBase
|
||||
with SystemSettingsService with AccountService with RepositoryService with ActivityService
|
||||
@@ -17,7 +17,8 @@ trait AccountControllerBase extends ControllerBase {
|
||||
|
||||
case class AccountNewForm(userName: String, password: String,mailAddress: String, url: Option[String])
|
||||
|
||||
case class AccountEditForm(password: Option[String], mailAddress: String, url: Option[String], fileId: Option[String])
|
||||
case class AccountEditForm(password: Option[String], mailAddress: String, url: Option[String],
|
||||
fileId: Option[String], clearImage: Boolean)
|
||||
|
||||
val newForm = mapping(
|
||||
"userName" -> trim(label("User name" , text(required, maxlength(100), identifier, uniqueUserName))),
|
||||
@@ -30,7 +31,8 @@ trait AccountControllerBase extends ControllerBase {
|
||||
"password" -> trim(label("Password" , optional(text(maxlength(20))))),
|
||||
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100), uniqueMailAddress("userName")))),
|
||||
"url" -> trim(label("URL" , optional(text(maxlength(200))))),
|
||||
"fileId" -> trim(label("File ID" , optional(text())))
|
||||
"fileId" -> trim(label("File ID" , optional(text()))),
|
||||
"clearImage" -> trim(label("Clear image" , boolean()))
|
||||
)(AccountEditForm.apply)
|
||||
|
||||
/**
|
||||
@@ -48,6 +50,14 @@ trait AccountControllerBase extends ControllerBase {
|
||||
} getOrElse NotFound
|
||||
}
|
||||
|
||||
get("/:userName/_avatar"){
|
||||
val userName = params("userName")
|
||||
getAccountByUserName(userName).flatMap(_.image).map { image =>
|
||||
contentType = FileUtil.getMimeType(image)
|
||||
new java.io.File(getUserUploadDir(userName), image)
|
||||
} getOrElse NotFound
|
||||
}
|
||||
|
||||
get("/:userName/_edit")(oneselfOnly {
|
||||
val userName = params("userName")
|
||||
getAccountByUserName(userName).map(x => account.html.edit(Some(x))) getOrElse NotFound
|
||||
@@ -61,13 +71,20 @@ trait AccountControllerBase extends ControllerBase {
|
||||
mailAddress = form.mailAddress,
|
||||
url = form.url))
|
||||
|
||||
form.fileId.map { fileId =>
|
||||
val filename = app.FileUploadUtil.getFilename(fileId)
|
||||
org.apache.commons.io.FileUtils.moveFile(
|
||||
app.FileUploadUtil.getTemporaryFile(fileId),
|
||||
new java.io.File(getUserUploadDir(userName), filename.get)
|
||||
)
|
||||
updateAvatarImage(userName, filename)
|
||||
if(form.clearImage){
|
||||
account.image.map { image =>
|
||||
new java.io.File(getUserUploadDir(userName), image).delete()
|
||||
updateAvatarImage(userName, None)
|
||||
}
|
||||
} else {
|
||||
form.fileId.map { fileId =>
|
||||
val filename = "avatar." + FileUtil.getExtension(FileUploadUtil.getUploadedFilename(fileId).get)
|
||||
FileUtils.moveFile(
|
||||
FileUploadUtil.getTemporaryFile(fileId),
|
||||
new java.io.File(getUserUploadDir(userName), filename)
|
||||
)
|
||||
updateAvatarImage(userName, Some(filename))
|
||||
}
|
||||
}
|
||||
|
||||
redirect("/%s".format(userName))
|
||||
|
||||
@@ -1,28 +1,26 @@
|
||||
package app
|
||||
|
||||
import util.Directory._
|
||||
import util.{FileUtil, FileUploadUtil}
|
||||
import org.scalatra._
|
||||
import org.scalatra.servlet.{MultipartConfig, FileUploadSupport}
|
||||
import java.text.SimpleDateFormat
|
||||
import org.apache.commons.io.FileUtils
|
||||
import javax.servlet.http.HttpSession
|
||||
|
||||
/**
|
||||
* Provides Ajax based file upload functionality.
|
||||
*
|
||||
* This servlet saves uploaded file as temporary file and returns the unique key.
|
||||
* You can get uploaded file using [[app.FileUploadUtil#getFile()]] with this key.
|
||||
* This servlet saves uploaded file as temporary file and returns the unique id.
|
||||
* You can get uploaded file using [[util.FileUploadUtil#getTemporaryFile()]] with this id.
|
||||
*/
|
||||
// TODO Remove temporary files at session timeout by session listener.
|
||||
class FileUploadController extends ScalatraServlet with FileUploadSupport with FlashMapSupport {
|
||||
configureMultipartHandling(MultipartConfig(maxFileSize = Some(3 * 1024 * 1024)))
|
||||
|
||||
post("/"){
|
||||
post("/image"){
|
||||
fileParams.get("file") match {
|
||||
case Some(file) => {
|
||||
val fileId = new SimpleDateFormat("yyyyMMddHHmmSSsss").format(new java.util.Date(System.currentTimeMillis))
|
||||
case Some(file) if(FileUtil.isImage(file.name)) => {
|
||||
val fileId = FileUploadUtil.generateFileId
|
||||
FileUtils.writeByteArrayToFile(FileUploadUtil.getTemporaryFile(fileId), file.get)
|
||||
session += "fileId" -> file.name
|
||||
session += "upload_" + fileId -> file.name
|
||||
Ok(fileId)
|
||||
}
|
||||
case None => BadRequest
|
||||
@@ -31,28 +29,3 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with F
|
||||
|
||||
}
|
||||
|
||||
// TODO move to other package or should be trait?
|
||||
object FileUploadUtil {
|
||||
|
||||
def TemporaryDir(implicit session: HttpSession): java.io.File =
|
||||
new java.io.File(GitBucketHome, "tmp/_%s".format(session.getId))
|
||||
|
||||
def getTemporaryFile(fileId: String)(implicit session: HttpSession): java.io.File =
|
||||
new java.io.File(TemporaryDir, fileId)
|
||||
|
||||
def removeTemporaryFile(fileId: String)(implicit session: HttpSession): Unit =
|
||||
getTemporaryFile(fileId).delete()
|
||||
|
||||
def removeTemporaryFiles()(implicit session: HttpSession): Unit =
|
||||
FileUtils.deleteDirectory(TemporaryDir)
|
||||
|
||||
def getFilename(fileId: String)(implicit session: HttpSession): Option[String] = {
|
||||
val filename = Option(session.getAttribute(fileId).asInstanceOf[String])
|
||||
if(filename.isDefined){
|
||||
session.removeAttribute(fileId)
|
||||
}
|
||||
filename
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ object Directory {
|
||||
/**
|
||||
* Directory for uploaded files by the specified user.
|
||||
*/
|
||||
def getUserUploadDir(userName: String): File = new File("%s/%s/_files".format(GitBucketHome, userName))
|
||||
def getUserUploadDir(userName: String): File = new File("%s/data/%s/files".format(GitBucketHome, userName))
|
||||
|
||||
/**
|
||||
* Root of temporary directories for the specified repository.
|
||||
|
||||
33
src/main/scala/util/FileUploadUtil.scala
Normal file
33
src/main/scala/util/FileUploadUtil.scala
Normal file
@@ -0,0 +1,33 @@
|
||||
package util
|
||||
|
||||
import java.text.SimpleDateFormat
|
||||
import javax.servlet.http.HttpSession
|
||||
import util.Directory._
|
||||
import org.apache.commons.io.FileUtils
|
||||
|
||||
object FileUploadUtil {
|
||||
|
||||
def generateFileId: String =
|
||||
new SimpleDateFormat("yyyyMMddHHmmSSsss").format(new java.util.Date(System.currentTimeMillis))
|
||||
|
||||
def TemporaryDir(implicit session: HttpSession): java.io.File =
|
||||
new java.io.File(GitBucketHome, "tmp/_upload/%s".format(session.getId))
|
||||
|
||||
def getTemporaryFile(fileId: String)(implicit session: HttpSession): java.io.File =
|
||||
new java.io.File(TemporaryDir, fileId)
|
||||
|
||||
def removeTemporaryFile(fileId: String)(implicit session: HttpSession): Unit =
|
||||
getTemporaryFile(fileId).delete()
|
||||
|
||||
def removeTemporaryFiles()(implicit session: HttpSession): Unit =
|
||||
FileUtils.deleteDirectory(TemporaryDir)
|
||||
|
||||
def getUploadedFilename(fileId: String)(implicit session: HttpSession): Option[String] = {
|
||||
val filename = Option(session.getAttribute("upload_" + fileId).asInstanceOf[String])
|
||||
if(filename.isDefined){
|
||||
session.removeAttribute("upload_" + fileId)
|
||||
}
|
||||
filename
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,4 +44,13 @@ object FileUtil {
|
||||
}
|
||||
}
|
||||
|
||||
def getExtension(name: String): String = {
|
||||
val index = name.lastIndexOf('.')
|
||||
if(index >= 0){
|
||||
name.substring(index + 1)
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,12 +36,21 @@
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<label for="avatar"><strong>Image (Optional)</strong></label>
|
||||
<div id="avatar" class="muted small">
|
||||
<div id="clickable">No Image</div>
|
||||
<div id="avatar" class="muted">
|
||||
@if(account.nonEmpty && account.get.image.nonEmpty){
|
||||
<img src="@path/@account.get.userName/_avatar" style="with: 120px; height: 120px;"/>
|
||||
} else {
|
||||
<div id="clickable">No Image</div>
|
||||
}
|
||||
</div>
|
||||
@if(account.nonEmpty && account.get.image.nonEmpty){
|
||||
<label>
|
||||
<input type="checkbox" name="clearImage"/> Clear image
|
||||
</label>
|
||||
}
|
||||
<input type="hidden" name="fileId" value=""/>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<fieldset class="buttons">
|
||||
@if(account.isDefined){
|
||||
<input type="submit" class="btn btn-success" value="Save"/>
|
||||
<a href="@url(account.get.userName)" class="btn">Cancel</a>
|
||||
@@ -54,7 +63,7 @@
|
||||
<script>
|
||||
$(function(){
|
||||
var dropzone = new Dropzone('div#clickable', {
|
||||
url: '@path/upload',
|
||||
url: '@path/upload/image',
|
||||
previewsContainer: 'div#avatar',
|
||||
paramName: 'file',
|
||||
parallelUploads: 1,
|
||||
@@ -65,7 +74,6 @@ $(function(){
|
||||
dropzone.on("success", function(file, id){
|
||||
$('div#clickable').remove();
|
||||
$('input[name=fileId]').val(id);
|
||||
alert(id);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@@ -83,8 +91,11 @@ div#clickable {
|
||||
div#avatar {
|
||||
background-color: #f8f8f8;
|
||||
border: 1px dashed silver;
|
||||
margin-bottom: 10px;
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
}
|
||||
|
||||
fieldset.buttons {
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user