mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 06:25:51 +01:00
Add link for image file
* Render image tag with link tag on issue and wiki * Correct response content-type of attached image on issue
This commit is contained in:
@@ -275,7 +275,10 @@ trait IssuesControllerBase extends ControllerBase {
|
||||
|
||||
get("/:owner/:repository/_attached/:file")(referrersOnly { repository =>
|
||||
defining(new java.io.File(Directory.getAttachedDir(repository.owner, repository.name), params("file"))){ file =>
|
||||
if(file.exists) file else NotFound
|
||||
if(file.exists) {
|
||||
contentType = FileUtil.getMimeType(file)
|
||||
file
|
||||
} else NotFound
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -5,9 +5,13 @@ import java.net.URLConnection
|
||||
import java.io.File
|
||||
import util.ControlUtil._
|
||||
import scala.util.Random
|
||||
import eu.medsea.mimeutil.{MimeUtil2, MimeType}
|
||||
|
||||
object FileUtil {
|
||||
|
||||
private val mimeUtil = new MimeUtil2()
|
||||
mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector")
|
||||
|
||||
def getMimeType(name: String): String =
|
||||
defining(URLConnection.getFileNameMap()){ fileNameMap =>
|
||||
fileNameMap.getContentTypeFor(name) match {
|
||||
@@ -16,6 +20,16 @@ object FileUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns mime type detected by file content.
|
||||
*
|
||||
* @param file File object
|
||||
* @return mime type String
|
||||
*/
|
||||
def getMimeType(file: File): String = {
|
||||
MimeUtil2.getMostSpecificMimeType(mimeUtil.getMimeTypes(file, new MimeType("application/octet-stream"))).toString
|
||||
}
|
||||
|
||||
def getContentType(name: String, bytes: Array[Byte]): String = {
|
||||
defining(getMimeType(name)){ mimeType =>
|
||||
if(mimeType == "application/octet-stream" && isText(bytes)){
|
||||
|
||||
@@ -89,7 +89,8 @@ class GitBucketHtmlSerializer(
|
||||
) with LinkConverter with RequestCache {
|
||||
|
||||
override protected def printImageTag(imageNode: SuperNode, url: String): Unit =
|
||||
printer.print("<img src=\"").print(fixUrl(url)).print("\" alt=\"").printEncoded(printChildrenToString(imageNode)).print("\"/>")
|
||||
printer.print("<a target=\"_blank\" href=\"").print(fixUrl(url)).print("\">")
|
||||
.print("<img src=\"").print(fixUrl(url)).print("\" alt=\"").printEncoded(printChildrenToString(imageNode)).print("\"/></a>")
|
||||
|
||||
override protected def printLink(rendering: LinkRenderer.Rendering): Unit = {
|
||||
printer.print('<').print('a')
|
||||
|
||||
Reference in New Issue
Block a user