mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
(refs #28)Implementing avatar image uploading.
This commit is contained in:
@@ -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