github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/data/blobstore.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package data 18 19 import ( 20 "context" 21 "crypto/sha256" 22 "io" 23 24 "github.com/kaleido-io/firefly/internal/i18n" 25 "github.com/kaleido-io/firefly/internal/log" 26 "github.com/kaleido-io/firefly/pkg/database" 27 "github.com/kaleido-io/firefly/pkg/dataexchange" 28 "github.com/kaleido-io/firefly/pkg/fftypes" 29 ) 30 31 type blobStore struct { 32 database database.Plugin 33 exchange dataexchange.Plugin 34 } 35 36 func (bs *blobStore) UploadBLOB(ctx context.Context, ns string, reader io.Reader) (*fftypes.Data, error) { 37 38 data := &fftypes.Data{ 39 ID: fftypes.NewUUID(), 40 Namespace: ns, 41 Validator: "", 42 Blobstore: true, 43 Datatype: nil, 44 Created: fftypes.Now(), 45 Value: nil, 46 } 47 48 hash := sha256.New() 49 dxReader, dx := io.Pipe() 50 storeAndHash := io.MultiWriter(hash, dx) 51 52 var written int64 53 copyDone := make(chan error, 1) 54 go func() { 55 var err error 56 written, err = io.Copy(storeAndHash, reader) 57 log.L(ctx).Debugf("Upload BLOB streamed %d bytes (err=%v)", written, err) 58 _ = dx.Close() 59 copyDone <- err 60 }() 61 62 dxErr := bs.exchange.UploadBLOB(ctx, ns, *data.ID, dxReader) 63 dxReader.Close() 64 copyErr := <-copyDone 65 if dxErr != nil { 66 return nil, dxErr 67 } 68 if copyErr != nil { 69 return nil, i18n.WrapError(ctx, copyErr, i18n.MsgBlobStreamingFailed) 70 } 71 data.Hash = fftypes.HashResult(hash) 72 log.L(ctx).Infof("Uploaded BLOB %.2fkb hash=%s", float64(written)/1024, data.Hash) 73 74 err := bs.database.UpsertData(ctx, data, false, false) 75 if err != nil { 76 return nil, err 77 } 78 79 return data, nil 80 }