Browse Source

Display expiry and size info

pull/16/head
andreimarcu 9 years ago
parent
commit
02f86da3c7
  1. 12
      display.go
  2. 8
      expiry.go
  3. 4
      meta.go
  4. 4
      templates/display/base.html
  5. 10
      upload.go

12
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 {

8
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

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

4
templates/display/base.html

@ -13,6 +13,10 @@
{% block infoleft %}{% endblock %}
<div class="right">
{% if expiry %}
<span>file expires in {{ expiry }}</span> |
{% endif %}
<span>{{ size }}</span> |
<a href="/selif/{{ filename }}" download>get</a>
</div>
<div class="clear"></div>

10
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)
}
}
}
Loading…
Cancel
Save