mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
(refs #251)Remove BOM from UTF-8 string.
This commit is contained in:
@@ -3,7 +3,7 @@ package service
|
|||||||
import java.util.Date
|
import java.util.Date
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
import org.apache.commons.io.FileUtils
|
import org.apache.commons.io.FileUtils
|
||||||
import util.{PatchUtil, Directory, JGitUtil, LockUtil}
|
import util._
|
||||||
import _root_.util.ControlUtil._
|
import _root_.util.ControlUtil._
|
||||||
import org.eclipse.jgit.treewalk.{TreeWalk, CanonicalTreeParser}
|
import org.eclipse.jgit.treewalk.{TreeWalk, CanonicalTreeParser}
|
||||||
import org.eclipse.jgit.lib._
|
import org.eclipse.jgit.lib._
|
||||||
@@ -14,6 +14,7 @@ import java.io.ByteArrayInputStream
|
|||||||
import org.eclipse.jgit.patch._
|
import org.eclipse.jgit.patch._
|
||||||
import org.eclipse.jgit.api.errors.PatchFormatException
|
import org.eclipse.jgit.api.errors.PatchFormatException
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
|
import scala.Some
|
||||||
|
|
||||||
|
|
||||||
object WikiService {
|
object WikiService {
|
||||||
@@ -61,7 +62,8 @@ trait WikiService {
|
|||||||
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
||||||
if(!JGitUtil.isEmpty(git)){
|
if(!JGitUtil.isEmpty(git)){
|
||||||
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
|
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
|
||||||
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"), file.committer, file.time, file.commitId)
|
WikiPageInfo(file.name, StringUtil.convertFromByteArray(git.getRepository.open(file.id).getBytes),
|
||||||
|
file.committer, file.time, file.commitId)
|
||||||
}
|
}
|
||||||
} else None
|
} else None
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,9 +63,9 @@ object FileUtil {
|
|||||||
if(dir.exists()){
|
if(dir.exists()){
|
||||||
FileUtils.deleteDirectory(dir)
|
FileUtils.deleteDirectory(dir)
|
||||||
}
|
}
|
||||||
try{
|
try {
|
||||||
action(dir)
|
action(dir)
|
||||||
}finally{
|
} finally {
|
||||||
FileUtils.deleteDirectory(dir)
|
FileUtils.deleteDirectory(dir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package util
|
|||||||
import java.net.{URLDecoder, URLEncoder}
|
import java.net.{URLDecoder, URLEncoder}
|
||||||
import org.mozilla.universalchardet.UniversalDetector
|
import org.mozilla.universalchardet.UniversalDetector
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
|
import org.apache.commons.io.input.BOMInputStream
|
||||||
|
import org.apache.commons.io.IOUtils
|
||||||
|
|
||||||
object StringUtil {
|
object StringUtil {
|
||||||
|
|
||||||
@@ -27,7 +29,12 @@ object StringUtil {
|
|||||||
def escapeHtml(value: String): String =
|
def escapeHtml(value: String): String =
|
||||||
value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """)
|
value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """)
|
||||||
|
|
||||||
def convertFromByteArray(content: Array[Byte]): String = new String(content, detectEncoding(content))
|
/**
|
||||||
|
* Make string from byte array. Character encoding is detected automatically by [[util.StringUtil.detectEncoding]].
|
||||||
|
* And if given bytes contains UTF-8 BOM, it's removed from returned string..
|
||||||
|
*/
|
||||||
|
def convertFromByteArray(content: Array[Byte]): String =
|
||||||
|
IOUtils.toString(new BOMInputStream(new java.io.ByteArrayInputStream(content)), detectEncoding(content))
|
||||||
|
|
||||||
def detectEncoding(content: Array[Byte]): String =
|
def detectEncoding(content: Array[Byte]): String =
|
||||||
defining(new UniversalDetector(null)){ detector =>
|
defining(new UniversalDetector(null)){ detector =>
|
||||||
|
|||||||
Reference in New Issue
Block a user