processOrder processes the order request.
(ctx context.Context, order *Order)
| 161 | |
| 162 | // processOrder processes the order request. |
| 163 | func (p *processor) processOrder(ctx context.Context, order *Order) error { |
| 164 | // Read the input image from the bucket. |
| 165 | r, err := p.bucket.NewReader(ctx, order.InImage, nil) |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | |
| 170 | defer r.Close() |
| 171 | img, format, err := image.Decode(r) |
| 172 | if err != nil { |
| 173 | return err |
| 174 | } |
| 175 | |
| 176 | // Process and write the output image. |
| 177 | order.OutImage = fmt.Sprintf("%s-out.png", strings.TrimSuffix(order.InImage, "-in")) |
| 178 | w, err := p.bucket.NewWriter(ctx, order.OutImage, nil) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | if err := png.Encode(w, img); err != nil { |
| 183 | w.Close() |
| 184 | return err |
| 185 | } |
| 186 | if err := w.Close(); err != nil { |
| 187 | return err |
| 188 | } |
| 189 | |
| 190 | // Pretend that the conversion takes some time. |
| 191 | time.Sleep(time.Duration(rand.Intn(5)+2) * time.Second) |
| 192 | |
| 193 | order.Note = fmt.Sprintf("converted from %s to png", format) |
| 194 | return nil |
| 195 | } |