Issue 143: Add preview for image files

master
Nathan Adams 2016-03-19 19:08:15 -05:00
parent 24cf41de2b
commit 957111d6ad
2 changed files with 51 additions and 33 deletions

View File

@ -38,6 +38,8 @@ class IDF_FileUtil
'skin', 'sln', 'svc', 'vala', 'vb', 'vbproj', 'vbs', 'wsdl', 'xhtml',
'xml', 'xsd', 'xsl', 'xslt');
public static $imageExtensions = ["png", "gif", "jpeg", "jpg"];
public static $map = array("cxx" => "cpp", "h" => "cpp", "hpp" => "cpp", "rc"=>"text", "sh"=>"bash", "cs"=>"csharp");
public static $syntaxhighlightext = array("html", "as3", "cf", "cpp", "c", "css", "pas", "diff", "patch", "erl", "java", "jfx", "js", "pl", "php", "py", "rb", "sass", "scss", "scala", "sql", "vb", );
@ -62,34 +64,46 @@ class IDF_FileUtil
* @param string the content of the file
* @return string
*/
public static function highLight($fileinfo, $content)
public static function highLight($fileinfo, $content, $url)
{
$pretty = '';
if (self::isSupportedExtension($fileinfo[2])) {
$pretty = ' prettyprint';
if (self::isText($fileinfo)) {
$pretty = '';
if (self::isSupportedExtension($fileinfo[2])) {
$pretty = ' prettyprint';
}
$table = array();
$i = 1;
/*foreach (self::splitIntoLines($content) as $line) {
$table[] = '<tr class="c-line"><td class="code-lc" id="L'.$i.'"><a href="#L'.$i.'">'.$i.'</a></td>'
.'<td class="code mono'.$pretty.'">'.self::emphasizeControlCharacters(Pluf_esc($line)).'</td></tr>';
$i++;
}
return Pluf_Template::markSafe(implode("\n", $table));*/
//var_dump($fileinfo);
$ext = "";
if (in_array($fileinfo[2], self::$syntaxhighlightext))
$ext = $fileinfo[2];
elseif (array_key_exists($fileinfo[2], self::$map))
$ext = self::$map[$fileinfo[2]];
else
$ext = "text";
if ($ext == "php" || $ext == "html" || $ext == "htm" || $ext == "js")
$content = '<div id="highlight"><pre class="brush: ' . $ext . '">' . str_replace("<", "&lt;", $content) . '</pre></div>';
else
$content = '<div id="highlight"><script type="syntaxhighlighter" class="brush: ' . $ext . '">' . $content . '</script></div>';
}
$table = array();
$i = 1;
/*foreach (self::splitIntoLines($content) as $line) {
$table[] = '<tr class="c-line"><td class="code-lc" id="L'.$i.'"><a href="#L'.$i.'">'.$i.'</a></td>'
.'<td class="code mono'.$pretty.'">'.self::emphasizeControlCharacters(Pluf_esc($line)).'</td></tr>';
$i++;
if (self::isImage($fileinfo)) {
$content = '<tr style="border: 0 !important;"><td style="border: 0 !important;">';
$content .= '<img src="' . $url . '" download="' . $fileinfo[1] . '" alt="' . $fileinfo[1] . '"></a></td></tr>';
}
return Pluf_Template::markSafe(implode("\n", $table));*/
//var_dump($fileinfo);
$ext = "";
if (in_array($fileinfo[2], self::$syntaxhighlightext))
$ext = $fileinfo[2];
elseif (array_key_exists($fileinfo[2], self::$map))
$ext = self::$map[$fileinfo[2]];
else
$ext = "text";
if ($ext == "php" || $ext == "html" || $ext == "htm" || $ext == "js")
$content = '<div id="highlight"><pre class="brush: ' . $ext . '">' . str_replace("<", "&lt;", $content) . '</pre></div>';
else
$content = '<div id="highlight"><script type="syntaxhighlighter" class="brush: ' . $ext . '">' . $content . '</script></div>';
return Pluf_Template::markSafe($content);
return Pluf_Template::markSafe($content);
}
/**
@ -242,4 +256,12 @@ class IDF_FileUtil
$ext = array_merge(self::$supportedExtenstions, explode(' ' , $ext));
return (in_array($fileinfo[2], $ext));
}
public static function isImage($fileinfo) {
if (0 === strpos($fileinfo[0], 'image/')) {
return true;
}
return false;
}
}

View File

@ -211,13 +211,13 @@ class IDF_Views_Source
if ($request_file_info->type != 'tree') {
$info = self::getRequestedFileMimeType($request_file_info,
$commit, $scm);
if (!IDF_FileUtil::isText($info)) {
if (!IDF_FileUtil::isText($info) && !IDF_FileUtil::isImage($info)) {
$rep = new Pluf_HTTP_Response($scm->getFile($request_file_info),
$info[0]);
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$info[1].'"';
return $rep;
} else {
// We want to display the content of the file as text
// We want to display the content of the file
$extra = array('branches' => $branches,
'tags' => $tags,
'commit' => $commit,
@ -358,6 +358,7 @@ class IDF_Views_Source
*/
public function viewFile($request, $match, $extra)
{
$project = $request->project;
$title = sprintf(__('%1$s %2$s Source Tree'), (string) $request->project,
$this->getScmType($request));
$scm = IDF_Scm::get($request->project);
@ -377,13 +378,8 @@ class IDF_Views_Source
$previous = substr($request_file, 0, -strlen($l.' '));
$scmConf = $request->conf->getVal('scm', 'git');
$props = $scm->getProperties($commit, $request_file);
$cache = Pluf_Cache::factory();
$key = sha1($request_file.$commit);
$content = IDF_FileUtil::highLight($extra['mime'], $scm->getFile($request_file_info));
/*if (null === ($content=$cache->get($key))) {
$cache->set($key, $content);
}*/
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::getFile', [$project->shortname, $commit, $request_file]);
$content = IDF_FileUtil::highLight($extra['mime'], $scm->getFile($request_file_info), $url);
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/file.html',
array(
'page_title' => $page_title,