godash/pkg/media/media.go

30 lines
669 B
Go
Raw Normal View History

2024-09-30 20:16:29 +02:00
package media
import (
"fmt"
"io"
"io/fs"
"net/http"
"os"
)
2025-01-31 12:08:03 +01:00
func DownloadSelfHostedIcon(url, title, filePath string) (string, error) {
resp, err := http.Get(url)
2024-09-30 20:16:29 +02:00
if err != nil {
2025-01-31 12:08:03 +01:00
return "", fmt.Errorf("failed to get icon: %w", err)
2024-09-30 20:16:29 +02:00
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
2025-01-31 12:08:03 +01:00
return "", fmt.Errorf("failed to get icon, status: %d, url: %s", resp.StatusCode, url)
2024-09-30 20:16:29 +02:00
}
data, err := io.ReadAll(resp.Body)
if err != nil {
2025-01-31 12:08:03 +01:00
return "", fmt.Errorf("failed to read icon: %w", err)
2024-09-30 20:16:29 +02:00
}
err = os.WriteFile(filePath, data, fs.FileMode(0640))
if err != nil {
2025-01-31 12:08:03 +01:00
return "", fmt.Errorf("failed to write icon: %w", err)
2024-09-30 20:16:29 +02:00
}
2025-01-31 12:08:03 +01:00
return filePath, nil
2024-09-30 20:16:29 +02:00
}