Merge pull request #41 from dyreshark/master

Cleanup
This commit is contained in:
Andrei Marcu 2015-10-06 03:01:33 -04:00
commit a5d4f754e7
6 changed files with 38 additions and 66 deletions

View File

@ -17,6 +17,8 @@ import (
"github.com/zenazn/goji/web"
)
const maxDisplayFileSizeBytes = 1024 * 512
func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
filePath := path.Join(Config.filesDir, fileName)
@ -29,8 +31,8 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
expiry, _ := metadataGetExpiry(fileName)
var expiryHuman string
if expiry != 0 {
expiryHuman = humanize.RelTime(time.Now(), time.Unix(expiry, 0), "", "")
if !expiry.IsZero() {
expiryHuman = humanize.RelTime(time.Now(), expiry, "", "")
}
sizeHuman := humanize.Bytes(uint64(fileInfo.Size()))
extra := make(map[string]string)
@ -47,7 +49,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
js, _ := json.Marshal(map[string]string{
"filename": fileName,
"mimetype": mimetype,
"expiry": strconv.FormatInt(expiry, 10),
"expiry": strconv.FormatInt(expiry.Unix(), 10),
"size": strconv.FormatInt(fileInfo.Size(), 10),
})
w.Write(js)
@ -65,7 +67,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
} else if mimetype == "application/pdf" {
tpl = Templates["display/pdf.html"]
} else if supportedBinExtension(extension) {
if fileInfo.Size() < 500000 {
if fileInfo.Size() < maxDisplayFileSizeBytes {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
tpl = Templates["display/file.html"]

View File

@ -4,37 +4,14 @@ import (
"time"
)
// Get what the unix timestamp will be in "seconds".
func getFutureTimestamp(seconds int64) (ts int64) {
now := int64(time.Now().Unix())
if seconds == 0 {
ts = 0
} else {
ts = now + seconds
}
return
}
// Determine if a file with expiry set to "ts" has expired yet
func isTsExpired(ts int64) (expired bool) {
now := int64(time.Now().Unix())
if ts == 0 {
expired = false
} else if now > ts {
expired = true
} else {
expired = false
}
return
func isTsExpired(ts time.Time) bool {
now := time.Now()
return !ts.IsZero() && now.After(ts)
}
// Determine if the given filename is expired
func isFileExpired(filename string) bool {
exp, _ := metadataGetExpiry(filename)
return isTsExpired(exp)
}

24
meta.go
View File

@ -7,6 +7,7 @@ import (
"os"
"path"
"strconv"
"time"
)
// Write metadata from Upload struct to file
@ -47,29 +48,24 @@ func metadataRead(filename string) ([]string, error) {
return lines, scanner.Err()
}
func metadataGetExpiry(filename string) (int64, error) {
func metadataGetExpiry(filename string) (expiry time.Time, err error) {
metadata, err := metadataRead(filename)
if len(metadata) < 1 {
err := errors.New("ERR: Metadata file does not contain expiry")
return 0, err
}
// XXX in this case it's up to the caller to determine proper behavior
// for a nonexistant metadata file or broken file
if err != nil {
return 0, err
return
}
var expiry int64
expiry, err = strconv.ParseInt(metadata[0], 10, 32)
if err != nil {
return 0, err
} else {
return int64(expiry), err
if len(metadata) < 1 {
err = errors.New("ERR: Metadata file does not contain expiry")
return
}
expirySecs, err := strconv.ParseInt(metadata[0], 10, 64)
expiry = time.Unix(expirySecs, 0)
return
}
func metadataGetDeleteKey(filename string) (string, error) {

View File

@ -2,7 +2,6 @@ package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
@ -22,15 +21,15 @@ var Config struct {
bind string
filesDir string
metaDir string
noLogs bool
allowHotlink bool
siteName string
siteURL string
fastcgi bool
remoteUploads bool
contentSecurityPolicy string
fileContentSecurityPolicy string
xFrameOptions string
noLogs bool
allowHotlink bool
fastcgi bool
remoteUploads bool
}
var Templates = make(map[string]*pongo2.Template)
@ -52,14 +51,12 @@ func setup() {
// make directories if needed
err := os.MkdirAll(Config.filesDir, 0755)
if err != nil {
fmt.Println("Error: could not create files directory")
os.Exit(1)
log.Fatal("Could not create files directory:", err)
}
err = os.MkdirAll(Config.metaDir, 0700)
if err != nil {
fmt.Println("Error: could not create metadata directory")
os.Exit(1)
log.Fatal("Could not create metadata directory:", err)
}
// ensure siteURL ends wth '/'
@ -70,15 +67,13 @@ func setup() {
// Template setup
p2l, err := NewPongo2TemplatesLoader()
if err != nil {
fmt.Println("Error: could not load templates")
os.Exit(1)
log.Fatal("Error: could not load templates", err)
}
TemplateSet := pongo2.NewSet("templates", p2l)
TemplateSet.Globals["sitename"] = Config.siteName
err = populateTemplatesMap(TemplateSet, Templates)
if err != nil {
fmt.Println("Error: could not load templates")
os.Exit(1)
log.Fatal("Error: could not load templates", err)
}
staticBox = rice.MustFindBox("static")

View File

@ -41,8 +41,7 @@ func (fs *Pongo2Loader) Abs(base, name string) string {
}
func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Template) error {
templates := [...]string{
templates := []string{
"index.html",
"paste.html",
"404.html",

View File

@ -14,6 +14,7 @@ import (
"regexp"
"strconv"
"strings"
"time"
"bitbucket.org/taruti/mimemagic"
"github.com/dchest/uniuri"
@ -32,7 +33,7 @@ var fileBlacklist = map[string]bool{
type UploadRequest struct {
src io.Reader
filename string
expiry int64 // Seconds until expiry, 0 = never
expiry time.Duration // Seconds until expiry, 0 = never
randomBarename bool
deletionKey string // Empty string if not defined
}
@ -41,7 +42,7 @@ type UploadRequest struct {
type Upload struct {
Filename string // Final filename on disk
Size int64
Expiry int64 // Unix timestamp of expiry, 0=never
Expiry time.Time // Unix timestamp of expiry, 0=never
DeleteKey string // Deletion key, one generated if not provided
}
@ -243,7 +244,9 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
defer dst.Close()
// Get the rest of the metadata needed for storage
upload.Expiry = getFutureTimestamp(upReq.expiry)
if upReq.expiry != 0 {
upload.Expiry = time.Now().Add(upReq.expiry)
}
// If no delete key specified, pick a random one.
if upReq.deletionKey == "" {
@ -279,7 +282,7 @@ func generateJSONresponse(upload Upload) []byte {
"url": Config.siteURL + upload.Filename,
"filename": upload.Filename,
"delete_key": upload.DeleteKey,
"expiry": strconv.FormatInt(int64(upload.Expiry), 10),
"expiry": strconv.FormatInt(upload.Expiry.Unix(), 10),
"size": strconv.FormatInt(upload.Size, 10),
})
@ -302,7 +305,7 @@ func barePlusExt(filename string) (barename, extension string) {
return
}
func parseExpiry(expStr string) int64 {
func parseExpiry(expStr string) time.Duration {
if expStr == "" {
return 0
} else {
@ -310,7 +313,7 @@ func parseExpiry(expStr string) int64 {
if err != nil {
return 0
} else {
return int64(expiry)
return time.Duration(expiry) * time.Second
}
}
}