diff --git a/kadai1/uetsu/imgConv/convert/convert.go b/kadai1/uetsu/imgConv/convert/convert.go new file mode 100644 index 0000000..d693a76 --- /dev/null +++ b/kadai1/uetsu/imgConv/convert/convert.go @@ -0,0 +1,50 @@ +package convert + +import ( + "image" + "image/jpeg" + "image/png" + "os" + "path/filepath" + "strings" +) + +// InputExt 拡張子の変換前と変換後を格納する。 +type InputExt struct { + Src string + Dst string +} + +// ImgConv ファイルをjpgからpngに変換する。 +// TODO: 他の拡張子に対応 +func ImgConv(filePath string, inputExt InputExt) (convertedFilePath string, err error) { + file, err := os.Open(filePath) + if err != nil { + return + } + defer file.Close() + + var decodeImage image.Image + switch inputExt.Src { + case "jpg", "jpeg": + decodeImage, err = jpeg.Decode(file) + } + if err != nil { + return + } + + convertedFilePath = strings.Join([]string{filePath[:len(filePath)-len(filepath.Ext(filePath))], inputExt.Dst}, ".") + + dstfile, err := os.Create(convertedFilePath) + if err != nil { + return + } + defer dstfile.Close() + + err = png.Encode(dstfile, decodeImage) + if err != nil { + return + } + + return +} diff --git a/kadai1/uetsu/imgConv/doc.go b/kadai1/uetsu/imgConv/doc.go new file mode 100644 index 0000000..89ce20b --- /dev/null +++ b/kadai1/uetsu/imgConv/doc.go @@ -0,0 +1,19 @@ +// Copyright 2019 masashi.uetsu. All rights reserved. + +/* + 指定したディレクトリ配下のjpegファイルを再帰的に探索してpngファイルに変換します。ディレクトリは並べて複数指定可能です。 + + Usage + + imgconv dirPath dirPath + + func searchfile.RecursionFile + + RecursionFile(dirPath string) (filePathList []string) + + func convert.ImgConv + + ImgConv(filePath string) (convertedFilePath string, err error) + +*/ +package main diff --git a/kadai1/uetsu/imgConv/main.go b/kadai1/uetsu/imgConv/main.go new file mode 100644 index 0000000..7f4c3b0 --- /dev/null +++ b/kadai1/uetsu/imgConv/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "fmt" + + "dojo6/kadai1/uetsu/ImgConv/convert" + "dojo6/kadai1/uetsu/imgConv/searchfile" +) + +func main() { + flag.Parse() + args := flag.Args() + + inputExt := convert.InputExt{ + "jpg", + "png", + } + + for _, dirPath := range args { + filePathList := searchfile.RecursionFile(dirPath) + + for _, filePath := range filePathList { + convertedFilePath, err := convert.ImgConv(filePath, inputExt) + if err != nil { + fmt.Printf("%s convert fail\n", filePath) + continue + } + fmt.Printf("%s convert succeed => %s\n", filePath, convertedFilePath) + } + + } +} diff --git a/kadai1/uetsu/imgConv/searchfile/searchfile.go b/kadai1/uetsu/imgConv/searchfile/searchfile.go new file mode 100644 index 0000000..f7ea913 --- /dev/null +++ b/kadai1/uetsu/imgConv/searchfile/searchfile.go @@ -0,0 +1,28 @@ +package searchfile + +import ( + "io/ioutil" + "path/filepath" +) + +// RecursionFile 再帰的にファイル名の一覧を取得する +func RecursionFile(dirPath string) (filePathList []string) { + + files, err := ioutil.ReadDir(dirPath) + if err != nil { + panic(err) + } + + for _, file := range files { + if file.IsDir() { + filePathList = append(filePathList, RecursionFile(filepath.Join(dirPath, file.Name()))...) + continue + } + if filepath.Ext(file.Name()) != ".jpg" { + continue + } + filePathList = append(filePathList, filepath.Join(dirPath, file.Name())) + } + + return +} diff --git a/kadai1/uetsu/imgConv/searchfile/searchfile_test.go b/kadai1/uetsu/imgConv/searchfile/searchfile_test.go new file mode 100644 index 0000000..82cd8ce --- /dev/null +++ b/kadai1/uetsu/imgConv/searchfile/searchfile_test.go @@ -0,0 +1,44 @@ +package searchfile_test + +import ( + "dojo6/kadai1/uetsu/ImgConv/searchfile" + "sort" + "testing" +) + +var TestCase = []struct { + in string + out []string +}{ + {"sample", []string{"sample/hoge.jpg"}}, + {"sample2", []string{"sample2/hoge.jpg", "sample2/hogo/hoge2.jpg"}}, +} + +func Test_RecursionFile(t *testing.T) { + for _, tt := range TestCase { + expect := tt.out + actual := searchfile.RecursionFile(tt.in) + sort.Strings(expect) + sort.Strings(actual) + if len(actual) != len(expect) { + t.Errorf(`expect="%s" actual="%s"`, expect, actual) + break + } + for i := range expect { + if actual[i] != expect[i] { + t.Errorf(`expect="%s" actual="%s"`, expect, actual) + break + } + } + } +} +func Test_FileNotExists(t *testing.T) { + t.Helper() + defer func() { + err := recover() + if err == nil { + t.Errorf("Unexpected File Exists") + } + }() + searchfile.RecursionFile("test") +}