| 62 | } |
| 63 | |
| 64 | func DownloadFileToTemp(fileUrl string, pattern string) (string, error) { |
| 65 | tempDir, err := os.MkdirTemp("", pattern) |
| 66 | if err != nil { |
| 67 | return "", err |
| 68 | } |
| 69 | |
| 70 | fileName := filepath.Base(fileUrl) |
| 71 | tempFilePath := filepath.Join(tempDir, fileName) |
| 72 | |
| 73 | outFile, err := os.Create(tempFilePath) |
| 74 | if err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | defer outFile.Close() |
| 78 | |
| 79 | resp, err := http.Get(fileUrl) //nolint:gosec // G107: URL comes from trusted configuration |
| 80 | if err != nil { |
| 81 | return "", err |
| 82 | } |
| 83 | defer resp.Body.Close() |
| 84 | |
| 85 | if resp.StatusCode != http.StatusOK { |
| 86 | return "", fmt.Errorf("failed to download file (%s): status code %s", fileUrl, resp.Status) |
| 87 | } |
| 88 | |
| 89 | _, err = io.Copy(outFile, resp.Body) |
| 90 | if err != nil { |
| 91 | return "", err |
| 92 | } |
| 93 | |
| 94 | return tempFilePath, nil |
| 95 | } |
| 96 | |
| 97 | func GenerateTempFilePath(pattern string) (string, error) { |
| 98 | file, err := os.CreateTemp("", pattern) |