mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 05:55:51 +01:00
Removed FileUploadControllerBase(in the middle of improving file
upload).
This commit is contained in:
@@ -11,8 +11,7 @@ import jp.sf.amateras.scalatra.forms._
|
|||||||
import org.apache.commons.io.FileUtils
|
import org.apache.commons.io.FileUtils
|
||||||
import model.Account
|
import model.Account
|
||||||
import service.{SystemSettingsService, AccountService}
|
import service.{SystemSettingsService, AccountService}
|
||||||
import javax.servlet.http.{HttpServletResponse, HttpSession, HttpServletRequest}
|
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import javax.servlet.{FilterChain, ServletResponse, ServletRequest}
|
import javax.servlet.{FilterChain, ServletResponse, ServletRequest}
|
||||||
import org.scalatra.i18n._
|
import org.scalatra.i18n._
|
||||||
|
|
||||||
@@ -164,7 +163,7 @@ case class Context(settings: SystemSettingsService.SystemSettings, loginAccount:
|
|||||||
/**
|
/**
|
||||||
* Base trait for controllers which manages account information.
|
* Base trait for controllers which manages account information.
|
||||||
*/
|
*/
|
||||||
trait AccountManagementControllerBase extends ControllerBase with FileUploadControllerBase {
|
trait AccountManagementControllerBase extends ControllerBase {
|
||||||
self: AccountService =>
|
self: AccountService =>
|
||||||
|
|
||||||
protected def updateImage(userName: String, fileId: Option[String], clearImage: Boolean): Unit =
|
protected def updateImage(userName: String, fileId: Option[String], clearImage: Boolean): Unit =
|
||||||
@@ -175,9 +174,9 @@ trait AccountManagementControllerBase extends ControllerBase with FileUploadCont
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fileId.map { fileId =>
|
fileId.map { fileId =>
|
||||||
val filename = "avatar." + FileUtil.getExtension(getUploadedFilename(fileId).get)
|
val filename = "avatar." + FileUtil.getExtension(session.getAndRemove(Keys.Session.Upload(fileId)).get)
|
||||||
FileUtils.moveFile(
|
FileUtils.moveFile(
|
||||||
getTemporaryFile(fileId),
|
new java.io.File(getTemporaryDir(session.getId), fileId),
|
||||||
new java.io.File(getUserUploadDir(userName), filename)
|
new java.io.File(getUserUploadDir(userName), filename)
|
||||||
)
|
)
|
||||||
updateAvatarImage(userName, Some(filename))
|
updateAvatarImage(userName, Some(filename))
|
||||||
@@ -197,28 +196,3 @@ trait AccountManagementControllerBase extends ControllerBase with FileUploadCont
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Base trait for controllers which needs file uploading feature.
|
|
||||||
*/
|
|
||||||
trait FileUploadControllerBase {
|
|
||||||
|
|
||||||
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, s"tmp/_upload/${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] =
|
|
||||||
session.getAndRemove[String](Keys.Session.Upload(fileId))
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,31 +1,40 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import _root_.util.{Keys, FileUtil}
|
import util.{Keys, FileUtil}
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
|
import util.Directory._
|
||||||
import org.scalatra._
|
import org.scalatra._
|
||||||
import org.scalatra.servlet.{MultipartConfig, FileUploadSupport}
|
import org.scalatra.servlet.{MultipartConfig, FileUploadSupport, FileItem}
|
||||||
import org.apache.commons.io.FileUtils
|
import org.apache.commons.io.FileUtils
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides Ajax based file upload functionality.
|
* Provides Ajax based file upload functionality.
|
||||||
*
|
*
|
||||||
* This servlet saves uploaded file as temporary file and returns the unique id.
|
* This servlet saves uploaded file.
|
||||||
* You can get uploaded file using [[app.FileUploadControllerBase#getTemporaryFile()]] with this id.
|
|
||||||
*/
|
*/
|
||||||
class FileUploadController extends ScalatraServlet with FileUploadSupport with FileUploadControllerBase {
|
class FileUploadController extends ScalatraServlet with FileUploadSupport {
|
||||||
|
|
||||||
configureMultipartHandling(MultipartConfig(maxFileSize = Some(3 * 1024 * 1024)))
|
configureMultipartHandling(MultipartConfig(maxFileSize = Some(3 * 1024 * 1024)))
|
||||||
|
|
||||||
post("/image"){
|
post("/image"){
|
||||||
fileParams.get("file") match {
|
execute { (file, fileId) =>
|
||||||
case Some(file) if(FileUtil.isImage(file.name)) => defining(generateFileId){ fileId =>
|
FileUtils.writeByteArrayToFile(new java.io.File(getTemporaryDir(session.getId), fileId), file.get)
|
||||||
FileUtils.writeByteArrayToFile(getTemporaryFile(fileId), file.get)
|
|
||||||
session += Keys.Session.Upload(fileId) -> file.name
|
session += Keys.Session.Upload(fileId) -> file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post("/image/:owner/:repository"){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private def execute(f: (FileItem, String) => Unit) = fileParams.get("file") match {
|
||||||
|
case Some(file) if(FileUtil.isImage(file.name)) =>
|
||||||
|
defining(FileUtil.generateFileId){ fileId =>
|
||||||
|
f(file, fileId)
|
||||||
|
|
||||||
Ok(fileId)
|
Ok(fileId)
|
||||||
}
|
}
|
||||||
case None => BadRequest
|
case _ => BadRequest
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
package servlet
|
package servlet
|
||||||
|
|
||||||
import javax.servlet.http.{HttpSessionEvent, HttpSessionListener}
|
import javax.servlet.http.{HttpSessionEvent, HttpSessionListener}
|
||||||
import app.FileUploadControllerBase
|
import org.apache.commons.io.FileUtils
|
||||||
|
import util.Directory._
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes session associated temporary files when session is destroyed.
|
* Removes session associated temporary files when session is destroyed.
|
||||||
*/
|
*/
|
||||||
class SessionCleanupListener extends HttpSessionListener with FileUploadControllerBase {
|
class SessionCleanupListener extends HttpSessionListener {
|
||||||
|
|
||||||
def sessionCreated(se: HttpSessionEvent): Unit = {}
|
def sessionCreated(se: HttpSessionEvent): Unit = {}
|
||||||
|
|
||||||
def sessionDestroyed(se: HttpSessionEvent): Unit = removeTemporaryFiles()(se.getSession)
|
def sessionDestroyed(se: HttpSessionEvent): Unit = FileUtils.deleteDirectory(getTemporaryDir(se.getSession.getId))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,31 +34,29 @@ object Directory {
|
|||||||
|
|
||||||
val DatabaseHome = s"${GitBucketHome}/data"
|
val DatabaseHome = s"${GitBucketHome}/data"
|
||||||
|
|
||||||
/**
|
|
||||||
* Repository names of the specified user.
|
|
||||||
*/
|
|
||||||
def getRepositories(owner: String): List[String] =
|
|
||||||
defining(new File(s"${RepositoryHome}/${owner}")){ dir =>
|
|
||||||
if(dir.exists){
|
|
||||||
dir.listFiles.filter { file =>
|
|
||||||
file.isDirectory && !file.getName.endsWith(".wiki.git")
|
|
||||||
}.map(_.getName.replaceFirst("\\.git$", "")).toList
|
|
||||||
} else {
|
|
||||||
Nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Substance directory of the repository.
|
* Substance directory of the repository.
|
||||||
*/
|
*/
|
||||||
def getRepositoryDir(owner: String, repository: String): File =
|
def getRepositoryDir(owner: String, repository: String): File =
|
||||||
new File(s"${RepositoryHome}/${owner}/${repository}.git")
|
new File(s"${RepositoryHome}/${owner}/${repository}.git")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directory for files which are attached to issue.
|
||||||
|
*/
|
||||||
|
def getAttachedDir(owner: String, repository: String): File =
|
||||||
|
new File(s"${RepositoryHome}/${owner}/${repository}/issues")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Directory for uploaded files by the specified user.
|
* Directory for uploaded files by the specified user.
|
||||||
*/
|
*/
|
||||||
def getUserUploadDir(userName: String): File = new File(s"${GitBucketHome}/data/${userName}/files")
|
def getUserUploadDir(userName: String): File = new File(s"${GitBucketHome}/data/${userName}/files")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Root of temporary directories for the upload file.
|
||||||
|
*/
|
||||||
|
def getTemporaryDir(sessionId: String): File =
|
||||||
|
new File(s"${GitBucketHome}/tmp/_upload/${sessionId}")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Root of temporary directories for the specified repository.
|
* Root of temporary directories for the specified repository.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.apache.commons.io.FileUtils
|
|||||||
import java.net.URLConnection
|
import java.net.URLConnection
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
|
import scala.util.Random
|
||||||
|
|
||||||
object FileUtil {
|
object FileUtil {
|
||||||
|
|
||||||
@@ -31,23 +32,7 @@ object FileUtil {
|
|||||||
|
|
||||||
def isText(content: Array[Byte]): Boolean = !content.contains(0)
|
def isText(content: Array[Byte]): Boolean = !content.contains(0)
|
||||||
|
|
||||||
// def createZipFile(dest: File, dir: File): Unit = {
|
def generateFileId: String = System.currentTimeMillis + Random.alphanumeric.take(10).mkString
|
||||||
// def addDirectoryToZip(out: ZipArchiveOutputStream, dir: File, path: String): Unit = {
|
|
||||||
// dir.listFiles.map { file =>
|
|
||||||
// if(file.isFile){
|
|
||||||
// out.putArchiveEntry(new ZipArchiveEntry(path + "/" + file.getName))
|
|
||||||
// out.write(FileUtils.readFileToByteArray(file))
|
|
||||||
// out.closeArchiveEntry
|
|
||||||
// } else if(file.isDirectory){
|
|
||||||
// addDirectoryToZip(out, file, path + "/" + file.getName)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// using(new ZipArchiveOutputStream(dest)){ out =>
|
|
||||||
// addDirectoryToZip(out, dir, dir.getName)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
def getFileName(path: String): String = defining(path.lastIndexOf('/')){ i =>
|
def getFileName(path: String): String = defining(path.lastIndexOf('/')){ i =>
|
||||||
if(i >= 0) path.substring(i + 1) else path
|
if(i >= 0) path.substring(i + 1) else path
|
||||||
|
|||||||
Reference in New Issue
Block a user