| 203 | } |
| 204 | |
| 205 | func fetchFileStream(urlStr string) (f string, r io.ReadCloser, err error) { |
| 206 | u, _ := url.Parse(urlStr) |
| 207 | if (u.Scheme == "") || (u.Host == "") || (u.Path == "") { |
| 208 | return "", nil, errors.New("Not a valid url: " + urlStr) |
| 209 | } |
| 210 | pathParts := strings.Split(strings.TrimRight(u.Path, "/"), "/") |
| 211 | fileName := pathParts[len(pathParts)-1] |
| 212 | cleanPath := strings.Join(pathParts, "/") |
| 213 | |
| 214 | if (u.Scheme == "http") || (u.Scheme == "https") { |
| 215 | res, err := httpclient.Get(u.String(), httpclient.Header{}, nil, nil) |
| 216 | if err != nil { |
| 217 | return "", nil, errors.New("httpclient returned: " + err.Error()) |
| 218 | } |
| 219 | if res.StatusCode != 200 { //err in fetching data |
| 220 | resbody, _ := ioutil.ReadAll(res.Body) |
| 221 | return "", nil, errors.New(fmt.Sprintf("url=%s, res=%s", u.String(), resbody)) |
| 222 | } |
| 223 | return fileName, res.Body, err |
| 224 | } else if u.Scheme == "ftp" { |
| 225 | // set port if missing |
| 226 | ftpHost := u.Host |
| 227 | hostParts := strings.Split(u.Host, ":") |
| 228 | if len(hostParts) == 1 { |
| 229 | ftpHost = u.Host + ":21" |
| 230 | } |
| 231 | c, _, _, err := tinyftp.Dial("tcp", ftpHost) |
| 232 | if err != nil { |
| 233 | return "", nil, errors.New("ftpclient returned: " + err.Error()) |
| 234 | } |
| 235 | defer c.Close() |
| 236 | if _, _, err = c.Login("", ""); err != nil { |
| 237 | return "", nil, errors.New("ftpclient returned: " + err.Error()) |
| 238 | } |
| 239 | dconn, _, _, err := c.RetrieveFrom(cleanPath) |
| 240 | if err != nil { |
| 241 | return "", nil, errors.New("ftpclient returned: " + err.Error()) |
| 242 | } |
| 243 | return fileName, dconn, err |
| 244 | } |
| 245 | return "", nil, errors.New("unsupported protocol scheme: " + u.Scheme) |
| 246 | } |
| 247 | |
| 248 | func writeChecksum(f func() hash.Hash, c chan checkSumCom) { |
| 249 | go func() { |