Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions kadai1/sh-tatsuno/gophoto/conv/img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package conv

import (
"image"
"image/gif" // import gif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントがコードから分かる以上の情報がないんですがコメントを残した理由はなんでしょうか?

"image/jpeg" // import jpeg
"image/png" // import png
"os"
"path/filepath"

"golang.org/x/xerrors"
)

// Img image struct
type Img struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

略さない

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pathと画像自体の情報を持ったものがImgで良いのか?

Path string
Data image.Image
}

// NewImg generate Img struct
func NewImg(path string) (*Img, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Img構造体を作ること以上のことをやっている。

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

せっかくフィールドにPathを持っているのになぜ引数で渡すのか?

ext := filepath.Ext(path)
var err error
err = func(ext string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

クロージャ内で参照しないのであれば、err := ...でよい。

for _, postfix := range []string{".jpeg", ".jpg", ".gif", ".png"} {
Copy link

@ebiiim ebiiim Jun 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO: postfixは間違いではありませんが、suffixのほうがより伝わりやすいかもしれません。ご参考

(同名のメールサーバがありますので、ちょっと気になっちゃいました)

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーがないのであれば、明示的にnilで返す。
この場合、ここにくるときにnilじゃないパターンはない。
なお、L.54では新しいerr変数が作られている。

}

// AddExt add extension
func (i *Img) AddExt(ext string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Withでは?

return i.Path + ext
}

// Replace replace image
func (i *Img) Replace(ext string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名前から何をReplaceするか想像できない。しかも拡張子をしている理由も判断できない。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メソッド名から処理(画像変換)を想像できないかもです。Convert(ext string, removeOldFile bool)などいかがでしょうか?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これもたしかにおっしゃる通りですね...!


// check path
prevPath := i.Path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最後に代入するならprevPathは不要では?

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変換だけにしておいた方が良さそう

return err
}
i.Path = newPath
return nil
}
85 changes: 85 additions & 0 deletions kadai1/sh-tatsuno/gophoto/conv/img_test.go
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストの回でやりますが、テーブル駆動テストにすると見通しがよくなります。


// ### Given ###
img, err := NewImg("./lena.jpeg")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストに使うものはtestdataというディレクトリ以下に置くとよいです

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.")
}
})

}
Binary file added kadai1/sh-tatsuno/gophoto/conv/lena.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai1/sh-tatsuno/gophoto/conv/lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions kadai1/sh-tatsuno/gophoto/dir/lookup.go
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
}
73 changes: 73 additions & 0 deletions kadai1/sh-tatsuno/gophoto/dir/lookup_test.go
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
Copy link
Member

Choose a reason for hiding this comment

The 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())
}
})
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading