mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-12-24 19:30:18 +01:00
implementing preview
This commit is contained in:
parent
3f01f58b1e
commit
d2b9546f59
5 changed files with 94 additions and 40 deletions
|
@ -35,7 +35,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"github.com/dutchcoders/go-clamd"
|
||||
"github.com/golang/gddo/httputil/header"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/kennygrant/sanitize"
|
||||
html_template "html/template"
|
||||
|
@ -57,23 +56,60 @@ func healthHandler(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprintf(w, "Approaching Neutral Zone, all systems normal and functioning.")
|
||||
}
|
||||
|
||||
/* The preview handler will show a preview of the content for browsers (accept type text/html), and referer is not transfer.sh */
|
||||
func previewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("preview")
|
||||
|
||||
vars := mux.Vars(r)
|
||||
|
||||
token := vars["token"]
|
||||
filename := vars["filename"]
|
||||
|
||||
reader, contentType, contentLength, err := storage.Get(token, filename)
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
reader.Close()
|
||||
|
||||
templatePath := "static/download.html"
|
||||
|
||||
if strings.HasPrefix(contentType, "image") {
|
||||
templatePath = "static/download.image.html"
|
||||
}
|
||||
|
||||
tmpl, err := html_template.ParseFiles(templatePath)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := struct {
|
||||
ContentType string
|
||||
Filename string
|
||||
Url string
|
||||
ContentLength uint64
|
||||
}{
|
||||
contentType,
|
||||
filename,
|
||||
r.URL.String(),
|
||||
contentLength,
|
||||
}
|
||||
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// this handler will output html or text, depending on the
|
||||
// support of the client (Accept header).
|
||||
|
||||
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// vars := mux.Vars(r)
|
||||
|
||||
actual := header.ParseAccept(r.Header, "Accept")
|
||||
|
||||
html := false
|
||||
|
||||
for _, s := range actual {
|
||||
if s.Value == "text/html" {
|
||||
html = true
|
||||
}
|
||||
}
|
||||
|
||||
if html {
|
||||
if acceptsHtml(r.Header) {
|
||||
tmpl, err := html_template.ParseFiles("static/index.html")
|
||||
|
||||
if err != nil {
|
||||
|
@ -482,20 +518,7 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10))
|
||||
|
||||
mediaType, _, _ := mime.ParseMediaType(contentType)
|
||||
|
||||
switch {
|
||||
case mediaType == "text/html":
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
||||
break
|
||||
case strings.HasPrefix(mediaType, "text"):
|
||||
case mediaType == "":
|
||||
break
|
||||
default:
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
||||
}
|
||||
|
||||
w.Header().Set("Connection", "close")
|
||||
|
||||
if _, err = io.Copy(w, reader); err != nil {
|
||||
|
|
|
@ -34,6 +34,7 @@ import (
|
|||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
@ -79,23 +80,26 @@ func main() {
|
|||
r.HandleFunc("/({files:.*}).tar.gz", tarGzHandler).Methods("GET")
|
||||
r.HandleFunc("/download/{token}/{filename}", getHandler).Methods("GET")
|
||||
|
||||
/*r.HandleFunc("/{token}/{filename}", viewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
|
||||
r.HandleFunc("/{token}/{filename}", previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
|
||||
if !acceptsHtml(r.Header) {
|
||||
return false
|
||||
}
|
||||
|
||||
match := (r.Referer() == "")
|
||||
|
||||
u, err := url.Parse(r.Referer())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return true
|
||||
}
|
||||
|
||||
if u.Host == "transfer.sh" {
|
||||
return false
|
||||
}
|
||||
|
||||
if u.Host == "" {
|
||||
return false
|
||||
}
|
||||
match = match || (u.Host == "transfer.sh")
|
||||
|
||||
return true
|
||||
}).Methods("GET")*/
|
||||
match = match || (u.Host == "127.0.0.1")
|
||||
|
||||
log.Printf("%s %s match %s", r.Referer(), u.Host, match)
|
||||
return match
|
||||
}).Methods("GET")
|
||||
|
||||
r.HandleFunc("/{token}/{filename}", getHandler).Methods("GET")
|
||||
r.HandleFunc("/get/{token}/{filename}", getHandler).Methods("GET")
|
||||
|
|
7
transfersh-server/static/download.image.html
Normal file
7
transfersh-server/static/download.image.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
|
||||
{{.ContentType}}
|
||||
{{.ContentLength}}
|
||||
{{.Filename}}
|
||||
<a href="{{.Url}}">Download</a>
|
||||
</html>
|
|
@ -27,6 +27,7 @@ package main
|
|||
import (
|
||||
"github.com/goamz/goamz/aws"
|
||||
"github.com/goamz/goamz/s3"
|
||||
"github.com/golang/gddo/httputil/header"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"strings"
|
||||
|
@ -78,3 +79,15 @@ func encodeRFC2047(String string) string {
|
|||
addr := mail.Address{String, ""}
|
||||
return strings.Trim(addr.String(), " <>")
|
||||
}
|
||||
|
||||
func acceptsHtml(hdr http.Header) bool {
|
||||
actual := header.ParseAccept(hdr, "Accept")
|
||||
|
||||
for _, s := range actual {
|
||||
if s.Value == "text/html" {
|
||||
return (true)
|
||||
}
|
||||
}
|
||||
|
||||
return (false)
|
||||
}
|
||||
|
|
7
transfersh-web/download.image.html
Normal file
7
transfersh-web/download.image.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
|
||||
{{.ContentType}}
|
||||
{{.ContentLength}}
|
||||
{{.Filename}}
|
||||
<a href="{{.Url}}">Download</a>
|
||||
</html>
|
Loading…
Reference in a new issue