LoadFileOrURL loads a file from a local path or a URL
(fileRef string)
| 30 | |
| 31 | // LoadFileOrURL loads a file from a local path or a URL |
| 32 | func LoadFileOrURL(fileRef string) ([]byte, error) { |
| 33 | parts := strings.SplitAfterN(fileRef, "://", 2) |
| 34 | if len(parts) == 2 { |
| 35 | scheme := parts[0] |
| 36 | switch scheme { |
| 37 | case "http://": |
| 38 | fallthrough |
| 39 | case "https://": |
| 40 | // #nosec G107 |
| 41 | resp, err := http.Get(fileRef) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | defer resp.Body.Close() |
| 46 | return io.ReadAll(resp.Body) |
| 47 | default: |
| 48 | return nil, errors.New("invalid file scheme") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return os.ReadFile(filepath.Clean(fileRef)) |
| 53 | } |
| 54 | |
| 55 | // ValidateAndExtractName validates and extracts a name from either |
| 56 | // an explicit name parameter OR from metadata.name in the file content. |
no test coverage detected