Any suggestion how to improve compressor and resizer's APIs? Regards, Glen
On Thursday, November 9, 2017 at 12:26:14 PM UTC+8, Glen Huang wrote: > > I'm writing a small image processing tool, given an image content, it only > does two things: > > 1. compressing the image content > 2. generating a thumbnail (by resizing and then compressing it) > > I want to model the compressing(the same goes for resizing) after the > compress stdlib: you create a compressor that accepts an io.Writer, and > then write to the compressor. > > The problem comes when I want to join resizing and compressing. They're > Resizer and Compressor respectively, and they conforms to io.WriteCloser. > Compressor is nested inside Resizer. When Resizer is closed, I want > Compressor to be automatically closed. But I don't know how to do it in a > general way. For example > > type Thumbnail struct { > w io.Writer > ContentType string > generator io.WriteCloser > } > > func NewThumbnail(w io.Writer, contentType string) *Thumbnail { > c = &Content{w, contentType, nil} > switch contentType { > case "image/jpeg": > compressor := jpeg.NewCompressor(w) > resizer := jpeg.Resizer(compressor) > c.generator = jpeg.NewCompressor(w) > } > return c > } > > func (c *Thumbnail) Close() error { > return generator.Close() > } > > > > Right now, closing generator won't close compressor. > > I don't want to explicitly store compressor and resizer in Thumbnail and > close them manually, because I want to support different image types, which > might involve different steps to generate the thumbnail. > > In this case, should I make Compressor and Resizer accept io.WriteCloser > instead? I'm not sure because > > 1. why the compress stdlib doesn't accept io.WriteCloser? > 2. I will need to give io.Write to Compressor eventually, in which case I > need to wrap it in a ioutil.NopCloser, which is only for io.Reader. Maybe > there is a reason it only wraps io.Reader? > > Regards, > Glen > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.