|
| 1 | +package converter |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "image" |
| 7 | + "image/jpeg" |
| 8 | + "image/png" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// Converter Convert jpg to png or png to jpg. |
| 15 | +// In represent a input file type. |
| 16 | +// Out represent a output file type. |
| 17 | +// Directory represent a target directory. |
| 18 | +type Converter struct { |
| 19 | + in string |
| 20 | + out string |
| 21 | + directory string |
| 22 | +} |
| 23 | + |
| 24 | +// FileSet is Set of input file name and output file name. |
| 25 | +type FileSet struct { |
| 26 | + inputFileName string |
| 27 | + outputFileName string |
| 28 | +} |
| 29 | + |
| 30 | +// New return a new Converter. |
| 31 | +func New(in, out, directory string) *Converter { |
| 32 | + return &Converter{in: in, out: out, directory: directory} |
| 33 | +} |
| 34 | + |
| 35 | +// Run execute image convert function. |
| 36 | +func (c Converter) Run() int { |
| 37 | + var err error |
| 38 | + fileSetSlice, err := c.dirWalk() |
| 39 | + if err != nil { |
| 40 | + fmt.Println(err) |
| 41 | + return 1 |
| 42 | + } |
| 43 | + err = c.convert(fileSetSlice) |
| 44 | + if err != nil { |
| 45 | + fmt.Println(err) |
| 46 | + return 1 |
| 47 | + } |
| 48 | + return 0 |
| 49 | +} |
| 50 | + |
| 51 | +// dirWalk returns file set of input file name and output file in target directory. |
| 52 | +func (c Converter) dirWalk() ([]FileSet, error) { |
| 53 | + fileSetSlice := make([]FileSet, 0, 50) |
| 54 | + err := filepath.Walk(c.directory, func(path string, info os.FileInfo, err error) error { |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + if filepath.Ext(info.Name()) == ("." + c.in) { |
| 60 | + inputFileName := path |
| 61 | + outputFileName := c.outputFilePath(inputFileName) |
| 62 | + fileSet := FileSet{inputFileName: inputFileName, outputFileName: outputFileName} |
| 63 | + fmt.Println(inputFileName, outputFileName) |
| 64 | + fileSetSlice = append(fileSetSlice, fileSet) |
| 65 | + } |
| 66 | + return nil |
| 67 | + }) |
| 68 | + |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + return fileSetSlice, nil |
| 74 | +} |
| 75 | + |
| 76 | +// convert calls execute function every file set. |
| 77 | +func (c Converter) convert(fileSetSlice []FileSet) error { |
| 78 | + var err error |
| 79 | + |
| 80 | + for _, fileSet := range fileSetSlice { |
| 81 | + err = c.execute(fileSet) |
| 82 | + if err != nil { |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return err |
| 88 | +} |
| 89 | + |
| 90 | +// execute function open file and calls encode function. |
| 91 | +func (c Converter) execute(fileset FileSet) error { |
| 92 | + var err error |
| 93 | + |
| 94 | + inputFile, err := os.Open(fileset.inputFileName) |
| 95 | + defer inputFile.Close() |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + outputFile, err := os.Create(fileset.outputFileName) |
| 101 | + defer outputFile.Close() |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + img, _, err := image.Decode(inputFile) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + err = c.encode(outputFile, img) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +// encode returns error by encode function. |
| 120 | +// if output type is not support, Encode returns error. |
| 121 | +func (c Converter) encode(file *os.File, image image.Image) error { |
| 122 | + switch c.out { |
| 123 | + case "jpg", "jpeg": |
| 124 | + err := jpeg.Encode(file, image, &jpeg.Options{Quality: 80}) |
| 125 | + return err |
| 126 | + case "png": |
| 127 | + err := png.Encode(file, image) |
| 128 | + return err |
| 129 | + default: |
| 130 | + return errors.New("invalid output type") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// outputFilePath returns output file path to correspond input file path. |
| 135 | +func (c Converter) outputFilePath(inputFileName string) string { |
| 136 | + stringSlice := strings.Split(inputFileName, ".") |
| 137 | + outputFileName := stringSlice[0] + "." + c.out |
| 138 | + return outputFileName |
| 139 | +} |
0 commit comments