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" "os"
"path" "path"
"strings" "strings"
"time"
"bitbucket.org/taruti/mimemagic" "bitbucket.org/taruti/mimemagic"
"github.com/dustin/go-humanize"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
"github.com/zenazn/goji/web" "github.com/zenazn/goji/web"
) )
@ -21,6 +23,13 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
return 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) file, _ := os.Open(filePath)
header := make([]byte, 512) header := make([]byte, 512)
file.Read(header) file.Read(header)
@ -45,7 +54,8 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err = tpl.ExecuteWriter(pongo2.Context{ err = tpl.ExecuteWriter(pongo2.Context{
"mime": mimetype, "mime": mimetype,
"filename": fileName, "filename": fileName,
"size": fileInfo.Size(),
"size": sizeHuman,
"expiry": expiryHuman,
}, w) }, w)
if err != nil { if err != nil {

8
expiry.go

@ -5,8 +5,8 @@ import (
) )
// Get what the unix timestamp will be in "seconds". // 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 { if seconds == 0 {
ts = 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 // 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 { if ts == 0 {
expired = false expired = false

4
meta.go

@ -47,7 +47,7 @@ func metadataRead(filename string) ([]string, error) {
return lines, scanner.Err() return lines, scanner.Err()
} }
func metadataGetExpiry(filename string) (int32, error) {
func metadataGetExpiry(filename string) (int64, error) {
metadata, err := metadataRead(filename) metadata, err := metadataRead(filename)
if len(metadata) < 1 { if len(metadata) < 1 {
@ -68,7 +68,7 @@ func metadataGetExpiry(filename string) (int32, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} else { } else {
return int32(expiry), err
return int64(expiry), err
} }
} }

4
templates/display/base.html

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

10
upload.go

@ -19,7 +19,7 @@ import (
type UploadRequest struct { type UploadRequest struct {
src io.Reader src io.Reader
filename string filename string
expiry int32 // Seconds until expiry, 0 = never
expiry int64 // Seconds until expiry, 0 = never
randomBarename bool randomBarename bool
deletionKey string // Empty string if not defined deletionKey string // Empty string if not defined
} }
@ -28,7 +28,7 @@ type UploadRequest struct {
type Upload struct { type Upload struct {
Filename string // Final filename on disk Filename string // Final filename on disk
Size int64 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 DeleteKey string // Deletion key, one generated if not provided
} }
@ -211,15 +211,15 @@ func barePlusExt(filename string) (barename, extension string) {
return return
} }
func parseExpiry(expStr string) int32 {
func parseExpiry(expStr string) int64 {
if expStr == "" { if expStr == "" {
return 0 return 0
} else { } else {
expiry, err := strconv.ParseInt(expStr, 10, 32)
expiry, err := strconv.ParseInt(expStr, 10, 64)
if err != nil { if err != nil {
return 0 return 0
} else { } else {
return int32(expiry)
return int64(expiry)
} }
} }
} }
Loading…
Cancel
Save