forked from graymeta/stow
/
item.go
89 lines (74 loc) · 2.1 KB
/
item.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
package google
import (
"context"
"io"
"net/url"
"time"
"cloud.google.com/go/storage"
)
type Item struct {
container *Container // Container information is required by a few methods.
client *storage.Client // A client is needed to make requests.
name string
hash string
etag string
size int64
url *url.URL
lastModified time.Time
metadata map[string]interface{}
object *storage.ObjectAttrs
ctx context.Context
}
// ID returns a string value that represents the name of a file.
func (i *Item) ID() string {
return i.name
}
// Name returns a string value that represents the name of the file.
func (i *Item) Name() string {
return i.name
}
// Size returns the size of an item in bytes.
func (i *Item) Size() (int64, error) {
return i.size, nil
}
// URL returns a url which follows the predefined format
func (i *Item) URL() *url.URL {
return i.url
}
// Open returns an io.ReadCloser to the object. Useful for downloading/streaming the object.
func (i *Item) Open() (io.ReadCloser, error) {
obj := i.container.Bucket().Object(i.name)
return obj.NewReader(i.ctx)
}
// OpenRange returns an io.Reader to the object for a specific byte range
func (i *Item) OpenRange(start, end uint64) (io.ReadCloser, error) {
obj := i.container.Bucket().Object(i.name)
return obj.NewRangeReader(i.ctx, int64(start), int64(end - start) + 1)
}
// LastMod returns the last modified date of the item.
func (i *Item) LastMod() (time.Time, error) {
return i.lastModified, nil
}
// Metadata returns a nil map and no error.
func (i *Item) Metadata() (map[string]interface{}, error) {
return i.metadata, nil
}
// ETag returns the ETag value
func (i *Item) ETag() (string, error) {
return i.etag, nil
}
// Object returns the Google Storage Object
func (i *Item) StorageObject() *storage.ObjectAttrs {
return i.object
}
// prepUrl takes a MediaLink string and returns a url
func prepUrl(str string) (*url.URL, error) {
u, err := url.Parse(str)
if err != nil {
return nil, err
}
u.Scheme = "google"
// Discard the query string
u.RawQuery = ""
return u, nil
}