@@ -30,50 +30,52 @@ func GetHtmlPreview(dir string, name string) template.HTML {
3030 if err != nil {
3131 return template .HTML ("Cannot preview this file" )
3232 }
33- if isText {
34- if path .Ext (name ) == ".md" {
35- md , err := renderMarkdown (filePath )
36- if err != nil {
37- return template .HTML ("Cannot preview this file" )
38- }
39- return template .HTML (md )
40- } else if path .Ext (filePath ) == ".svg" {
41- return template .HTML ("<img width=\" 100%\" height=\" 100%\" src=\" " + filePath + "\" >" )
42- } else {
43- lexer := lexers .Match (name )
44- if lexer == nil {
45- txt , err := renderText (filePath )
46- if err != nil {
47- return template .HTML ("Cannot preview this file" )
48- }
49- return template .HTML (txt )
50- }
51- code , err := renderCode (filePath )
52- if err != nil {
53- return template .HTML ("Cannot preview this file" )
54- }
55- return template .HTML (code )
33+ ext := path .Ext (filePath )
34+ if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".webp" || ext == ".jpeg" || ext == "svg" {
35+ return template .HTML ("<img width=\" 100%\" height=\" 100%\" src=\" " + filePath + "\" >" )
36+ } else if ext == ".zip" {
37+ htmlZip , err := renderZip (filePath )
38+ if err != nil {
39+ return template .HTML ("Cannot preview this file" )
5640 }
57- } else {
58- ext := path .Ext (filePath )
59- if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".webp" || ext == ".jpeg" {
60- return template .HTML ("<img width=\" 100%\" height=\" 100%\" src=\" " + filePath + "\" >" )
61- } else if ext == ".zip" {
62- htmlZip , err := renderZip (filePath )
41+ return template .HTML (htmlZip )
42+ } else if ext == ".pdf" {
43+ encodedImage , err := renderPdf (filePath )
44+ if err != nil {
45+ log .Println ("ERROR:" , err )
46+ return template .HTML ("Cannot preview this file" )
47+ }
48+ return template .HTML ("<img width=\" 100%\" height=\" 100%\" src=\" " + encodedImage + "\" >" )
49+ } else if ext == ".md" {
50+ md , err := renderMarkdown (filePath )
51+ if err != nil {
52+ return template .HTML ("Cannot preview this file" )
53+ }
54+ return template .HTML (md )
55+ } else if isText {
56+ lexer := lexers .Match (name )
57+ if lexer == nil {
58+ txt , err := renderText (filePath )
6359 if err != nil {
6460 return template .HTML ("Cannot preview this file" )
6561 }
66- return template .HTML (htmlZip )
67- } else if ext == ".pdf" {
68- encodedImage , err := renderPdf (filePath )
62+ return template .HTML (txt )
63+ }
64+ code , err := renderCode (filePath )
65+ if err != nil {
66+ return template .HTML ("Cannot preview this file" )
67+ }
68+ if code == "" {
69+ txt , err := renderText (filePath )
6970 if err != nil {
70- log .Println ("ERROR:" , err )
7171 return template .HTML ("Cannot preview this file" )
7272 }
73- return template .HTML ("<img src= \" " + encodedImage + " \" >" )
73+ return template .HTML (txt )
7474 }
75+ return template .HTML (code )
76+ } else {
77+ return template .HTML ("Cannot preview this file" )
7578 }
76- return template .HTML ("Cannot preview this file" )
7779}
7880
7981func renderPdf (filePath string ) (string , error ) {
0 commit comments