/
processor.go
94 lines (74 loc) · 1.84 KB
/
processor.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
package jpeg
import (
"github.com/mateussouzaweb/compactor/compactor"
"github.com/mateussouzaweb/compactor/os"
"github.com/mateussouzaweb/compactor/pkg/generic"
"github.com/mateussouzaweb/compactor/pkg/webp"
)
// Init processor
func Init(bundle *compactor.Bundle) error {
err := os.NodeRequire("jpegoptim", "jpegoptim-bin")
if err != nil {
return err
}
return os.NodeRequire("cwebp", "cwebp-bin")
}
// Related processor
func Related(item *compactor.Item) ([]compactor.Related, error) {
var related []compactor.Related
related = append(related, compactor.Related{
Type: "alternative",
Dependency: true,
Source: "",
Path: item.File + ".webp",
Item: compactor.Get(item.Path + ".webp"),
})
return related, nil
}
// Execute processor
func Execute(bundle *compactor.Bundle) error {
destination := bundle.ToDestination(bundle.Item.Path)
err := os.Copy(bundle.Item.Path, destination)
if err != nil {
return err
}
return nil
}
// Optimize processor
func Optimize(bundle *compactor.Bundle) error {
if bundle.ShouldCompress(bundle.Item.Path) {
destination := bundle.ToDestination(bundle.Item.Path)
_, err := os.Exec(
"jpegoptim",
"--quiet",
"--strip-all",
"--all-progressive",
"--overwrite",
destination,
)
if err != nil {
return err
}
}
if bundle.ShouldGenerateProgressive(bundle.Item.Path) {
destination := bundle.ToDestination(bundle.Item.Path)
err := webp.CreateCopy(bundle.Item.Path, destination, 75)
if err != nil {
return err
}
}
return nil
}
// Plugin return the compactor plugin instance
func Plugin() *compactor.Plugin {
return &compactor.Plugin{
Namespace: "jpeg",
Extensions: []string{".jpeg", ".jpg"},
Init: Init,
Related: Related,
Resolve: generic.Resolve,
Execute: Execute,
Optimize: Optimize,
Delete: generic.Delete,
}
}