Skip to content

Commit 392e17e

Browse files
committed
chore: code cleanup in media downloader
1 parent 8e34302 commit 392e17e

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/wp2hugo/internal/hugogenerator/media_downloader.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,34 @@ import (
1212

1313
func writeFavicon(outputDirPath string, websiteURL string) error {
1414
log.Debug().Msg("Fetching and writing favicon")
15-
if err := createDirIfNotExist(path.Join(outputDirPath, "static")); err != nil {
16-
return err
17-
}
1815
filePath := path.Join(outputDirPath, "static", "favicon.ico")
1916
if !strings.HasPrefix(websiteURL, "http") {
2017
websiteURL = "https://" + websiteURL
2118
}
2219
url1 := fmt.Sprintf("%s/favicon.ico", websiteURL)
23-
resp, err := http.Get(url1)
20+
return downloadFromURL(url1, filePath)
21+
}
22+
23+
func downloadFromURL(srcURL string, destFilePath string) error {
24+
log.Debug().
25+
Str("srcURL", srcURL).
26+
Str("destFilePath", destFilePath).
27+
Msg("Downloading from URL")
28+
if err := createDirIfNotExist(path.Dir(destFilePath)); err != nil {
29+
return err
30+
}
31+
32+
resp, err := http.Get(srcURL)
2433
if err != nil {
25-
return fmt.Errorf("error fetching favicon: %s", err)
34+
return fmt.Errorf("error fetching %s: %s", srcURL, err)
2635
}
2736
defer resp.Body.Close()
2837
if resp.StatusCode != http.StatusOK {
29-
return fmt.Errorf("error fetching favicon: %s", resp.Status)
38+
return fmt.Errorf("error fetching %s: %s", srcURL, resp.Status)
3039
}
31-
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
40+
file, err := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
3241
if err != nil {
33-
return fmt.Errorf("error opening favicon file: %s", err)
42+
return fmt.Errorf("error opening file %s: %s", file, err)
3443
}
3544
defer file.Close()
3645
return resp.Write(file)

0 commit comments

Comments
 (0)