This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
134 lines (113 loc) · 3.35 KB
/
files.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package mailimage
import (
"fmt"
"math/rand"
"os"
"path"
"time"
"github.com/disintegration/imaging"
"golang.org/x/xerrors"
)
// mailimagePath returns the path where the mails and images are saved.
func mailimagePath() string {
if os.Getenv("MAILIMAGE_PATH") == "" {
return defaultMailimagePath
}
return os.Getenv("MAILIMAGE_PATH")
}
type mailFile struct {
*os.File
name string
folder string
}
// Create creates a new mailFile with the date as the name
func newMailFile() (*mailFile, error) {
f := mailFile{
name: fmt.Sprintf("%s-%02d.eml", time.Now().Format("2006-01-02_15-04-05"), rand.Intn(99)),
folder: "progress",
}
fi, err := os.Create(f.path())
if err != nil {
return nil, xerrors.Errorf("can not open image file for writing: %w", err)
}
f.File = fi
return &f, nil
}
// move moves the file from one folder to another
func (f *mailFile) move(folder string) error {
if err := os.MkdirAll(path.Join(mailimagePath(), folder), os.ModePerm); err != nil {
return xerrors.Errorf("can not create folder %s: %w", folder, err)
}
if err := os.Rename(f.path(), path.Join(mailimagePath(), folder, f.name)); err != nil {
return xerrors.Errorf("can not move mail from %s to %s: %w", f.folder, folder, err)
}
f.folder = folder
return nil
}
// remove changes the name of the file.
func (f *mailFile) rename(name string) error {
if err := os.Rename(f.path(), path.Join(mailimagePath(), f.folder, name)); err != nil {
return xerrors.Errorf("can not rename mail from %s to %s: %w", f.name, name, err)
}
f.name = name
return nil
}
// path returns the full path of the file (including the name of the file)
func (f *mailFile) path() string {
return path.Join(mailimagePath(), f.folder, f.name)
}
func openImage(id int, ext string) (*os.File, error) {
path := path.Join(mailimagePath(), "images", fmt.Sprintf("%d%s", id, ext))
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, errUnknownImage
}
return nil, xerrors.Errorf("can not open image %d%s: %s", id, ext, err)
}
return f, nil
}
func openThumbnail(id int, redis *pool) (*os.File, error) {
path := path.Join(mailimagePath(), "thumbnail", fmt.Sprintf("%d.jpg", id))
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
if err := createThumbnail(id, redis); err != nil {
return nil, err
}
return openThumbnail(id, redis)
}
return nil, xerrors.Errorf("can not open thumbnail: %w", err)
}
return f, nil
}
func createThumbnail(id int, redis *pool) error {
ext, err := redis.getExtension(id)
if err != nil {
return err
}
f, err := openImage(id, ext)
if err != nil {
return xerrors.Errorf("can not open image %s%ext: %w", id, ext, err)
}
image, err := imaging.Decode(f)
if err != nil {
f.Close()
return xerrors.Errorf("can not read image: %w", err)
}
f.Close()
// scale image
image = imaging.Fill(image, 250, 200, imaging.Center, imaging.Lanczos)
if err := os.MkdirAll(path.Join(mailimagePath(), "thumbnail"), os.ModePerm); err != nil {
return xerrors.Errorf("can not create folder thumbnail: %w", err)
}
f, err = os.Create(path.Join(mailimagePath(), "thumbnail", fmt.Sprintf("%d.jpg", id)))
if err != nil {
return xerrors.Errorf("can not create thumbnail: %w", err)
}
defer f.Close()
if err := imaging.Encode(f, image, imaging.JPEG); err != nil {
return xerrors.Errorf("can not write thumbnail: %w", err)
}
return nil
}