Browse Source

Display contents of common archives. Fixes #34

pull/49/head v0.8.1
andreimarcu 9 years ago
parent
commit
d05f0b645b
  1. 92
      display.go
  2. 9
      templates/display/file.html

92
display.go

@ -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 {

9
templates/display/file.html

@ -3,5 +3,14 @@
{% block main %}
<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>
{% if files|length > 0 %}
<p>Contents of the archive:</p>
<ul>
{% for file in files %}
<li>{{ file }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endblock %}
Loading…
Cancel
Save