Skip to content
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

Kadai2 edm20627 #51

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7dc7e7a
ひとまず動くものを作成
keethii27 Feb 25, 2021
5aed38c
ユーザ定義型を作成
keethii27 Feb 25, 2021
9dc9755
自作パッケージを作成
keethii27 Feb 25, 2021
7d5d81e
vlid関数をメソッドに変更
keethii27 Feb 25, 2021
7776099
微修正
keethii27 Feb 25, 2021
a504b57
画像変換処理のエラーハンドリング追加
keethii27 Feb 25, 2021
44bbfd1
削除フラグの追加
keethii27 Feb 25, 2021
87c6eb3
GoDocを生成する
keethii27 Feb 25, 2021
44e95d3
サンプル画像を追加
keethii27 Feb 25, 2021
f21ec02
README.md 追加
keethii27 Feb 26, 2021
49c5e89
.gitignore
keethii27 Feb 26, 2021
74fabfa
go mod tidy 実行'
keethii27 Feb 26, 2021
f4be072
.gitignoreの配置変更
keethii27 Apr 7, 2021
183f52f
課題1をコピー
keethii27 Apr 7, 2021
5fefe04
go moduleパス変更
keethii27 Apr 8, 2021
e1ba6c2
リファクタリング
keethii27 Apr 8, 2021
7caa18b
テスト追加
keethii27 Apr 17, 2021
c75f90f
fix エラーハンドリング追加
keethii27 May 2, 2021
6d344e9
fix タイポ
keethii27 May 2, 2021
3f72fd8
fix ネストが深いためValidメソッドをリファクタリング
keethii27 May 2, 2021
38cf933
fix TestValidに正 to 否、否 to 正ケースを追加
keethii27 May 2, 2021
539b81a
fix テスト用画像を使用しテストできるようにtastdataを作成
keethii27 May 2, 2021
1133222
fix DeleteOptionのテスト追加
keethii27 May 2, 2021
6d99bb2
fix 実際にテストでファイル削除しているか確認できるように修正
keethii27 May 6, 2021
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
17 changes: 17 additions & 0 deletions kadai1/edm20627/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_store
12 changes: 12 additions & 0 deletions kadai1/edm20627/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 画像変換コマンド
指定ディレクトリ以下の画像ファイルを変換します。

## 使用方法

`cmd --from jpg --to png ディレクトリ`

## オプション

* `--from(-f)` 変換対象の画像形式(デフォルト: jpg)
* `--to(-t)` 変換後の画像形式(デフォルト: png)
* `--delete(-d)` 変換前の画像を削除(デフォルト: 無効)
3 changes: 3 additions & 0 deletions kadai1/edm20627/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/edm20627/gopherdojo-studyroom/kadai1/edm20627

go 1.15
108 changes: 108 additions & 0 deletions kadai1/edm20627/imageconvert/imageconvert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package imageconvert

import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
)

// Supported extensions.
var SupportedFormat = []string{"png", "jpg", "jpeg", "gif"}

type ConvertImage struct {
filepaths []string
From, To string
DeleteOption bool
}

// Get the target files for image conversion.
func (ci *ConvertImage) Get(dirs []string) error {
for _, dir := range dirs {
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if info == nil {
return errors.New(path + " is not directory")
}
if info.IsDir() || filepath.Ext(path)[1:] != ci.From {
return nil
}
ci.filepaths = append(ci.filepaths, path)
return nil
})
if err != nil {
return err
}
}
return nil
}

// Perform image conversion.
func (ci *ConvertImage) Convert() error {
for _, path := range ci.filepaths {
err := convert(path, ci.To, ci.DeleteOption)
if err != nil {
return err
}
}
return nil
}

// Check for supported extensions.
func (ci *ConvertImage) Valid() bool {
for _, v := range SupportedFormat {
if v == ci.From {
for _, v := range SupportedFormat {
if v == ci.To {
return true
}
}
}
}
return false
}

func convert(path string, to string, deleteOption bool) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

img, _, err := image.Decode(f)
if err != nil {
return err
}

out, err := os.Create(path[:len(path)-len(filepath.Ext(path))+1] + to)
if err != nil {
return err
}
defer out.Close()

switch to {
case "png":
if err := png.Encode(out, img); err != nil {
return err
}
case "jpg", "jpeg":
if err := jpeg.Encode(out, img, nil); err != nil {
return err
}
case "gif":
if err := gif.Encode(out, img, nil); err != nil {
return err
}
}

if deleteOption {
if err := os.Remove(path); err != nil {
return err
}
}

return nil
}
43 changes: 43 additions & 0 deletions kadai1/edm20627/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"flag"
"fmt"
"os"
"strings"

