diff --git a/go/storage/needle.go b/go/storage/needle.go index 3bf627141..51079376a 100644 --- a/go/storage/needle.go +++ b/go/storage/needle.go @@ -118,7 +118,7 @@ func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) { n.LastModified = uint64(time.Now().Unix()) } n.SetHasLastModifiedDate() - if n.Ttl != nil { + if n.Ttl != EMPTY_TTL { n.SetHasTtl() } diff --git a/go/storage/volume_ttl.go b/go/storage/volume_ttl.go index 5ff43e24e..459ee55ba 100644 --- a/go/storage/volume_ttl.go +++ b/go/storage/volume_ttl.go @@ -4,10 +4,6 @@ import ( "strconv" ) -var ( - TtlRange []uint16 -) - const ( //stored unit types Empty byte = iota @@ -19,14 +15,6 @@ const ( Year ) -func init() { - TtlRange = []uint16{3, 10, 30, - 60 /*1 hour*/, 60 * 3, 60 * 6, 60 * 12, - 1440 /*1 day*/, 1440 * 3, 1440 * 7, 1440 * 15, 1440 * 31, - 44888 /*1 month*/, 65535, - } -} - type TTL struct { count byte unit byte @@ -120,7 +108,7 @@ func toStoredByte(readableUnitByte byte) byte { return Week case 'M': return Month - case 'Y': + case 'y': return Year } return 0 @@ -141,7 +129,7 @@ func (t TTL) Minutes() uint32 { case Month: return uint32(t.count) * 60 * 24 * 31 case Year: - return uint32(t.count) * 60 * 24 * 31 * 365 + return uint32(t.count) * 60 * 24 * 365 } return 0 } diff --git a/go/storage/volume_ttl_test.go b/go/storage/volume_ttl_test.go new file mode 100644 index 000000000..216469a4c --- /dev/null +++ b/go/storage/volume_ttl_test.go @@ -0,0 +1,60 @@ +package storage + +import ( + "testing" +) + +func TestTTLReadWrite(t *testing.T) { + ttl, _ := ReadTTL("") + if ttl.Minutes() != 0 { + t.Errorf("empty ttl:%v", ttl) + } + + ttl, _ = ReadTTL("9") + if ttl.Minutes() != 9 { + t.Errorf("9 ttl:%v", ttl) + } + + ttl, _ = ReadTTL("8m") + if ttl.Minutes() != 8 { + t.Errorf("8m ttl:%v", ttl) + } + + ttl, _ = ReadTTL("5h") + if ttl.Minutes() != 300 { + t.Errorf("5h ttl:%v", ttl) + } + + ttl, _ = ReadTTL("5d") + if ttl.Minutes() != 5*24*60 { + t.Errorf("5d ttl:%v", ttl) + } + + ttl, _ = ReadTTL("5w") + if ttl.Minutes() != 5*7*24*60 { + t.Errorf("5w ttl:%v", ttl) + } + + ttl, _ = ReadTTL("5M") + if ttl.Minutes() != 5*31*24*60 { + t.Errorf("5M ttl:%v", ttl) + } + + ttl, _ = ReadTTL("5y") + if ttl.Minutes() != 5*365*24*60 { + t.Errorf("5y ttl:%v", ttl) + } + + output := make([]byte, 2) + ttl.ToBytes(output) + ttl2 := LoadTTLFromBytes(output) + if ttl.Minutes() != ttl2.Minutes() { + t.Errorf("ttl:%v ttl2:%v", ttl, ttl2) + } + + ttl3 := LoadTTLFromUint32(ttl.ToUint32()) + if ttl.Minutes() != ttl3.Minutes() { + t.Errorf("ttl:%v ttl3:%v", ttl, ttl3) + } + +}