"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/update.go" (28 May 2021, 2021 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package drive
    2 
    3 import (
    4     "fmt"
    5     "google.golang.org/api/drive/v3"
    6     "google.golang.org/api/googleapi"
    7     "io"
    8     "mime"
    9     "path/filepath"
   10     "time"
   11 )
   12 
   13 type UpdateArgs struct {
   14     Out         io.Writer
   15     Progress    io.Writer
   16     Id          string
   17     Path        string
   18     Name        string
   19     Description string
   20     Parents     []string
   21     Mime        string
   22     Recursive   bool
   23     ChunkSize   int64
   24     Timeout     time.Duration
   25 }
   26 
   27 func (self *Drive) Update(args UpdateArgs) error {
   28     srcFile, srcFileInfo, err := openFile(args.Path)
   29     if err != nil {
   30         return fmt.Errorf("Failed to open file: %s", err)
   31     }
   32 
   33     defer srcFile.Close()
   34 
   35     // Instantiate empty drive file
   36     dstFile := &drive.File{Description: args.Description}
   37 
   38     // Use provided file name or use filename
   39     if args.Name == "" {
   40         dstFile.Name = filepath.Base(srcFileInfo.Name())
   41     } else {
   42         dstFile.Name = args.Name
   43     }
   44 
   45     // Set provided mime type or get type based on file extension
   46     if args.Mime == "" {
   47         dstFile.MimeType = mime.TypeByExtension(filepath.Ext(dstFile.Name))
   48     } else {
   49         dstFile.MimeType = args.Mime
   50     }
   51 
   52     // Set parent folders
   53     dstFile.Parents = args.Parents
   54 
   55     // Chunk size option
   56     chunkSize := googleapi.ChunkSize(int(args.ChunkSize))
   57 
   58     // Wrap file in progress reader
   59     progressReader := getProgressReader(srcFile, args.Progress, srcFileInfo.Size())
   60 
   61     // Wrap reader in timeout reader
   62     reader, ctx := getTimeoutReaderContext(progressReader, args.Timeout)
   63 
   64     fmt.Fprintf(args.Out, "Uploading %s\n", args.Path)
   65     started := time.Now()
   66 
   67     f, err := self.service.Files.Update(args.Id, dstFile).Fields("id", "name", "size").Context(ctx).Media(reader, chunkSize).Do()
   68     if err != nil {
   69         if isTimeoutError(err) {
   70             return fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
   71         }
   72         return fmt.Errorf("Failed to upload file: %s", err)
   73     }
   74 
   75     // Calculate average upload rate
   76     rate := calcRate(f.Size, started, time.Now())
   77 
   78     fmt.Fprintf(args.Out, "Updated %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(f.Size, false))
   79     return nil
   80 }