Checks if the given filepath is a valid and existing file path. If ignoreExtension is true, then the dir + basepath is checked for existence ignoring the extension. In the return the first return value is the actual path that exists (with the extension), second argument is the file info and finally
(fpath string, ignoreExtension bool)
| 18 | // ignoring the extension. |
| 19 | // In the return the first return value is the actual path that exists (with the extension), second argument is the file info and finally the error |
| 20 | func IsFileReadable(fpath string, ignoreExtension bool) (string, os.FileInfo, error) { |
| 21 | info, err := os.Stat(fpath) |
| 22 | if err != nil { |
| 23 | if os.IsNotExist(err) { |
| 24 | if ignoreExtension { |
| 25 | logger.Infof(context.TODO(), "looking for any extensions") |
| 26 | matches, err := filepath.Glob(fpath + ".*") |
| 27 | if err == nil && len(matches) == 1 { |
| 28 | logger.Infof(context.TODO(), "Extension match found [%s]", matches[0]) |
| 29 | info, err = os.Stat(matches[0]) |
| 30 | if err == nil { |
| 31 | return matches[0], info, nil |
| 32 | } |
| 33 | } else { |
| 34 | logger.Errorf(context.TODO(), "Extension match not found [%v,%v]", err, matches) |
| 35 | } |
| 36 | } |
| 37 | return "", nil, errors.Wrapf(err, "file not found at path [%s]", fpath) |
| 38 | } |
| 39 | if os.IsPermission(err) { |
| 40 | return "", nil, errors.Wrapf(err, "unable to read file [%s], Flyte does not have permissions", fpath) |
| 41 | } |
| 42 | return "", nil, errors.Wrapf(err, "failed to read file") |
| 43 | } |
| 44 | return fpath, info, nil |
| 45 | } |
| 46 | |
| 47 | // Uploads a file to the data store. |
| 48 | func UploadFileToStorage(ctx context.Context, filePath string, toPath storage.DataReference, size int64, store *storage.DataStore) error { |