2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-30 13:50:52 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-03-29 12:30:23 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-30 13:50:52 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-30 13:50:52 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-12-18 13:35:28 +00:00
|
|
|
"storj.io/storj/uplink/metainfo/kvmetainfo"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/uplink/storage/streams"
|
2018-11-30 13:50:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Upload implements Writer and Closer for writing to stream.
|
|
|
|
type Upload struct {
|
2018-12-03 14:38:03 +00:00
|
|
|
ctx context.Context
|
2019-12-18 13:35:28 +00:00
|
|
|
stream kvmetainfo.MutableStream
|
2018-12-03 14:38:03 +00:00
|
|
|
streams streams.Store
|
|
|
|
writer io.WriteCloser
|
|
|
|
closed bool
|
|
|
|
errgroup errgroup.Group
|
2018-11-30 13:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpload creates new stream upload.
|
2019-12-18 13:35:28 +00:00
|
|
|
func NewUpload(ctx context.Context, stream kvmetainfo.MutableStream, streams streams.Store) *Upload {
|
2018-11-30 13:50:52 +00:00
|
|
|
reader, writer := io.Pipe()
|
|
|
|
|
|
|
|
upload := Upload{
|
2018-12-03 14:38:03 +00:00
|
|
|
ctx: ctx,
|
|
|
|
stream: stream,
|
|
|
|
streams: streams,
|
|
|
|
writer: writer,
|
2018-11-30 13:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
upload.errgroup.Go(func() error {
|
|
|
|
obj := stream.Info()
|
2018-12-07 18:31:29 +00:00
|
|
|
|
|
|
|
serMetaInfo := pb.SerializableMeta{
|
|
|
|
ContentType: obj.ContentType,
|
|
|
|
UserDefined: obj.Metadata,
|
|
|
|
}
|
|
|
|
metadata, err := proto.Marshal(&serMetaInfo)
|
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return errs.Combine(err, reader.CloseWithError(err))
|
2018-12-07 18:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = streams.Put(ctx, storj.JoinPaths(obj.Bucket.Name, obj.Path), obj.Bucket.PathCipher, reader, metadata, obj.Expires)
|
2018-11-30 13:50:52 +00:00
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return errs.Combine(err, reader.CloseWithError(err))
|
2018-11-30 13:50:52 +00:00
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
|
|
|
|
return nil
|
2018-11-30 13:50:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return &upload
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes len(data) bytes from data to the underlying data stream.
|
|
|
|
//
|
|
|
|
// See io.Writer for more details.
|
|
|
|
func (upload *Upload) Write(data []byte) (n int, err error) {
|
|
|
|
if upload.closed {
|
|
|
|
return 0, Error.New("already closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return upload.writer.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the stream and releases the underlying resources.
|
|
|
|
func (upload *Upload) Close() error {
|
|
|
|
if upload.closed {
|
|
|
|
return Error.New("already closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
upload.closed = true
|
|
|
|
|
|
|
|
err := upload.writer.Close()
|
|
|
|
|
|
|
|
// Wait for streams.Put to commit the upload to the PointerDB
|
2019-03-29 12:30:23 +00:00
|
|
|
return errs.Combine(err, upload.errgroup.Wait())
|
2018-11-30 13:50:52 +00:00
|
|
|
}
|