Home Download Docs Code Community
     1	/*
     2	Copyright 2011 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 index
    18	
    19	import (
    20		"bytes"
    21		"errors"
    22	
    23		"perkeep.org/internal/magic"
    24		"perkeep.org/pkg/blob"
    25		"perkeep.org/pkg/schema"
    26	)
    27	
    28	type BlobSniffer struct {
    29		br blob.Ref
    30	
    31		contents  []byte
    32		written   int64
    33		meta      *schema.Blob // or nil
    34		mimeType  string
    35		camliType schema.CamliType
    36	}
    37	
    38	func NewBlobSniffer(ref blob.Ref) *BlobSniffer {
    39		if !ref.Valid() {
    40			panic("invalid ref")
    41		}
    42		return &BlobSniffer{br: ref}
    43	}
    44	
    45	func (sn *BlobSniffer) SchemaBlob() (meta *schema.Blob, ok bool) {
    46		return sn.meta, sn.meta != nil
    47	}
    48	
    49	func (sn *BlobSniffer) Write(d []byte) (int, error) {
    50		if !sn.br.Valid() {
    51			panic("write on sniffer with invalid blobref")
    52		}
    53		sn.written += int64(len(d))
    54		if len(sn.contents) < schema.MaxSchemaBlobSize {
    55			n := schema.MaxSchemaBlobSize - len(sn.contents)
    56			if len(d) < n {
    57				n = len(d)
    58			}
    59			sn.contents = append(sn.contents, d[:n]...)
    60		}
    61		return len(d), nil
    62	}
    63	
    64	// Size returns the number of bytes written to the BlobSniffer.
    65	// It might be more than schema.MaxSchemaBlobSize.
    66	// See IsTruncated.
    67	func (sn *BlobSniffer) Size() int64 {
    68		return sn.written
    69	}
    70	
    71	// IsTruncated reports whether the BlobSniffer had more than
    72	// schema.MaxSchemaBlobSize bytes written to it.
    73	func (sn *BlobSniffer) IsTruncated() bool {
    74		return sn.written > schema.MaxSchemaBlobSize
    75	}
    76	
    77	// Body returns the bytes written to the BlobSniffer.
    78	func (sn *BlobSniffer) Body() ([]byte, error) {
    79		if sn.IsTruncated() {
    80			return nil, errors.New("index.Body: was truncated")
    81		}
    82		return sn.contents, nil
    83	}
    84	
    85	// MIMEType returns the sniffed blob's content-type or the empty string if unknown.
    86	// If the blob is a Perkeep schema metadata blob, the MIME type will be of
    87	// the form "application/json; camliType=foo".
    88	func (sn *BlobSniffer) MIMEType() string { return sn.mimeType }
    89	
    90	func (sn *BlobSniffer) CamliType() schema.CamliType { return sn.camliType }
    91	
    92	func (sn *BlobSniffer) Parse() {
    93		if sn.bufferIsCamliJSON() {
    94			sn.camliType = sn.meta.Type()
    95			sn.mimeType = "application/json; camliType=" + string(sn.camliType)
    96		} else {
    97			sn.mimeType = magic.MIMEType(sn.contents)
    98		}
    99	}
   100	
   101	func (sn *BlobSniffer) bufferIsCamliJSON() bool {
   102		buf := sn.contents
   103		if !schema.LikelySchemaBlob(buf) {
   104			return false
   105		}
   106		blob, err := schema.BlobFromReader(sn.br, bytes.NewReader(buf))
   107		if err != nil {
   108			return false
   109		}
   110		sn.meta = blob
   111		return true
   112	}
Website layout inspired by memcached.
Content by the authors.