mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
713 B
Go
46 lines
713 B
Go
10 months ago
|
package httpgetter
|
||
2 years ago
|
|
||
|
import (
|
||
1 year ago
|
"errors"
|
||
2 years ago
|
"io"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type Image struct {
|
||
|
Blob []byte
|
||
|
Mediatype string
|
||
|
}
|
||
|
|
||
|
func GetImage(urlStr string) (*Image, error) {
|
||
|
if _, err := url.Parse(urlStr); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
response, err := http.Get(urlStr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
mediatype, err := getMediatype(response)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if !strings.HasPrefix(mediatype, "image/") {
|
||
1 year ago
|
return nil, errors.New("Wrong image mediatype")
|
||
2 years ago
|
}
|
||
|
|
||
|
bodyBytes, err := io.ReadAll(response.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
image := &Image{
|
||
|
Blob: bodyBytes,
|
||
|
Mediatype: mediatype,
|
||
|
}
|
||
|
return image, nil
|
||
|
}
|