|
|
@ -1,12 +1,18 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"archive/tar" |
|
|
|
"archive/zip" |
|
|
|
"compress/bzip2" |
|
|
|
"compress/gzip" |
|
|
|
"encoding/json" |
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"sort" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
@ -38,11 +44,13 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
|
} |
|
|
|
sizeHuman := humanize.Bytes(uint64(fileInfo.Size())) |
|
|
|
extra := make(map[string]string) |
|
|
|
files := []string{} |
|
|
|
|
|
|
|
file, _ := os.Open(filePath) |
|
|
|
defer file.Close() |
|
|
|
|
|
|
|
header := make([]byte, 512) |
|
|
|
file.Read(header) |
|
|
|
file.Close() |
|
|
|
|
|
|
|
mimetype := mimemagic.Match("", header) |
|
|
|
extension := strings.TrimPrefix(filepath.Ext(fileName), ".") |
|
|
@ -68,37 +76,96 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
|
tpl = Templates["display/audio.html"] |
|
|
|
} else if mimetype == "application/pdf" { |
|
|
|
tpl = Templates["display/pdf.html"] |
|
|
|
} else if mimetype == "application/x-tar" { |
|
|
|
f, _ := os.Open(filePath) |
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
tReadr := tar.NewReader(f) |
|
|
|
for { |
|
|
|
header, err := tReadr.Next() |
|
|
|
if err == io.EOF || err != nil { |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
if header.Typeflag == tar.TypeDir || header.Typeflag == tar.TypeReg { |
|
|
|
files = append(files, header.Name) |
|
|
|
} |
|
|
|
} |
|
|
|
sort.Strings(files) |
|
|
|
|
|
|
|
} else if mimetype == "application/x-gzip" { |
|
|
|
f, _ := os.Open(filePath) |
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
gzf, err := gzip.NewReader(f) |
|
|
|
if err == nil { |
|
|
|
tReadr := tar.NewReader(gzf) |
|
|
|
for { |
|
|
|
header, err := tReadr.Next() |
|
|
|
if err == io.EOF || err != nil { |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
if header.Typeflag == tar.TypeDir || header.Typeflag == tar.TypeReg { |
|
|
|
files = append(files, header.Name) |
|
|
|
} |
|
|
|
} |
|
|
|
sort.Strings(files) |
|
|
|
} |
|
|
|
} else if mimetype == "application/x-bzip" { |
|
|
|
f, _ := os.Open(filePath) |
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
bzf := bzip2.NewReader(f) |
|
|
|
tReadr := tar.NewReader(bzf) |
|
|
|
for { |
|
|
|
header, err := tReadr.Next() |
|
|
|
if err == io.EOF || err != nil { |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
if header.Typeflag == tar.TypeDir || header.Typeflag == tar.TypeReg { |
|
|
|
files = append(files, header.Name) |
|
|
|
} |
|
|
|
} |
|
|
|
sort.Strings(files) |
|
|
|
|
|
|
|
} else if mimetype == "application/zip" { |
|
|
|
f, _ := os.Open(filePath) |
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
zf, err := zip.NewReader(f, fileInfo.Size()) |
|
|
|
if err == nil { |
|
|
|
for _, f := range zf.File { |
|
|
|
files = append(files, f.Name) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else if supportedBinExtension(extension) { |
|
|
|
if fileInfo.Size() < maxDisplayFileSizeBytes { |
|
|
|
bytes, err := ioutil.ReadFile(filePath) |
|
|
|
if err != nil { |
|
|
|
tpl = Templates["display/file.html"] |
|
|
|
} else { |
|
|
|
if err == nil { |
|
|
|
extra["extension"] = extension |
|
|
|
extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension) |
|
|
|
extra["contents"] = string(bytes) |
|
|
|
tpl = Templates["display/bin.html"] |
|
|
|
} |
|
|
|
} else { |
|
|
|
tpl = Templates["display/file.html"] |
|
|
|
} |
|
|
|
} else if extension == "md" { |
|
|
|
if fileInfo.Size() < maxDisplayFileSizeBytes { |
|
|
|
bytes, err := ioutil.ReadFile(filePath) |
|
|
|
if err != nil { |
|
|
|
tpl = Templates["display/file.html"] |
|
|
|
} else { |
|
|
|
if err == nil { |
|
|
|
unsafe := blackfriday.MarkdownCommon(bytes) |
|
|
|
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) |
|
|
|
|
|
|
|
extra["contents"] = string(html) |
|
|
|
tpl = Templates["display/md.html"] |
|
|
|
} |
|
|
|
} else { |
|
|
|
tpl = Templates["display/file.html"] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
// Catch other files
|
|
|
|
if tpl == nil { |
|
|
|
tpl = Templates["display/file.html"] |
|
|
|
} |
|
|
|
|
|
|
@ -108,6 +175,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { |
|
|
|
"size": sizeHuman, |
|
|
|
"expiry": expiryHuman, |
|
|
|
"extra": extra, |
|
|
|
"files": files, |
|
|
|
}, w) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|