mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-02 03:26:06 +01:00
(refs #28)Implementing avatar image uploading.
This commit is contained in:
1
src/main/resources/update/1_3.sql
Normal file
1
src/main/resources/update/1_3.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE ACCOUNT ADD COLUMN IMAGE VARCHAR(100);
|
||||
@@ -3,7 +3,9 @@ package app
|
||||
import service._
|
||||
import util.OneselfAuthenticator
|
||||
import util.StringUtil._
|
||||
import util.Directory._
|
||||
import jp.sf.amateras.scalatra.forms._
|
||||
import org.apache.commons.io.FileSystemUtils
|
||||
|
||||
class AccountController extends AccountControllerBase
|
||||
with SystemSettingsService with AccountService with RepositoryService with ActivityService
|
||||
@@ -15,7 +17,7 @@ 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])
|
||||
case class AccountEditForm(password: Option[String], mailAddress: String, url: Option[String], fileId: Option[String])
|
||||
|
||||
val newForm = mapping(
|
||||
"userName" -> trim(label("User name" , text(required, maxlength(100), identifier, uniqueUserName))),
|
||||
@@ -27,7 +29,8 @@ trait AccountControllerBase extends ControllerBase {
|
||||
val editForm = mapping(
|
||||
"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)))))
|
||||
"url" -> trim(label("URL" , optional(text(maxlength(200))))),
|
||||
"fileId" -> trim(label("File ID" , optional(text())))
|
||||
)(AccountEditForm.apply)
|
||||
|
||||
/**
|
||||
@@ -57,6 +60,16 @@ trait AccountControllerBase extends ControllerBase {
|
||||
password = form.password.map(encrypt).getOrElse(account.password),
|
||||
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)
|
||||
}
|
||||
|
||||
redirect("/%s".format(userName))
|
||||
} getOrElse NotFound
|
||||
})
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package app
|
||||
|
||||
import util.Directory._
|
||||
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.
|
||||
@@ -11,15 +15,15 @@ import org.scalatra.servlet.{MultipartConfig, FileUploadSupport}
|
||||
*/
|
||||
// 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)))
|
||||
configureMultipartHandling(MultipartConfig(maxFileSize = Some(3 * 1024 * 1024)))
|
||||
|
||||
post("/"){
|
||||
fileParams.get("file") match {
|
||||
// TODO save as temporary file and return key.
|
||||
case Some(file) => {
|
||||
println(file.name)
|
||||
println(file.size)
|
||||
Ok("1234")
|
||||
val fileId = new SimpleDateFormat("yyyyMMddHHmmSSsss").format(new java.util.Date(System.currentTimeMillis))
|
||||
FileUtils.writeByteArrayToFile(FileUploadUtil.getTemporaryFile(fileId), file.get)
|
||||
session += "fileId" -> file.name
|
||||
Ok(fileId)
|
||||
}
|
||||
case None => BadRequest
|
||||
}
|
||||
@@ -27,10 +31,28 @@ class FileUploadController extends ScalatraServlet with FileUploadSupport with F
|
||||
|
||||
}
|
||||
|
||||
// TODO Not implemented yet.
|
||||
// TODO move to other package or should be trait?
|
||||
object FileUploadUtil {
|
||||
def getFile(key: String) = {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ object Accounts extends Table[Account]("ACCOUNT") {
|
||||
def registeredDate = column[java.util.Date]("REGISTERED_DATE")
|
||||
def updatedDate = column[java.util.Date]("UPDATED_DATE")
|
||||
def lastLoginDate = column[java.util.Date]("LAST_LOGIN_DATE")
|
||||
def * = userName ~ mailAddress ~ password ~ isAdmin ~ url.? ~ registeredDate ~ updatedDate ~ lastLoginDate.? <> (Account, Account.unapply _)
|
||||
def image = column[String]("IMAGE")
|
||||
def * = userName ~ mailAddress ~ password ~ isAdmin ~ url.? ~ registeredDate ~ updatedDate ~ lastLoginDate.? ~ image.? <> (Account, Account.unapply _)
|
||||
}
|
||||
|
||||
case class Account(
|
||||
@@ -22,5 +23,6 @@ case class Account(
|
||||
url: Option[String],
|
||||
registeredDate: java.util.Date,
|
||||
updatedDate: java.util.Date,
|
||||
lastLoginDate: Option[java.util.Date]
|
||||
lastLoginDate: Option[java.util.Date],
|
||||
image: Option[String]
|
||||
)
|
||||
|
||||
@@ -23,7 +23,8 @@ trait AccountService {
|
||||
url = url,
|
||||
registeredDate = currentDate,
|
||||
updatedDate = currentDate,
|
||||
lastLoginDate = None)
|
||||
lastLoginDate = None,
|
||||
image = None)
|
||||
|
||||
def updateAccount(account: Account): Unit =
|
||||
Accounts
|
||||
@@ -37,7 +38,10 @@ trait AccountService {
|
||||
account.registeredDate,
|
||||
currentDate,
|
||||
account.lastLoginDate)
|
||||
|
||||
|
||||
def updateAvatarImage(userName: String, image: Option[String]): Unit =
|
||||
Accounts.filter(_.userName is userName.bind).map(_.image.?).update(image)
|
||||
|
||||
def updateLastLoginDate(userName: String): Unit =
|
||||
Accounts.filter(_.userName is userName.bind).map(_.lastLoginDate).update(currentDate)
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ object AutoUpdate {
|
||||
* The history of versions. A head of this sequence is the current BitBucket version.
|
||||
*/
|
||||
val versions = Seq(
|
||||
Version(1, 3),
|
||||
Version(1, 2),
|
||||
Version(1, 1),
|
||||
Version(1, 0)
|
||||
|
||||
@@ -35,6 +35,11 @@ object Directory {
|
||||
def getRepositoryDir(owner: String, repository: String): File =
|
||||
new File("%s/%s/%s.git".format(RepositoryHome, owner, repository))
|
||||
|
||||
/**
|
||||
* Directory for uploaded files by the specified user.
|
||||
*/
|
||||
def getUserUploadDir(userName: String): File = new File("%s/%s/_files".format(GitBucketHome, userName))
|
||||
|
||||
/**
|
||||
* Root of temporary directories for the specified repository.
|
||||
*/
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
<div id="avatar" class="muted small">
|
||||
<div id="clickable">No Image</div>
|
||||
</div>
|
||||
<input type="hidden" name="avatarFileKey" value=""/>
|
||||
<input type="hidden" name="fileId" value=""/>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
@if(account.isDefined){
|
||||
@@ -62,8 +62,10 @@ $(function(){
|
||||
thumbnailHeight: 120
|
||||
});
|
||||
|
||||
dropzone.on("addedfile", function(){
|
||||
dropzone.on("success", function(file, id){
|
||||
$('div#clickable').remove();
|
||||
$('input[name=fileId]').val(id);
|
||||
alert(id);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user