"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/import.go" (28 May 2021, 1164 Bytes) of package /linux/misc/old/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     "io"
    6     "io/ioutil"
    7     "mime"
    8     "path/filepath"
    9     "strings"
   10 )
   11 
   12 type ImportArgs struct {
   13     Out      io.Writer
   14     Mime     string
   15     Progress io.Writer
   16     Path     string
   17     Parents  []string
   18 }
   19 
   20 func (self *Drive) Import(args ImportArgs) error {
   21     fromMime := args.Mime
   22     if fromMime == "" {
   23         fromMime = getMimeType(args.Path)
   24     }
   25     if fromMime == "" {
   26         return fmt.Errorf("Could not determine mime type of file, use --mime")
   27     }
   28 
   29     about, err := self.service.About.Get().Fields("importFormats").Do()
   30     if err != nil {
   31         return fmt.Errorf("Failed to get about: %s", err)
   32     }
   33 
   34     toMimes, ok := about.ImportFormats[fromMime]
   35     if !ok || len(toMimes) == 0 {
   36         return fmt.Errorf("Mime type '%s' is not supported for import", fromMime)
   37     }
   38 
   39     f, _, err := self.uploadFile(UploadArgs{
   40         Out:      ioutil.Discard,
   41         Progress: args.Progress,
   42         Path:     args.Path,
   43         Parents:  args.Parents,
   44         Mime:     toMimes[0],
   45     })
   46     if err != nil {
   47         return err
   48     }
   49 
   50     fmt.Fprintf(args.Out, "Imported %s with mime type: '%s'\n", f.Id, toMimes[0])
   51     return nil
   52 }
   53 
   54 func getMimeType(path string) string {
   55     t := mime.TypeByExtension(filepath.Ext(path))
   56     return strings.Split(t, ";")[0]
   57 }