Provide interface to get stream size (#876)

## Description

Allows kopia data collection consumers to query stream size.

This is required for OneDrive that needs to set the file size prior to uploading file data.

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [x] 🐹 Trivial/Minor

## Issue(s)

* #668 

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Vaibhav Kamra 2022-09-16 11:15:43 -07:00 committed by GitHub
parent 9e66f197c0
commit ac638d3162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 3 deletions

View File

@ -22,7 +22,7 @@ type Collection interface {
FullPath() path.Path FullPath() path.Path
} }
// DataStream represents a single item within a DataCollection // Stream represents a single item within a Collection
// that can be consumed as a stream (it embeds io.Reader) // that can be consumed as a stream (it embeds io.Reader)
type Stream interface { type Stream interface {
// ToReader returns an io.Reader for the DataStream // ToReader returns an io.Reader for the DataStream
@ -31,8 +31,14 @@ type Stream interface {
UUID() string UUID() string
} }
// DataStreamInfo is used to provide service specific // StreamInfo is used to provide service specific
// information about the DataStream // information about the Stream
type StreamInfo interface { type StreamInfo interface {
Info() details.ItemInfo Info() details.ItemInfo
} }
// StreamSize is used to provide size
// information about the Stream
type StreamSize interface {
Size() int64
}

View File

@ -38,6 +38,7 @@ func (kdc kopiaDataCollection) FullPath() path.Path {
type kopiaDataStream struct { type kopiaDataStream struct {
reader io.ReadCloser reader io.ReadCloser
uuid string uuid string
size int64
} }
func (kds kopiaDataStream) ToReader() io.ReadCloser { func (kds kopiaDataStream) ToReader() io.ReadCloser {
@ -47,3 +48,7 @@ func (kds kopiaDataStream) ToReader() io.ReadCloser {
func (kds kopiaDataStream) UUID() string { func (kds kopiaDataStream) UUID() string {
return kds.uuid return kds.uuid
} }
func (kds kopiaDataStream) Size() int64 {
return kds.size
}

View File

@ -66,6 +66,7 @@ func (suite *KopiaDataCollectionUnitSuite) TestReturnsStreams() {
&kopiaDataStream{ &kopiaDataStream{
reader: io.NopCloser(bytes.NewReader(testData[0])), reader: io.NopCloser(bytes.NewReader(testData[0])),
uuid: uuids[0], uuid: uuids[0],
size: int64(len(testData[0])),
}, },
}, },
}, },
@ -75,10 +76,12 @@ func (suite *KopiaDataCollectionUnitSuite) TestReturnsStreams() {
&kopiaDataStream{ &kopiaDataStream{
reader: io.NopCloser(bytes.NewReader(testData[0])), reader: io.NopCloser(bytes.NewReader(testData[0])),
uuid: uuids[0], uuid: uuids[0],
size: int64(len(testData[0])),
}, },
&kopiaDataStream{ &kopiaDataStream{
reader: io.NopCloser(bytes.NewReader(testData[1])), reader: io.NopCloser(bytes.NewReader(testData[1])),
uuid: uuids[1], uuid: uuids[1],
size: int64(len(testData[1])),
}, },
}, },
}, },
@ -100,6 +103,9 @@ func (suite *KopiaDataCollectionUnitSuite) TestReturnsStreams() {
buf, err := ioutil.ReadAll(returnedStream.ToReader()) buf, err := ioutil.ReadAll(returnedStream.ToReader())
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, buf, testData[count]) assert.Equal(t, buf, testData[count])
require.Implements(t, (*data.StreamSize)(nil), returnedStream)
ss := returnedStream.(data.StreamSize)
assert.Equal(t, len(buf), int(ss.Size()))
count++ count++
} }

View File

@ -564,6 +564,7 @@ func restoreSingleItem(
&kopiaDataStream{ &kopiaDataStream{
uuid: f.Name(), uuid: f.Name(),
reader: r, reader: r,
size: f.Size(),
}, },
}, },
path: itemDir, path: itemDir,