rssmon3/copart/auction/image.go

40 lines
601 B
Go

package auction
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"os"
)
type Image struct {
Raw []byte
Src *url.URL
}
func NewImage(url *url.URL) *Image {
return &Image{
Src: url,
}
}
func (img *Image) MarshalJSON() ([]byte, error) {
return json.Marshal(img.Src.String())
}
func (img *Image) Fetch() error {
resp, err := http.Get(img.Src.String())
if err != nil {
return err
}
defer resp.Body.Close()
Raw, err := ioutil.ReadAll(resp.Body)
img.Raw = Raw
return err
}
func (img *Image) Save(path string) error {
return ioutil.WriteFile(path, img.Raw, os.ModePerm)
}