-
Notifications
You must be signed in to change notification settings - Fork 6
Kadai1 marltake #14
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
Closed
Closed
Kadai1 marltake #14
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 課題1回答 | ||
## 画像変換コマンド仕様 | ||
* 指定されたディレクトリ以下の画像ファイルを再帰的に検索して処理する | ||
* デフォルトはJPGファイルをPNGファイルに変換 | ||
* オプションで変更前後の形式を指定可能 | ||
* 前後のフォーマットが同じオプションは指定不可 | ||
* 返還後のファイル名が存在する場合は、処理をskip | ||
* -i 問い合わせる、-f 強制上書きとかできるかな | ||
## 実装条件 | ||
* mainパッケージと分離する | ||
* 自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
* 準標準パッケージ:golang.org/x以下のパッケージ | ||
* ユーザ定義型を作ってみる | ||
* GoDocを生成してみる |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package convert | ||
|
||
import ( | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func ConfigConvert(src string, dest string) func(string, os.FileInfo, error) error { | ||
srcExt := "." + src | ||
lenSrcExt := len(srcExt) | ||
destExt := "." + dest | ||
// TODO declare decode for src and encode for dest here | ||
return func(path string, info os.FileInfo, err error) error { | ||
println(path) | ||
if strings.ToLower(filepath.Ext(path)) == srcExt { | ||
destPath := path[:len(path)-lenSrcExt] + destExt | ||
// TODO error handling | ||
if _, err := os.Stat(destPath); os.IsNotExist(err) { | ||
file, _ := os.Open(path) | ||
defer file.Close() | ||
var img image.Image | ||
switch src { | ||
case "jpg": | ||
img, _ = jpeg.Decode(file) | ||
case "png": | ||
img, _ = png.Decode(file) | ||
case "gif": | ||
img, _ = gif.Decode(file) | ||
} | ||
destfile, _ := os.Create(destPath) | ||
defer destfile.Close() | ||
switch dest { | ||
case "jpg": | ||
jpeg.Encode(destfile, img, nil) | ||
case "png": | ||
png.Encode(destfile, img) | ||
case "gif": | ||
gif.Encode(destfile, img, nil) | ||
} | ||
} else { | ||
println("skip not to over write.", path) | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func ParseTarget(target string) (src string, dest string, ok bool) { | ||
targets := strings.Split(target, ",") | ||
allowedExt := map[string]bool{ | ||
"jpg": true, | ||
"png": true, | ||
"gif": true, | ||
} | ||
if len(targets) != 2 { | ||
return "", "", false | ||
} | ||
if targets[0] == "" { | ||
targets[0] = "Jpg" | ||
} | ||
if targets[1] == "" { | ||
targets[1] = "png" | ||
} | ||
if targets[0] != targets[1] && allowedExt[targets[0]] && allowedExt[targets[1]] { | ||
return targets[0], targets[1], true | ||
} | ||
return "", "", false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"path/filepath" | ||
|
||
"./convert" | ||
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. 相対 import は不可になる。 |
||
) | ||
|
||
func main() { | ||
var target = flag.String("t", "jpg,png", "source and destination picture type") | ||
flag.Parse() | ||
src, dest, ok := convert.ParseTarget(*target) | ||
if !ok { | ||
log.Fatal(fmt.Errorf("Invalid target option %s", *target)) | ||
} | ||
err := filepath.Walk(flag.Arg(0), convert.ConfigConvert(src, dest)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
名前付きリターンを使うなら、src, dest, ok に代入して return で返すべき