jokes-bapak2/api/app/core/validator/image.go

31 lines
798 B
Go
Raw Normal View History

package validator
2021-07-23 14:24:58 +00:00
import (
"errors"
2021-09-27 10:10:19 +00:00
"jokes-bapak2-api/app/utils"
2021-07-23 14:24:58 +00:00
"net/http"
"strings"
"github.com/gojek/heimdall/v7/httpclient"
)
var ValidContentType = []string{"image/jpeg", "image/png", "image/webp", "image/gif"}
// CheckImageValidity calls to the image host to check whether it's a valid image or not.
func CheckImageValidity(client *httpclient.Client, link string) (bool, error) {
if strings.Contains(link, "https://") {
// Perform HTTP call to link
res, err := client.Get(link, http.Header{"User-Agent": []string{"JokesBapak2 API"}})
if err != nil {
return false, err
}
2021-07-23 15:03:04 +00:00
if res.StatusCode == 200 && utils.IsIn(ValidContentType, res.Header.Get("content-type")) {
2021-07-23 14:24:58 +00:00
return true, nil
}
2021-08-04 05:56:14 +00:00
2021-07-23 14:24:58 +00:00
return false, nil
}
return false, errors.New("URL must use HTTPS protocol")
2021-08-04 05:56:14 +00:00
}