Skip to content

Commit

Permalink
feat: add extension to file if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
michalczmiel committed Jan 17, 2024
1 parent 6d57645 commit e3df8fe
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion internal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -74,6 +75,18 @@ func validateContentType(contentType string, imageTypes []string) error {
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 {
Expand All @@ -87,7 +100,9 @@ func DownloadImageFromUrl(url, filePath string, parameters Parameters) error {
return err
}

file, err := os.Create(filePath)
correctFilePath := addExtensionIfMissing(filePath, contentType)

file, err := os.Create(correctFilePath)
if err != nil {
return err
}
Expand Down

0 comments on commit e3df8fe

Please sign in to comment.