From 4330d605e349453f3d892348c9d4ddbd8c74f908 Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Mon, 5 Oct 2015 23:49:57 -0700 Subject: [PATCH 1/3] Clean up logging on start --- server.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/server.go b/server.go index 312de95..37e2e1b 100644 --- a/server.go +++ b/server.go @@ -2,7 +2,6 @@ package main import ( "flag" - "fmt" "log" "net" "net/http" @@ -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") From 1e421e07cd27e501a15d601c121ca910f4b2c44b Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Mon, 5 Oct 2015 23:50:20 -0700 Subject: [PATCH 2/3] swap to using time types instead of ints --- display.go | 10 ++++++---- expiry.go | 29 +++-------------------------- meta.go | 24 ++++++++++-------------- upload.go | 17 ++++++++++------- 4 files changed, 29 insertions(+), 51 deletions(-) diff --git a/display.go b/display.go index 5fc26c7..6503c28 100644 --- a/display.go +++ b/display.go @@ -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"] diff --git a/expiry.go b/expiry.go index 0444842..d8b4ba5 100644 --- a/expiry.go +++ b/expiry.go @@ -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) } diff --git a/meta.go b/meta.go index f2165e0..adfb969 100644 --- a/meta.go +++ b/meta.go @@ -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) { diff --git a/upload.go b/upload.go index 3864896..d68c055 100644 --- a/upload.go +++ b/upload.go @@ -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,8 +42,8 @@ type UploadRequest struct { type Upload struct { Filename string // Final filename on disk Size int64 - Expiry int64 // Unix timestamp of expiry, 0=never - DeleteKey string // Deletion key, one generated if not provided + Expiry time.Time // Unix timestamp of expiry, 0=never + DeleteKey string // Deletion key, one generated if not provided } func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { @@ -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 } } } From 12551d12b3afce7f7977bae5c1099579d2519a60 Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Mon, 5 Oct 2015 23:51:49 -0700 Subject: [PATCH 3/3] housekeeping --- server.go | 8 ++++---- templates.go | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/server.go b/server.go index 37e2e1b..e3f2abf 100644 --- a/server.go +++ b/server.go @@ -21,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) diff --git a/templates.go b/templates.go index 195f0f4..eb2997d 100644 --- a/templates.go +++ b/templates.go @@ -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",