github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/cmd/camput/uploader.go (about)

     1  /*
     2  Copyright 2012 Google Inc.
     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 main
    18  
    19  import (
    20  	"net/http"
    21  	"strings"
    22  
    23  	"camlistore.org/pkg/blobserver"
    24  	"camlistore.org/pkg/client"
    25  	"camlistore.org/pkg/httputil"
    26  	"camlistore.org/pkg/syncutil"
    27  )
    28  
    29  type Uploader struct {
    30  	*client.Client
    31  
    32  	// fdGate guards gates the creation of file descriptors.
    33  	fdGate *syncutil.Gate
    34  
    35  	fileOpts *fileOptions // per-file options; may be nil
    36  
    37  	// for debugging; normally nil, but overrides Client if set
    38  	// TODO(bradfitz): clean this up? embed a StatReceiver instead
    39  	// of a Client?
    40  	altStatReceiver blobserver.StatReceiver
    41  
    42  	transport *httputil.StatsTransport // for HTTP statistics
    43  	pwd       string
    44  	statCache UploadCache
    45  	haveCache HaveCache
    46  
    47  	fs http.FileSystem // virtual filesystem to read from; nil means OS filesystem.
    48  }
    49  
    50  // possible options when uploading a file
    51  type fileOptions struct {
    52  	permanode bool // create a content-based permanode for each uploaded file
    53  	// tag is an optional tag or comma-delimited tags to apply to
    54  	// the above permanode.
    55  	tag string
    56  	// perform for the client the actions needing gpg signing when uploading a file.
    57  	vivify   bool
    58  	exifTime bool // use the time in exif metadata as the modtime if possible.
    59  	capCtime bool // use mtime as ctime if ctime > mtime
    60  }
    61  
    62  func (o *fileOptions) tags() []string {
    63  	if o == nil || o.tag == "" {
    64  		return nil
    65  	}
    66  	return strings.Split(o.tag, ",")
    67  }
    68  
    69  func (o *fileOptions) wantFilePermanode() bool {
    70  	return o != nil && o.permanode
    71  }
    72  
    73  func (o *fileOptions) wantVivify() bool {
    74  	return o != nil && o.vivify
    75  }
    76  
    77  func (o *fileOptions) wantCapCtime() bool {
    78  	return o != nil && o.capCtime
    79  }
    80  
    81  func (up *Uploader) uploadString(s string) (*client.PutResult, error) {
    82  	return up.Upload(client.NewUploadHandleFromString(s))
    83  }
    84  
    85  func (up *Uploader) Close() error {
    86  	var grp syncutil.Group
    87  	if up.haveCache != nil {
    88  		grp.Go(up.haveCache.Close)
    89  	}
    90  	grp.Go(up.Client.Close)
    91  	return grp.Err()
    92  }