-
Notifications
You must be signed in to change notification settings - Fork 7
kadai1: make image converter #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package conv | ||
|
||
import ( | ||
"image" | ||
"image/gif" // import gif | ||
"image/jpeg" // import jpeg | ||
"image/png" // import png | ||
"os" | ||
"path/filepath" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// Img image struct | ||
type Img struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 略さない There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pathと画像自体の情報を持ったものが |
||
Path string | ||
Data image.Image | ||
} | ||
|
||
// NewImg generate Img struct | ||
func NewImg(path string) (*Img, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
file, err := os.Open(path) | ||
defer file.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
img, _, err := image.Decode(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Img{ | ||
Path: path, | ||
Data: img, | ||
}, nil | ||
} | ||
|
||
// Save save img for input format if format satisfies ".jpeg", ".jpg", ".gif", ".png" | ||
func (i *Img) Save(path string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. せっかくフィールドに |
||
ext := filepath.Ext(path) | ||
var err error | ||
err = func(ext string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. クロージャ内で参照しないのであれば、 |
||
for _, postfix := range []string{".jpeg", ".jpg", ".gif", ".png"} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO: postfixは間違いではありませんが、suffixのほうがより伝わりやすいかもしれません。ご参考 (同名のメールサーバがありますので、ちょっと気になっちゃいました) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかにおっしゃる通り...、ご指摘ありがとうございます! |
||
if ext == postfix { | ||
return nil | ||
} | ||
} | ||
return xerrors.New("invalid extension") | ||
}(ext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// if file exist, do nothing | ||
if _, err := os.Stat(path); !os.IsNotExist(err) { | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. それ以外のエラーの場合は? |
||
} | ||
|
||
dst, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer dst.Close() | ||
|
||
switch ext { | ||
case ".jpeg", ".jpg": | ||
err = jpeg.Encode(dst, i.Data, nil) | ||
case ".gif": | ||
err = gif.Encode(dst, i.Data, nil) | ||
case ".png": | ||
err = png.Encode(dst, i.Data) | ||
default: | ||
err = xerrors.New("error in main method") | ||
} | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. エラーがないのであれば、明示的に |
||
} | ||
|
||
// AddExt add extension | ||
func (i *Img) AddExt(ext string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return i.Path + ext | ||
} | ||
|
||
// Replace replace image | ||
func (i *Img) Replace(ext string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 名前から何をReplaceするか想像できない。しかも拡張子をしている理由も判断できない。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. メソッド名から処理(画像変換)を想像できないかもです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これもたしかにおっしゃる通りですね...! |
||
|
||
// check path | ||
prevPath := i.Path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 最後に代入するなら |
||
if filepath.Ext(prevPath) == ext { | ||
return nil | ||
} | ||
|
||
// save file | ||
newPath := prevPath[:len(prevPath)-len(filepath.Ext(prevPath))] + ext | ||
|
||
// if file exist, do nothing | ||
if _, err := os.Stat(newPath); !os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
if err := i.Save(newPath); err != nil { | ||
return err | ||
} | ||
|
||
// remove old file | ||
if err := os.Remove(prevPath); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 変換だけにしておいた方が良さそう |
||
return err | ||
} | ||
i.Path = newPath | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package conv | ||
|
||
import ( | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func Test_Img(t *testing.T) { | ||
t.Run("OK: .png -> .jpeg", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. テストの回でやりますが、テーブル駆動テストにすると見通しがよくなります。 |
||
|
||
// ### Given ### | ||
img, err := NewImg("./lena.jpeg") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. テストに使うものは |
||
if err != nil { | ||
t.Fatalf("Cannot load file. err: %v", err) | ||
} | ||
|
||
// ### When ### | ||
path := "./lena-png.jpeg" | ||
if err = img.Save(path); err != nil { | ||
t.Fatalf("Cannot save file. err: %v", err) | ||
} | ||
|
||
// ### Then ### | ||
file, err := os.Open(path) | ||
if err != nil { | ||
t.Fatalf("Cannot open saved file. err: %v", err) | ||
} | ||
if _, err = jpeg.Decode(file); err != nil { | ||
t.Fatalf("Cannot decode saved file. err: %v", err) | ||
} | ||
if err := os.Remove(path); err != nil { | ||
t.Fatalf("Cannot remove saved file. err: %v", err) | ||
} | ||
|
||
}) | ||
|
||
t.Run("OK: .jpeg -> .png", func(t *testing.T) { | ||
|
||
// ### Given ### | ||
img, err := NewImg("./lena.png") | ||
if err != nil { | ||
t.Fatalf("Cannot load file. err: %v", err) | ||
} | ||
|
||
// ### When ### | ||
path := "./lena-jpeg.png" | ||
if err = img.Save(path); err != nil { | ||
t.Fatalf("Cannot save file. err: %v", err) | ||
} | ||
|
||
// ### Then ### | ||
file, err := os.Open(path) | ||
if err != nil { | ||
t.Fatalf("Cannot open saved file. err: %v", err) | ||
} | ||
if _, err = png.Decode(file); err != nil { | ||
t.Fatalf("Cannot decode saved file. err: %v", err) | ||
} | ||
if err := os.Remove(path); err != nil { | ||
t.Fatalf("Cannot remove saved file. err: %v", err) | ||
} | ||
|
||
}) | ||
|
||
t.Run("NG: cannot openfile", func(t *testing.T) { | ||
|
||
// ### Given ### | ||
_, err := NewImg("./noexist.png") | ||
if err == nil { | ||
t.Fatal("Load no exist file.") | ||
} | ||
}) | ||
|
||
t.Run("NG: invalid extension", func(t *testing.T) { | ||
|
||
// ### Given ### | ||
_, err := NewImg("./img.go") | ||
if err == nil { | ||
t.Fatal("Load invalid file.") | ||
} | ||
}) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dir | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
) | ||
|
||
// Lookup lookup directory to find image files | ||
func Lookup(dir string, ext string, pathList []string) ([]string, error) { | ||
|
||
fileInfos, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, fileInfo := range fileInfos { | ||
|
||
// get file info | ||
path := filepath.Join(dir, fileInfo.Name()) | ||
|
||
// check if the path is about directory | ||
if fileInfo.IsDir() { | ||
pathList, err = Lookup(path, ext, pathList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// check if the postfix is equal to the input format | ||
if filepath.Ext(path) == ext { | ||
pathList = append(pathList, path) | ||
} | ||
} | ||
|
||
return pathList, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package dir | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_Lookup(t *testing.T) { | ||
|
||
t.Run("OK: .jpg", func(t *testing.T) { | ||
var actual []string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要? |
||
// ### Given ### | ||
actual, err := Lookup("./test", ".jpg", actual) | ||
if err != nil { | ||
t.Fatalf("Failed test. err: %v", err) | ||
} | ||
|
||
expected := []string{"test/test1.jpg", "test/test3/test4.jpg", "test/test3/test5.jpg"} | ||
|
||
// ### Then ### | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("Failed test. expected: %v,\n but actual: %v", expected, actual) | ||
} | ||
}) | ||
|
||
t.Run("OK: .png", func(t *testing.T) { | ||
var actual []string | ||
// ### Given ### | ||
actual, err := Lookup("./test", ".png", actual) | ||
if err != nil { | ||
t.Fatalf("Failed test. err: %v", err) | ||
} | ||
|
||
expected := []string{"test/test2.png", "test/test3/test6.png"} | ||
|
||
// ### Then ### | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("Failed test. expected: %v,\n but actual: %v", expected, actual) | ||
} | ||
}) | ||
|
||
t.Run("OK: .gif(empty)", func(t *testing.T) { | ||
var actual []string | ||
// ### Given ### | ||
actual, err := Lookup("./test", ".gif", actual) | ||
if err != nil { | ||
t.Fatalf("Failed test. err: %v", err) | ||
} | ||
|
||
var expected []string | ||
|
||
// ### Then ### | ||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("Failed test. expected: %v,\n but actual: %v", expected, actual) | ||
} | ||
}) | ||
|
||
t.Run("NG: not found directory", func(t *testing.T) { | ||
var actual []string | ||
// ### Given ### | ||
_, err := Lookup("./test2", ".jpg", actual) | ||
if err == nil { | ||
t.Fatalf("should be error.") | ||
} | ||
|
||
expected := "open ./test2: no such file or directory" | ||
|
||
// ### Then ### | ||
if err.Error() != expected { | ||
t.Fatalf("Failed test. expected: %s,\n but actual: %s", expected, err.Error()) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントがコードから分かる以上の情報がないんですがコメントを残した理由はなんでしょうか?