Open a file and return the io Reader, the size, the last modification, and the path as URL. If the sizeOnly parameter is true no io Reader will be returned.
(fn string, sizeOnly bool)
| 87 | // Open a file and return the io Reader, the size, the last modification, and the path as URL. |
| 88 | // If the sizeOnly parameter is true no io Reader will be returned. |
| 89 | func (s *List) openFile(fn string, sizeOnly bool) (io.ReadCloser, int64, time.Time, *url.URL, error) { |
| 90 | if fn == "-" { |
| 91 | return stdin, 0, time.Unix(0, 0), nil, nil |
| 92 | } |
| 93 | |
| 94 | u, err := url.Parse(fn) |
| 95 | if err != nil { |
| 96 | // NOTE: raw paths are parsed with u.Scheme="" |
| 97 | s.setFatalErr(err) |
| 98 | return nil, 0, time.Unix(0, 0), nil, err |
| 99 | } |
| 100 | |
| 101 | switch u.Scheme { |
| 102 | case "", "file": |
| 103 | path := filepath.Join(u.Host, u.Path) // On Windows Host contains the drive letter |
| 104 | fi, err := os.Stat(path) |
| 105 | if err != nil { |
| 106 | s.setFatalErr(err) |
| 107 | return nil, 0, time.Unix(0, 0), u, err |
| 108 | } |
| 109 | if sizeOnly { |
| 110 | return nil, fi.Size(), fi.ModTime(), u, nil |
| 111 | } |
| 112 | f, err := os.Open(path) |
| 113 | if err != nil { |
| 114 | s.setFatalErr(err) |
| 115 | return nil, fi.Size(), fi.ModTime(), u, err |
| 116 | } |
| 117 | return f, fi.Size(), fi.ModTime(), u, err |
| 118 | case "s3": |
| 119 | if sizeOnly { |
| 120 | resp, err := s.svc.HeadObject(&s3.HeadObjectInput{ |
| 121 | Bucket: aws.String(u.Host), |
| 122 | Key: aws.String(u.Path), |
| 123 | }) |
| 124 | if err != nil { |
| 125 | err := fmt.Errorf("error opening %q: %v", fn, err) |
| 126 | s.setFatalErr(err) |
| 127 | return nil, 0, time.Unix(0, 0), u, err |
| 128 | } |
| 129 | return nil, *resp.ContentLength, *resp.LastModified, u, nil |
| 130 | } else { |
| 131 | resp, err := s.svc.GetObject(&s3.GetObjectInput{ |
| 132 | Bucket: aws.String(u.Host), |
| 133 | Key: aws.String(u.Path), |
| 134 | }) |
| 135 | if err != nil { |
| 136 | err := fmt.Errorf("error opening %q: %v", fn, err) |
| 137 | s.setFatalErr(err) |
| 138 | return nil, 0, time.Unix(0, 0), u, err |
| 139 | } |
| 140 | return resp.Body, *resp.ContentLength, *resp.LastModified, u, nil |
| 141 | } |
| 142 | case "http", "https": |
| 143 | resp, err := httpGet(fn) |
| 144 | if err != nil { |
| 145 | s.setFatalErr(err) |
| 146 | return nil, 0, time.Unix(0, 0), u, err |