github.com/coreos/mantle@v0.13.0/storage/object.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package storage
    16  
    17  import (
    18  	"encoding/base64"
    19  	"hash/crc32"
    20  	"io"
    21  	"sort"
    22  
    23  	"google.golang.org/api/storage/v1"
    24  
    25  	"github.com/coreos/mantle/lang/natsort"
    26  	"github.com/coreos/mantle/lang/reader"
    27  )
    28  
    29  // SortObjects orders Objects by Name using natural sorting.
    30  func SortObjects(objs []*storage.Object) {
    31  	sort.Slice(objs, func(i, j int) bool {
    32  		return natsort.Less(objs[i].Name, objs[j].Name)
    33  	})
    34  }
    35  
    36  // Update CRC32c and Size in the given Object
    37  func crcSum(obj *storage.Object, media io.ReaderAt) error {
    38  	c := crc32.New(crc32.MakeTable(crc32.Castagnoli))
    39  	n, err := io.Copy(c, reader.AtReader(media))
    40  	if err != nil {
    41  		return err
    42  	}
    43  	obj.Size = uint64(n)
    44  	obj.Crc32c = base64.StdEncoding.EncodeToString(c.Sum(nil))
    45  	return nil
    46  }
    47  
    48  // Judges whether two Objects are equal based on size and CRC. To guard against
    49  // uninitialized fields, nil objects and empty CRC values are never equal.
    50  func crcEq(a, b *storage.Object) bool {
    51  	if a == nil || b == nil {
    52  		return false
    53  	}
    54  	if a.Crc32c == "" || b.Crc32c == "" {
    55  		return false
    56  	}
    57  	return a.Size == b.Size && a.Crc32c == b.Crc32c
    58  }
    59  
    60  // Duplicate basic Object metadata, useful for preparing a copy operation.
    61  func dupObj(src *storage.Object) *storage.Object {
    62  	dst := &storage.Object{
    63  		Bucket:             src.Bucket,
    64  		CacheControl:       src.CacheControl,
    65  		ContentDisposition: src.ContentDisposition,
    66  		ContentEncoding:    src.ContentEncoding,
    67  		ContentLanguage:    src.ContentLanguage,
    68  		ContentType:        src.ContentType,
    69  		Crc32c:             src.Crc32c,
    70  		Md5Hash:            src.Md5Hash,
    71  		Name:               src.Name,
    72  		Size:               src.Size,
    73  	}
    74  	if len(src.Metadata) > 0 {
    75  		dst.Metadata = make(map[string]string)
    76  		for k, v := range src.Metadata {
    77  			dst.Metadata[k] = v
    78  		}
    79  	}
    80  	return dst
    81  }