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 miyahara #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions kadai2/miyahara/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.DS_Store
22 changes: 22 additions & 0 deletions kadai2/miyahara/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 画像変換コマンド
## 課題内容
- 次の仕様を満たすコマンドを作って下さい
- ディレクトリを指定する
- 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト)
- ディレクトリ以下は再帰的に処理する
- 変換前と変換後の画像形式を指定できる(オプション)
- 以下を満たすように開発してください
- mainパッケージと分離する
- 自作パッケージと標準パッケージと準標準パッケージのみ使う
- 準標準パッケージ:golang.org/x以下のパッケージ
- ユーザ定義型を作ってみる
- GoDocを生成してみる

## 実行方法
実行するために以下のコマンドを実行してください。
```
go build -o conv
./conv -i=[input file type] -o=[output file type] [target directory]

```
`input file type` と`output file type` はjpgかpngを選択してください。
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.
139 changes: 139 additions & 0 deletions kadai2/miyahara/converter/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package converter

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

// Converter Convert jpg to png or png to jpg.
// In represent a input file type.
// Out represent a output file type.
// Directory represent a target directory.
type Converter struct {
in string
out string
directory string
}

// FileSet is Set of input file name and output file name.
type FileSet struct {
inputFileName string
outputFileName string
}

// New return a new Converter.
func New(in, out, directory string) *Converter {
return &Converter{in: in, out: out, directory: directory}
}

// Run execute image convert function.
func (c Converter) Run() int {
var err error
fileSetSlice, err := c.dirWalk()
if err != nil {
fmt.Println(err)
return 1
}
err = c.convert(fileSetSlice)
if err != nil {
fmt.Println(err)
return 1
}
return 0
}

// dirWalk returns file set of input file name and output file in target directory.
func (c Converter) dirWalk() ([]FileSet, error) {
fileSetSlice := make([]FileSet, 0, 50)
err := filepath.Walk(c.directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if filepath.Ext(info.Name()) == ("." + c.in) {
inputFileName := path
outputFileName := c.outputFilePath(inputFileName)
fileSet := FileSet{inputFileName: inputFileName, outputFileName: outputFileName}
fmt.Println(inputFileName, outputFileName)
fileSetSlice = append(fileSetSlice, fileSet)
}
return nil
})

if err != nil {
return nil, err
}

return fileSetSlice, nil
}

// convert calls execute function every file set.
func (c Converter) convert(fileSetSlice []FileSet) error {
var err error

for _, fileSet := range fileSetSlice {
err = c.execute(fileSet)
if err != nil {
break
}
}

return err
}

// execute function open file and calls encode function.
func (c Converter) execute(fileset FileSet) error {
var err error

inputFile, err := os.Open(fileset.inputFileName)
defer inputFile.Close()
if err != nil {
return err
}

outputFile, err := os.Create(fileset.outputFileName)
defer outputFile.Close()
Copy link

Choose a reason for hiding this comment

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

https://play.golang.org/p/S9YecKjLVo8
例えば、すでにあるディレクトリに対して os.Create を実行します。
当然エラーが返ってきますが、そのとき outputFile の中身はどうなっていますか?
その場合に outputFile.Close() を呼ぶとどうなるか試してみてください。

if err != nil {
return err
}

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

err = c.encode(outputFile, img)
if err != nil {
return err
}

return nil
}

// encode returns error by encode function.
// if output type is not support, Encode returns error.
func (c Converter) encode(file *os.File, image image.Image) error {
switch c.out {
case "jpg", "jpeg":
err := jpeg.Encode(file, image, &jpeg.Options{Quality: 80})
return err
case "png":
err := png.Encode(file, image)
return err
default:
return errors.New("invalid output type")
}
}

// outputFilePath returns output file path to correspond input file path.
func (c Converter) outputFilePath(inputFileName string) string {
stringSlice := strings.Split(inputFileName, ".")
Copy link

Choose a reason for hiding this comment

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

Mac だと /Applications/Slack.app/Contents/Resources/app.asar.unpacked/src/static/slack-menubar-highlight.png というパスに画像ファイルがあるんですが、正しく変換されますか?

outputFileName := stringSlice[0] + "." + c.out
return outputFileName
}
Loading