-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
49 lines (40 loc) · 1.16 KB
/
content.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package resources
// TODO(ericsnow) Move this file to the charm repo?
import (
"io"
"os"
charmresource "github.com/juju/charm/v10/resource"
"github.com/juju/errors"
"github.com/juju/utils/v3"
)
// Content holds a reader for the content of a resource along
// with details about that content.
type Content struct {
// Data holds the resource content, ready to be read (once).
Data io.Reader
// Size is the byte count of the data.
Size int64
// Fingerprint holds the checksum of the data.
Fingerprint charmresource.Fingerprint
}
// GenerateContent returns a new Content for the given data stream.
func GenerateContent(reader io.ReadSeeker) (Content, error) {
var sizer utils.SizeTracker
sizingReader := io.TeeReader(reader, &sizer)
fp, err := charmresource.GenerateFingerprint(sizingReader)
if err != nil {
return Content{}, errors.Trace(err)
}
if _, err := reader.Seek(0, os.SEEK_SET); err != nil {
return Content{}, errors.Trace(err)
}
size := sizer.Size()
content := Content{
Data: reader,
Size: size,
Fingerprint: fp,
}
return content, nil
}