Home Download Docs Code Community
     1	/*
     2	Copyright 2013 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	package blobserver
    18	
    19	import (
    20		"context"
    21		"errors"
    22		"fmt"
    23		"hash"
    24		"io"
    25		"strings"
    26	
    27		"perkeep.org/pkg/blob"
    28	)
    29	
    30	// ErrReadonly is the error value returned by read-only blobservers.
    31	var ErrReadonly = errors.New("this blobserver is read only")
    32	
    33	// ReceiveString uploads the blob given by the string s to dst
    34	// and returns its blobref and size.
    35	func ReceiveString(ctx context.Context, dst BlobReceiver, s string) (blob.SizedRef, error) {
    36		return Receive(ctx, dst, blob.RefFromString(s), strings.NewReader(s))
    37	}
    38	
    39	// Receive wraps calling a BlobReceiver's ReceiveBlob method,
    40	// additionally providing verification of the src digest, and also
    41	// notifying the blob hub on success.
    42	// The error will be ErrCorruptBlob if the blobref didn't match.
    43	func Receive(ctx context.Context, dst BlobReceiver, br blob.Ref, src io.Reader) (blob.SizedRef, error) {
    44		return receive(ctx, dst, br, src, true)
    45	}
    46	
    47	func ReceiveNoHash(ctx context.Context, dst BlobReceiver, br blob.Ref, src io.Reader) (blob.SizedRef, error) {
    48		return receive(ctx, dst, br, src, false)
    49	}
    50	
    51	func receive(ctx context.Context, dst BlobReceiver, br blob.Ref, src io.Reader, checkHash bool) (sb blob.SizedRef, err error) {
    52		src = io.LimitReader(src, MaxBlobSize)
    53		if checkHash {
    54			h := br.Hash()
    55			if h == nil {
    56				return sb, fmt.Errorf("invalid blob type %v; no registered hash function", br)
    57			}
    58			src = &checkHashReader{h, br, src, false}
    59		}
    60		sb, err = dst.ReceiveBlob(ctx, br, src)
    61		if err != nil {
    62			if checkHash && src.(*checkHashReader).corrupt {
    63				err = ErrCorruptBlob
    64			}
    65			return
    66		}
    67		err = GetHub(dst).NotifyBlobReceived(sb)
    68		return
    69	}
    70	
    71	// checkHashReader is an io.Reader that wraps the src Reader but turns
    72	// an io.EOF into an ErrCorruptBlob if the data read doesn't match the
    73	// hash of br.
    74	type checkHashReader struct {
    75		h       hash.Hash
    76		br      blob.Ref
    77		src     io.Reader
    78		corrupt bool
    79	}
    80	
    81	func (c *checkHashReader) Read(p []byte) (n int, err error) {
    82		n, err = c.src.Read(p)
    83		c.h.Write(p[:n])
    84		if err == io.EOF && !c.br.HashMatches(c.h) {
    85			err = ErrCorruptBlob
    86			c.corrupt = true
    87		}
    88		return
    89	}
Website layout inspired by memcached.
Content by the authors.