Display contents of common archives. Fixes #34

This commit is contained in:
andreimarcu 2015-10-07 16:45:41 -04:00
parent edfb80daac
commit d05f0b645b
2 changed files with 89 additions and 12 deletions

View File

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

View File

@ -3,5 +3,14 @@
{% block main %} {% block main %}
<div class="normal display-file"> <div class="normal display-file">
<p class="center">You are requesting <a href="/selif/{{ filename }}">{{ filename }}</a>, <a href="/selif/{{ filename }}">click here</a> to download.</p> <p class="center">You are requesting <a href="/selif/{{ filename }}">{{ filename }}</a>, <a href="/selif/{{ filename }}">click here</a> to download.</p>
{% if files|length > 0 %}
<p>Contents of the archive:</p>
<ul>
{% for file in files %}
<li>{{ file }}</li>
{% endfor %}
</ul>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}