swap to using time types instead of ints

This commit is contained in:
George Burgess IV 2015-10-05 23:50:20 -07:00
parent 4330d605e3
commit 1e421e07cd
4 changed files with 29 additions and 51 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

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