diff --git a/internal/files.go b/internal/files.go index b4604aa..7a6dbc5 100644 --- a/internal/files.go +++ b/internal/files.go @@ -3,6 +3,8 @@ package internal import ( "fmt" "os" + "path/filepath" + "strings" ) const DefaultPath = "." @@ -23,3 +25,31 @@ func CreateDirectoryIfDoesNotExists(directory string) error { return nil } + +func validateContentType(contentType string, imageTypes []string) error { + if !strings.HasPrefix(contentType, "image") { + return fmt.Errorf("content type '%s' is not an image", contentType) + } + + imageType := strings.Split(contentType, "/")[1] + + for _, allowedImageType := range imageTypes { + if imageType == allowedImageType { + return nil + } + } + + return fmt.Errorf("image type '%s' is not allowed", imageType) +} + +func addExtensionIfMissing(filePath, contentType string) string { + extension := filepath.Ext(filePath) + + if extension != "" { + return filePath + } + + extension = "." + strings.Split(contentType, "/")[1] + + return filePath + extension +} diff --git a/internal/http.go b/internal/http.go index 53cef61..38b999e 100644 --- a/internal/http.go +++ b/internal/http.go @@ -6,8 +6,6 @@ import ( "math/rand" "net/http" "os" - "path/filepath" - "strings" "time" "golang.org/x/net/html" @@ -59,34 +57,6 @@ func request(url, userAgent string) (*http.Response, error) { return response, nil } -func validateContentType(contentType string, imageTypes []string) error { - if !strings.HasPrefix(contentType, "image") { - return fmt.Errorf("content type '%s' is not an image", contentType) - } - - imageType := strings.Split(contentType, "/")[1] - - for _, allowedImageType := range imageTypes { - if imageType == allowedImageType { - return nil - } - } - - return fmt.Errorf("image type '%s' is not allowed", imageType) -} - -func addExtensionIfMissing(filePath, contentType string) string { - extension := filepath.Ext(filePath) - - if extension != "" { - return filePath - } - - extension = "." + strings.Split(contentType, "/")[1] - - return filePath + extension -} - func DownloadImageFromUrl(url, filePath string, parameters Parameters) error { response, err := request(url, parameters.UserAgent) if err != nil {