"github.com/edm20627/gopherdojo-studyroom/kadai1/edm20627/imageconvert"
)

var ci = imageconvert.ConvertImage{}

func init() {
flag.StringVar(&ci.From, "-from", "jpg", "Specify the image format before conversion")
flag.StringVar(&ci.From, "f", "jpg", "Specify the image format before conversion (short)")
flag.StringVar(&ci.To, "-to", "png", "Specify the converted image format")
flag.StringVar(&ci.To, "t", "png", "Specify the converted image format (short)")
flag.BoolVar(&ci.DeleteOption, "-delete", false, "Delete the image before conversion")
flag.BoolVar(&ci.DeleteOption, "d", false, "Delete the image before conversion (short)")
}

func main() {
flag.Parse()
dirs := flag.Args()

if !ci.Valid() {
fmt.Fprintln(os.Stderr, "supported formt is "+strings.Join(imageconvert.SupportedFormat, ", "))
os.Exit(1)
}

err := ci.Get(dirs)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

err = ci.Convert()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Binary file added kadai1/edm20627/sample_image/hoge/sample3.png
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/edm20627/sample_image/hoge/sample4.png
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/edm20627/sample_image/sample.jpg
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/edm20627/sample_image/sample2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions kadai2/edm20627/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_store
12 changes: 12 additions & 0 deletions kadai2/edm20627/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 画像変換コマンド
指定ディレクトリ以下の画像ファイルを変換します。

## 使用方法

`cmd --from jpg --to png ディレクトリ`

## オプション

* `--from(-f)` 変換対象の画像形式(デフォルト: jpg)
* `--to(-t)` 変換後の画像形式(デフォルト: png)
* `--delete(-d)` 変換前の画像を削除(デフォルト: 無効)
3 changes: 3 additions & 0 deletions kadai2/edm20627/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/edm20627/gopherdojo-studyroom/kadai2/edm20627

go 1.15
1 change: 1 addition & 0 deletions kadai2/edm20627/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/edm20627/gopherdojo-studyroom v0.0.0-20200808023015-dd3cfe05cf7a h1:qrHQJOIqb8bC1Ulkv5H6eGp3DZgRA93eLUJr6XK/Gjk=
117 changes: 117 additions & 0 deletions kadai2/edm20627/imageconvert/imageconvert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package imageconvert

import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
)

// Supported extensions.
var SupportedFormat = []string{"png", "jpg", "jpeg", "gif"}

var (
ErrNotSpecified = errors.New("Need to specify directory or file")
ErrMotDirectory = errors.New("Specify directory or file is not directory")

Choose a reason for hiding this comment

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

おそらくs/Mot/Not/ですね

)

type ConvertImage struct {
Filepaths []string
From, To string
DeleteOption bool
}

// Get the target files for image conversion.
func (ci *ConvertImage) Get(dirs []string) error {
if len(dirs) == 0 {
return ErrNotSpecified
}

for _, dir := range dirs {
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if info == nil {
return ErrMotDirectory
}
if info.IsDir() || filepath.Ext(path)[1:] != ci.From {
return nil
}
ci.Filepaths = append(ci.Filepaths, path)
return nil
})
if err != nil {
return err
}
}
return nil
}

// Perform image conversion.
func (ci *ConvertImage) Convert() error {
for _, path := range ci.Filepaths {
err := convert(path, ci.To, ci.DeleteOption)
if err != nil {
return err
}
}
return nil
}

// Check for supported extensions.
func (ci *ConvertImage) Valid() bool {
for _, v := range SupportedFormat {
if v == ci.From {
for _, v := range SupportedFormat {
if v == ci.To {
return true
}
}
}
}
return false

Choose a reason for hiding this comment

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

ネストが深いので以下の方が綺麗かなと思いました

	var fromSupported, toSupported bool
	for _, v := range SupportedFormat {
		if v == ci.From {
			fromSupported = true
		}
		if v == ci.To {
			toSupported = true
		}
	}
	return fromSupported && toSupported

}

func convert(path string, to string, deleteOption bool) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

img, _, err := image.Decode(f)
if err != nil {
return err
}

out, err := os.Create(path[:len(path)-len(filepath.Ext(path))+1] + to)
if err != nil {
return err
}
defer out.Close()

switch to {
case "png":
if err := png.Encode(out, img); err != nil {
return err
}
case "jpg", "jpeg":
if err := jpeg.Encode(out, img, nil); err != nil {
return err
}
case "gif":
if err := gif.Encode(out, img, nil); err != nil {
return err
}
}

if deleteOption {
if err := os.Remove(path); err != nil {
return err
}
}

return nil
}
Loading