-
Notifications
You must be signed in to change notification settings - Fork 402
/
object.go
96 lines (82 loc) · 2.51 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package mobile
import (
"fmt"
"time"
libuplink "storj.io/storj/lib/uplink"
"storj.io/storj/pkg/storj"
)
// ObjectInfo object metadata
type ObjectInfo struct {
Version int32
Bucket string
Path string
IsPrefix bool
Size int64
ContentType string
Created int64
Modified int64
Expires int64
metadata map[string]string
}
func newObjectInfoFromObject(object storj.Object) *ObjectInfo {
return &ObjectInfo{
Version: int32(object.Version),
Bucket: object.Bucket.Name,
Path: object.Path,
IsPrefix: object.IsPrefix,
Size: object.Size,
ContentType: object.ContentType,
Created: object.Created.UTC().UnixNano() / int64(time.Millisecond),
Modified: object.Modified.UTC().UnixNano() / int64(time.Millisecond),
Expires: object.Expires.UTC().UnixNano() / int64(time.Millisecond),
metadata: object.Metadata,
}
}
func newObjectInfoFromObjectMeta(objectMeta libuplink.ObjectMeta) *ObjectInfo {
return &ObjectInfo{
// TODO ObjectMeta doesn't have Version but storj.Object has
// Version: int32(objectMeta.Version),
Bucket: objectMeta.Bucket,
Path: objectMeta.Path,
IsPrefix: objectMeta.IsPrefix,
Size: objectMeta.Size,
ContentType: objectMeta.ContentType,
Created: objectMeta.Created.UTC().UnixNano() / int64(time.Millisecond),
Modified: objectMeta.Modified.UTC().UnixNano() / int64(time.Millisecond),
Expires: objectMeta.Expires.UTC().UnixNano() / int64(time.Millisecond),
metadata: objectMeta.Metadata,
}
}
// GetMetadata gets objects custom metadata
func (bl *ObjectInfo) GetMetadata(key string) string {
return bl.metadata[key]
}
// ObjectList represents list of objects
type ObjectList struct {
list storj.ObjectList
}
// More returns true if list request was not able to return all results
func (bl *ObjectList) More() bool {
return bl.list.More
}
// Prefix prefix for objects from list
func (bl *ObjectList) Prefix() string {
return bl.list.Prefix
}
// Bucket returns bucket name
func (bl *ObjectList) Bucket() string {
return bl.list.Bucket
}
// Length returns number of returned items
func (bl *ObjectList) Length() int {
return len(bl.list.Items)
}
// Item gets item from specific index
func (bl *ObjectList) Item(index int) (*ObjectInfo, error) {
if index < 0 && index >= len(bl.list.Items) {
return nil, fmt.Errorf("index out of range")
}
return newObjectInfoFromObject(bl.list.Items[index]), nil
}