forked from noelyahan/impexp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
76 lines (67 loc) · 1.48 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package impexp
import (
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
"log"
"os"
)
type file struct {
path string
img image.Image
}
// NewFileImporter uses to import jpg, png images from given path
func NewFileImporter(path string) Importer {
return file{path, nil}
}
// NewFileExporter uses to export jpg, png images to given path
func NewFileExporter(img image.Image, path string) Exporter {
return file{path, img}
}
func (o file) Import() (image.Image, error) {
errMsg := "impexp cannot read or decode !"
ext := getExt(o.path)
f, err := os.Open(o.path)
defer f.Close()
if err != nil {
return nil, errors.New(errMsg)
}
var img image.Image
if ext == "jpg" || ext == "jpeg" {
img, err = jpeg.Decode(f)
} else if ext == "png" {
img, err = png.Decode(f)
} else {
return nil, errors.New(errMsg)
}
if err != nil {
return nil, errors.New(errMsg)
}
return img, nil
}
func (o file) Export() error {
img := o.img
if img == nil {
return errors.New("impexp found a invalid file ")
}
ext := getExt(o.path)
f, err := os.Create(o.path)
defer f.Close()
if err != nil {
msg := fmt.Sprintf("Sorry impexp failed to create: %s", o.path)
log.Printf(msg)
return errors.New(msg)
}
if ext == "jpg" || ext == "jpeg" {
err = jpeg.Encode(f, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
} else if ext == "png" {
err = png.Encode(f, img)
}
if err != nil {
msg := fmt.Sprintf("Sorry impexp cannot encode the image: %s", o.path)
return errors.New(msg)
}
return nil
}