mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 22:45:51 +01:00
(refs #28)Implementing avatar image uploading.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user