-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
114 lines (103 loc) · 2.15 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
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
package file
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/Defacto2/df2/pkg/directories"
"github.com/Defacto2/df2/pkg/logs"
"github.com/dustin/go-humanize"
"github.com/gookit/color"
gap "github.com/muesli/go-app-paths"
)
const (
gif = ".gif"
jpg = ".jpg"
jpeg = ".jpeg"
_png = ".png"
tif = ".tif"
tiff = ".tiff"
)
// Image is an image object.
type Image struct {
ID uint
UUID string
Name string
Ext string
Size int
}
func (i Image) String() string {
return fmt.Sprintf("(%v) %v %v ",
color.Primary.Sprint(i.ID), i.Name,
color.Info.Sprint(humanize.Bytes(uint64(i.Size))))
}
func (i Image) IsExt() (ok bool) {
switch filepath.Ext(strings.ToLower(i.Name)) {
case gif, jpg, jpeg, _png, tif, tiff:
return true
}
return false
}
func (i Image) IsDir(dir *directories.Dir) (ok bool) {
dirs := [2]string{dir.Img000, dir.Img400}
for _, path := range dirs {
if _, err := os.Stat(filepath.Join(path, i.UUID+_png)); !os.IsNotExist(err) {
return true
}
}
return false
}
// Check the existence of the named file and
// confirm it is not a directory or zero-byte file.
func Check(name string, err error) bool {
if err != nil {
return false
}
s, err := os.Stat(name)
if err != nil {
return false
}
if s.IsDir() || s.Size() < 1 {
return false
}
return true
}
// Remove the named file, only when confirm is true.
func Remove(confirm bool, name string) error {
if !confirm {
return nil
}
if err := os.Remove(name); err != nil {
return fmt.Errorf("remove %q: %w", name, err)
}
return nil
}
func RemoveWebP(name string) error {
s, err := os.Stat(name)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return fmt.Errorf("removewebp stat: %w", err)
}
if s.Size() == 0 {
if err := os.Remove(name); err != nil {
return fmt.Errorf("removewebp: %w", err)
}
}
return nil
}
// Vendor is the absolute path to store webpbin vendor downloads.
func Vendor() string {
fp, err := gap.NewScope(gap.User, logs.GapUser).CacheDir()
if err != nil {
h, err := os.UserHomeDir()
if err != nil {
log.Print(fmt.Errorf("vendorPath userhomedir: %w", err))
}
return path.Join(h, ".vendor/df2")
}
return fp
}