|
| 1 | +package Tinify |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + ResizeMethodScale = "scale" |
| 12 | + ResizeMethodFit = "fit" |
| 13 | + ResizeMethodCover = "cover" |
| 14 | +) |
| 15 | + |
| 16 | +type ResizeMethod string |
| 17 | + |
| 18 | +type ResizeOption struct { |
| 19 | + Method ResizeMethod `json:"method"` |
| 20 | + Width int64 `json:"width"` |
| 21 | + Height int64 `json:"height"` |
| 22 | +} |
| 23 | + |
| 24 | +type Source struct { |
| 25 | + url string |
| 26 | + commands map[string]interface{} |
| 27 | +} |
| 28 | + |
| 29 | +func newSource(url string, commands map[string]interface{}) *Source { |
| 30 | + s := new(Source) |
| 31 | + s.url = url |
| 32 | + if commands != nil { |
| 33 | + s.commands = commands |
| 34 | + } else { |
| 35 | + s.commands = make(map[string]interface{}) |
| 36 | + } |
| 37 | + |
| 38 | + return s |
| 39 | +} |
| 40 | + |
| 41 | +func FromFile(path string) (s *Source, err error) { |
| 42 | + buf, err := ioutil.ReadFile(path) |
| 43 | + if err != nil { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + return FromBuffer(buf) |
| 48 | +} |
| 49 | + |
| 50 | +func FromBuffer(buf []byte) (s *Source, err error) { |
| 51 | + response, err := GetClient().Request(http.MethodPost, "/shrink", buf) |
| 52 | + if err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + s, err = getSourceFromResponse(response) |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +func FromUrl(url string) (s *Source, err error) { |
| 61 | + if len(url) == 0 { |
| 62 | + err = errors.New("url is required") |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + body := map[string]interface{}{ |
| 67 | + "source": map[string]interface{}{ |
| 68 | + "url": url, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + response, err := GetClient().Request(http.MethodPost, "/shrink", body) |
| 73 | + if err != nil { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + s, err = getSourceFromResponse(response) |
| 78 | + return |
| 79 | +} |
| 80 | + |
| 81 | +func getSourceFromResponse(response *http.Response) (s *Source, err error) { |
| 82 | + location := response.Header["Location"] |
| 83 | + url := "" |
| 84 | + if len(location) > 0 { |
| 85 | + url = location[0] |
| 86 | + } |
| 87 | + |
| 88 | + s = newSource(url, nil) |
| 89 | + return |
| 90 | +} |
| 91 | + |
| 92 | +func (s *Source) ToFile(path string) error { |
| 93 | + result, err := s.toResult() |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + return result.ToFile(path) |
| 99 | +} |
| 100 | + |
| 101 | +func (s *Source) Resize(option *ResizeOption) error { |
| 102 | + if option == nil { |
| 103 | + return errors.New("option is required") |
| 104 | + } |
| 105 | + |
| 106 | + s.commands["resize"] = option |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func (s *Source) toResult() (r *Result, err error) { |
| 112 | + if len(s.url) == 0 { |
| 113 | + err = errors.New("url is empty") |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + //body := make([]byte, 0) |
| 118 | + //if len(s.commands) > 0 { |
| 119 | + // body, err = json.Marshal(s.commands) |
| 120 | + // if err != nil { |
| 121 | + // return |
| 122 | + // } |
| 123 | + //} |
| 124 | + response, err := GetClient().Request(http.MethodGet, s.url, s.commands) |
| 125 | + if err != nil { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + data, err := io.ReadAll(response.Body) |
| 130 | + if err != nil { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + r = NewResult(response.Header, data) |
| 135 | + return |
| 136 | +} |
0 commit comments