-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
80 lines (65 loc) · 1.83 KB
/
api.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
77
78
79
80
package imageupsizer
import (
"io/ioutil"
"os"
"path"
"path/filepath"
log "github.com/sirupsen/logrus"
)
func FindLargerImageFromFile(filename string) (*ImageData, error) {
originalImage, err := GetImageConfigFromFile(filename)
if err != nil {
return nil, err
}
log.Infof("[%s] Uploading to google image server...", filename)
contents, err := uploadImage(filename)
if err != nil {
return nil, err
}
largerImage, err := getImageList(contents)
if err != nil {
return nil, err
}
if largerImage.Area > originalImage.Width*originalImage.Height {
return largerImage, nil
}
return nil, ErrNoLargerAvailable
}
func GetLargerImageFromFile(filename, outputDir string) (*ImageData, error) {
var largerImage, err = FindLargerImageFromFile(filename)
if err != nil {
return nil, err
}
imageInfo, err := getImage(largerImage.URL)
if err != nil {
return nil, err
}
var newFile = filepath.Join(outputDir, path.Base(imageInfo.URL))
if err := ioutil.WriteFile(newFile, imageInfo.Bytes, os.ModePerm); err != nil {
log.Error(err)
}
imageInfo.LocalPath = newFile
return imageInfo, ioutil.WriteFile(path.Base(imageInfo.URL), imageInfo.Bytes, 0755)
}
func FindLargerImageFromBytes(image []byte, outputFile string) (*ImageData, error) {
var tmpfile = "FindLargerImageFromBytesTmpfile.image"
defer os.Remove(tmpfile)
var err = ioutil.WriteFile(tmpfile, image, 0755)
if err != nil {
return nil, err
}
largerImage, err := FindLargerImageFromFile(tmpfile)
if err != nil {
return nil, err
}
return largerImage, os.Remove(tmpfile)
}
func GetLargerImageFromBytes(image []byte, outputDir string) (*ImageData, error) {
var tmpfile = "GetLargerImageFromBytesTmpfile.image"
defer os.Remove(tmpfile)
var err = ioutil.WriteFile(tmpfile, image, 0755)
if err != nil {
return nil, err
}
return GetLargerImageFromFile(tmpfile, outputDir)
}