Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaka0325 committed Jul 17, 2020
1 parent a265027 commit a235d16
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 119 deletions.
1 change: 1 addition & 0 deletions kadai1/tanaka0325/imgconv/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func (args Args) uniq() []string {
for _, v := range args {
if !m[v] {
m[v] = true

u = append(u, v)
}
}
Expand Down
4 changes: 2 additions & 2 deletions kadai1/tanaka0325/imgconv/cmd/imgconv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var options imgconv.Options
var args imgconv.Args

func init() {
options.From = flag.String("f", "jpg", "file extention before convert")
options.To = flag.String("t", "png", "file extention after convert")
options.From = flag.String("f", "jpg", "file extension before convert")
options.To = flag.String("t", "png", "file extension after convert")
options.DryRun = flag.Bool("n", false, "dry run")
flag.Parse()

Expand Down
81 changes: 81 additions & 0 deletions kadai1/tanaka0325/imgconv/cnv_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package imgconv

import (
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"

"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)

type Decoder interface {
Decode(io.Reader) (image.Image, error)
}

type Encoder interface {
Encode(io.Writer, image.Image) error
}

type DecodeEncoder interface {
Decoder
Encoder
}

type CnvImage struct{}

// CnvImagePng is type for png format.
type CnvImagePNG CnvImage

func (ip CnvImagePNG) Decode(r io.Reader) (image.Image, error) { return png.Decode(r) }

func (ip CnvImagePNG) Encode(w io.Writer, i image.Image) error { return png.Encode(w, i) }

// CnvImageJPEG is type for jpeg format.
type CnvImageJPEG CnvImage

func (ip CnvImageJPEG) Decode(r io.Reader) (image.Image, error) { return jpeg.Decode(r) }

func (ip CnvImageJPEG) Encode(w io.Writer, i image.Image) error { return jpeg.Encode(w, i, nil) }

// CnvImageGIF is type for gif format.
type CnvImageGIF CnvImage

func (ip CnvImageGIF) Decode(r io.Reader) (image.Image, error) { return gif.Decode(r) }

func (ip CnvImageGIF) Encode(w io.Writer, i image.Image) error {
return gif.Encode(w, i, &gif.Options{NumColors: 256})
}

// CnvImageBMP is type for bmp format.
type CnvImageBMP CnvImage

func (ip CnvImageBMP) Decode(r io.Reader) (image.Image, error) { return bmp.Decode(r) }

func (ip CnvImageBMP) Encode(w io.Writer, i image.Image) error { return bmp.Encode(w, i) }

// CnvImageTIFF is type for tiff format.
type CnvImageTIFF CnvImage

func (ip CnvImageTIFF) Decode(r io.Reader) (image.Image, error) { return tiff.Decode(r) }

func (ip CnvImageTIFF) Encode(w io.Writer, i image.Image) error { return tiff.Encode(w, i, nil) }

func newCnvImage(ext string) DecodeEncoder {
switch ext {
case "png":
return &CnvImagePNG{}
case "jpg", "jpeg":
return &CnvImageJPEG{}
case "gif":
return &CnvImageGIF{}
case "bmp":
return &CnvImageBMP{}
case "tiff", "tif":
return &CnvImageTIFF{}
}

return nil
}
82 changes: 0 additions & 82 deletions kadai1/tanaka0325/imgconv/conv_image.go

This file was deleted.

5 changes: 0 additions & 5 deletions kadai1/tanaka0325/imgconv/go.mod.back

This file was deleted.

92 changes: 65 additions & 27 deletions kadai1/tanaka0325/imgconv/imgconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,85 @@ import (
)

var allowedExts = []string{"png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"}
var fromExt string
var toExt string

// Run is to convert image file format
// Run is to convert image file format.
func Run(options Options, args Args) error {
// validator
// // validator
if err := options.validate(allowedExts); err != nil {
return err
}

fromExt = *options.From
toExt = *options.To

// get target image flepaths from args
paths, err := getTargetFilePaths(args, *options.From)
paths, err := getTargetFilePaths(args, fromExt)
if err != nil {
return err
}

// convert
imgs, err := createConvImages(paths, *options.From, *options.To)
if err != nil {
return err
}
for _, img := range imgs {
if err := img.decode(); err != nil {
return err
}
for _, path := range paths {
filename := strings.Replace(path, "."+fromExt, "", -1)
// "))
f := newCnvImage(fromExt)
t := newCnvImage(toExt)

if *options.DryRun {
fmt.Println(img.filename+"."+img.fromExt, "=>", img.filename+"."+img.toExt)
} else {
if err := img.encode(); err != nil {
return err
}
fmt.Printf("%s.%s => %s.%s \n", filename, fromExt, filename, toExt)
} else if err := convert(f, t, filename); err != nil {
return err
}
}

return nil
}

func convert(d Decoder, e Encoder, filename string) (err error) {
// open file
r, err := os.Open(filename + "." + fromExt)
if err != nil {
return
}
defer r.Close()

// decode
img, err := d.Decode(r)
if err != nil {
return
}

// create file
w, err := os.Create(filename + "." + toExt)
if err != nil {
return err
}

defer func() {
err = w.Close()
}()

// encode
if err := e.Encode(w, img); err != nil {
return err
}

return
}

func getTargetFilePaths(args Args, from string) ([]string, error) {
uns := args.uniq()
paths := []string{}

paths := []string{}
for _, n := range uns {
if ok, err := isDir(n); err != nil {
return nil, err
} else if !ok {
return nil, fmt.Errorf("%s is not a directory", n)
}

if err := filepath.Walk(n, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == "."+from {
paths = append(paths, path)
Expand All @@ -63,16 +101,16 @@ func getTargetFilePaths(args Args, from string) ([]string, error) {
return paths, nil
}

func createConvImages(paths []string, from, to string) ([]convImage, error) {
images := []convImage{}
for _, p := range paths {
i := convImage{
filename: strings.Replace(p, "."+from, "", 1),
fromExt: strings.ToLower(from),
toExt: strings.ToLower(to),
}
images = append(images, i)
func isDir(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}

fi, err := f.Stat()
if err != nil {
return false, err
}

return images, nil
return fi.IsDir(), nil
}
7 changes: 4 additions & 3 deletions kadai1/tanaka0325/imgconv/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ type Options struct {
DryRun *bool
}

func (opt Options) validate(allow_list []string) error {
func (opt Options) validate(allowList []string) error {
to := strings.ToLower(*opt.To)
from := strings.ToLower(*opt.From)
targetExts := []string{to, from}

for _, e := range targetExts {
if err := include(allow_list, e); err != nil {
return fmt.Errorf("%w. ext is only allowd in %s", err, allow_list)
if err := include(allowList, e); err != nil {
return fmt.Errorf("%w. ext is only allowd in %s", err, allowList)
}
}

Expand Down

0 comments on commit a235d16

Please sign in to comment.