From 02f86da3c7e7fcab32b35eb72bd425cde3e76f24 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Tue, 29 Sep 2015 19:00:16 -0400 Subject: [PATCH] Display expiry and size info --- display.go | 12 +++++++++++- expiry.go | 8 ++++---- meta.go | 4 ++-- templates/display/base.html | 4 ++++ upload.go | 10 +++++----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/display.go b/display.go index 7092d2c..d3538e2 100644 --- a/display.go +++ b/display.go @@ -5,8 +5,10 @@ import ( "os" "path" "strings" + "time" "bitbucket.org/taruti/mimemagic" + "github.com/dustin/go-humanize" "github.com/flosch/pongo2" "github.com/zenazn/goji/web" ) @@ -21,6 +23,13 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { return } + expiry, _ := metadataGetExpiry(fileName) + var expiryHuman string + if expiry != 0 { + expiryHuman = humanize.RelTime(time.Now(), time.Unix(expiry, 0), "", "") + } + sizeHuman := humanize.Bytes(uint64(fileInfo.Size())) + file, _ := os.Open(filePath) header := make([]byte, 512) file.Read(header) @@ -45,7 +54,8 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { err = tpl.ExecuteWriter(pongo2.Context{ "mime": mimetype, "filename": fileName, - "size": fileInfo.Size(), + "size": sizeHuman, + "expiry": expiryHuman, }, w) if err != nil { diff --git a/expiry.go b/expiry.go index 81cf4ac..0444842 100644 --- a/expiry.go +++ b/expiry.go @@ -5,8 +5,8 @@ import ( ) // Get what the unix timestamp will be in "seconds". -func getFutureTimestamp(seconds int32) (ts int32) { - now := int32(time.Now().Unix()) +func getFutureTimestamp(seconds int64) (ts int64) { + now := int64(time.Now().Unix()) if seconds == 0 { ts = 0 @@ -18,8 +18,8 @@ func getFutureTimestamp(seconds int32) (ts int32) { } // Determine if a file with expiry set to "ts" has expired yet -func isTsExpired(ts int32) (expired bool) { - now := int32(time.Now().Unix()) +func isTsExpired(ts int64) (expired bool) { + now := int64(time.Now().Unix()) if ts == 0 { expired = false diff --git a/meta.go b/meta.go index e0ed2d8..f2165e0 100644 --- a/meta.go +++ b/meta.go @@ -47,7 +47,7 @@ func metadataRead(filename string) ([]string, error) { return lines, scanner.Err() } -func metadataGetExpiry(filename string) (int32, error) { +func metadataGetExpiry(filename string) (int64, error) { metadata, err := metadataRead(filename) if len(metadata) < 1 { @@ -68,7 +68,7 @@ func metadataGetExpiry(filename string) (int32, error) { if err != nil { return 0, err } else { - return int32(expiry), err + return int64(expiry), err } } diff --git a/templates/display/base.html b/templates/display/base.html index f250f82..94f49eb 100644 --- a/templates/display/base.html +++ b/templates/display/base.html @@ -13,6 +13,10 @@ {% block infoleft %}{% endblock %}
+ {% if expiry %} + file expires in {{ expiry }} | + {% endif %} + {{ size }} | get
diff --git a/upload.go b/upload.go index 8210a17..c11bad9 100644 --- a/upload.go +++ b/upload.go @@ -19,7 +19,7 @@ import ( type UploadRequest struct { src io.Reader filename string - expiry int32 // Seconds until expiry, 0 = never + expiry int64 // Seconds until expiry, 0 = never randomBarename bool deletionKey string // Empty string if not defined } @@ -28,7 +28,7 @@ type UploadRequest struct { type Upload struct { Filename string // Final filename on disk Size int64 - Expiry int32 // Unix timestamp of expiry, 0=never + Expiry int64 // Unix timestamp of expiry, 0=never DeleteKey string // Deletion key, one generated if not provided } @@ -211,15 +211,15 @@ func barePlusExt(filename string) (barename, extension string) { return } -func parseExpiry(expStr string) int32 { +func parseExpiry(expStr string) int64 { if expStr == "" { return 0 } else { - expiry, err := strconv.ParseInt(expStr, 10, 32) + expiry, err := strconv.ParseInt(expStr, 10, 64) if err != nil { return 0 } else { - return int32(expiry) + return int64(expiry) } } }