Issue 143: Add preview for image files
This commit is contained in:
		@@ -38,6 +38,8 @@ class IDF_FileUtil
 | 
				
			|||||||
              'skin', 'sln', 'svc', 'vala', 'vb', 'vbproj', 'vbs', 'wsdl', 'xhtml',
 | 
					              'skin', 'sln', 'svc', 'vala', 'vb', 'vbproj', 'vbs', 'wsdl', 'xhtml',
 | 
				
			||||||
              'xml', 'xsd', 'xsl', 'xslt');
 | 
					              '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 $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", );
 | 
					    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
 | 
					     * @param string the content of the file
 | 
				
			||||||
     * @return string
 | 
					     * @return string
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static function highLight($fileinfo, $content)
 | 
					    public static function highLight($fileinfo, $content, $url)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $pretty = '';
 | 
					
 | 
				
			||||||
        if (self::isSupportedExtension($fileinfo[2])) {
 | 
					        if (self::isText($fileinfo)) {
 | 
				
			||||||
            $pretty = ' prettyprint';
 | 
					            $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("<", "<", $content) . '</pre></div>';
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					                $content = '<div id="highlight"><script type="syntaxhighlighter" class="brush: ' . $ext . '">' . $content . '</script></div>';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        $table = array();
 | 
					
 | 
				
			||||||
        $i = 1;
 | 
					        if (self::isImage($fileinfo)) {
 | 
				
			||||||
        /*foreach (self::splitIntoLines($content) as $line) {
 | 
					            $content = '<tr style="border: 0 !important;"><td style="border: 0 !important;">';
 | 
				
			||||||
            $table[] = '<tr class="c-line"><td class="code-lc" id="L'.$i.'"><a href="#L'.$i.'">'.$i.'</a></td>'
 | 
					            $content .= '<img src="' . $url . '" download="' . $fileinfo[1] . '" alt="' . $fileinfo[1] . '"></a></td></tr>';
 | 
				
			||||||
                .'<td class="code mono'.$pretty.'">'.self::emphasizeControlCharacters(Pluf_esc($line)).'</td></tr>';
 | 
					 | 
				
			||||||
            $i++;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return Pluf_Template::markSafe(implode("\n", $table));*/
 | 
					
 | 
				
			||||||
        //var_dump($fileinfo);
 | 
					        return Pluf_Template::markSafe($content);
 | 
				
			||||||
        $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("<", "<", $content)  . '</pre></div>';
 | 
					 | 
				
			||||||
        else
 | 
					 | 
				
			||||||
            $content = '<div id="highlight"><script type="syntaxhighlighter" class="brush: ' . $ext . '">' . $content . '</script></div>';
 | 
					 | 
				
			||||||
        return  Pluf_Template::markSafe($content);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@@ -242,4 +256,12 @@ class IDF_FileUtil
 | 
				
			|||||||
        $ext = array_merge(self::$supportedExtenstions, explode(' ' , $ext));
 | 
					        $ext = array_merge(self::$supportedExtenstions, explode(' ' , $ext));
 | 
				
			||||||
        return (in_array($fileinfo[2], $ext));
 | 
					        return (in_array($fileinfo[2], $ext));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static function isImage($fileinfo) {
 | 
				
			||||||
 | 
					        if (0 === strpos($fileinfo[0], 'image/')) {
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -211,13 +211,13 @@ class IDF_Views_Source
 | 
				
			|||||||
        if ($request_file_info->type != 'tree') {
 | 
					        if ($request_file_info->type != 'tree') {
 | 
				
			||||||
            $info = self::getRequestedFileMimeType($request_file_info,
 | 
					            $info = self::getRequestedFileMimeType($request_file_info,
 | 
				
			||||||
                                                   $commit, $scm);
 | 
					                                                   $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),
 | 
					                $rep = new Pluf_HTTP_Response($scm->getFile($request_file_info),
 | 
				
			||||||
                                              $info[0]);
 | 
					                                              $info[0]);
 | 
				
			||||||
                $rep->headers['Content-Disposition'] = 'attachment; filename="'.$info[1].'"';
 | 
					                $rep->headers['Content-Disposition'] = 'attachment; filename="'.$info[1].'"';
 | 
				
			||||||
                return $rep;
 | 
					                return $rep;
 | 
				
			||||||
            } else {
 | 
					            } 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,
 | 
					                $extra = array('branches' => $branches,
 | 
				
			||||||
                               'tags' => $tags,
 | 
					                               'tags' => $tags,
 | 
				
			||||||
                               'commit' => $commit,
 | 
					                               'commit' => $commit,
 | 
				
			||||||
@@ -358,6 +358,7 @@ class IDF_Views_Source
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public function viewFile($request, $match, $extra)
 | 
					    public function viewFile($request, $match, $extra)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        $project = $request->project;
 | 
				
			||||||
        $title = sprintf(__('%1$s %2$s Source Tree'), (string) $request->project,
 | 
					        $title = sprintf(__('%1$s %2$s Source Tree'), (string) $request->project,
 | 
				
			||||||
                         $this->getScmType($request));
 | 
					                         $this->getScmType($request));
 | 
				
			||||||
        $scm = IDF_Scm::get($request->project);
 | 
					        $scm = IDF_Scm::get($request->project);
 | 
				
			||||||
@@ -377,13 +378,8 @@ class IDF_Views_Source
 | 
				
			|||||||
        $previous = substr($request_file, 0, -strlen($l.' '));
 | 
					        $previous = substr($request_file, 0, -strlen($l.' '));
 | 
				
			||||||
        $scmConf = $request->conf->getVal('scm', 'git');
 | 
					        $scmConf = $request->conf->getVal('scm', 'git');
 | 
				
			||||||
        $props = $scm->getProperties($commit, $request_file);
 | 
					        $props = $scm->getProperties($commit, $request_file);
 | 
				
			||||||
        $cache = Pluf_Cache::factory();
 | 
					        $url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::getFile', [$project->shortname, $commit, $request_file]);
 | 
				
			||||||
        $key = sha1($request_file.$commit);
 | 
					        $content = IDF_FileUtil::highLight($extra['mime'], $scm->getFile($request_file_info), $url);
 | 
				
			||||||
        $content = IDF_FileUtil::highLight($extra['mime'], $scm->getFile($request_file_info));
 | 
					 | 
				
			||||||
        /*if (null === ($content=$cache->get($key))) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $cache->set($key, $content);
 | 
					 | 
				
			||||||
        }*/
 | 
					 | 
				
			||||||
        return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/file.html',
 | 
					        return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/file.html',
 | 
				
			||||||
                                               array(
 | 
					                                               array(
 | 
				
			||||||
                                                     'page_title' => $page_title,
 | 
					                                                     'page_title' => $page_title,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user