ex_pic_to_video/func.go

79 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"net/http"
"net/url"
"strings"
)
func isImage(contentType string) bool {
imageTypes := []string{
"image/jpeg",
"image/png",
}
for _, imageType := range imageTypes {
if strings.Contains(contentType, imageType) {
return true
}
}
return false
}
func isURL(s string) bool {
// 检查字符串是否为空
if len(s) == 0 {
return false
}
// 使用url.Parse解析URL
u, err := url.Parse(s)
if err != nil {
return false
}
// 检查URL的Scheme是否为空即是否有协议如http, https等
if u.Scheme == "" {
return false
}
// 检查URL的Host是否为空即是否有主机名
if u.Host == "" {
return false
}
// 如果需要可以进一步验证Scheme是否为允许的协议如http, https
// 例如:
// allowedSchemes := map[string]bool{"http": true, "https": true}
// if !allowedSchemes[u.Scheme] {
// return false
// }
return true
}
// checkURL checks if the URL is accessible and if the content is an image.
func checkURL(url string) (bool, error) {
resp, err := http.Get(url)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("failed to fetch URL, status code: %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
return false, nil
}
return isImage(contentType), nil
}
func isLocalPath(s string) bool {
return strings.HasPrefix(s, "/") || (len(s) >= 2 && s[1] == ':')
}