MCPcopy
hub / github.com/OpenNHP/opennhp / DownloadFileToTemp

Function DownloadFileToTemp

nhp/utils/utils.go:64–95  ·  view source on GitHub ↗
(fileUrl string, pattern string)

Source from the content-addressed store, hash-verified

62}
63
64func 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
97func GenerateTempFilePath(pattern string) (string, error) {
98 file, err := os.CreateTemp("", pattern)

Callers

nothing calls this directly

Calls 2

CloseMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected