-
Notifications
You must be signed in to change notification settings - Fork 0
/
osx.go
76 lines (61 loc) · 1.35 KB
/
osx.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 osx
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/just-hms/dow/pkg/bytex"
)
func DownloadFolderPath() (string, error) {
s := os.Getenv("DOW_DOWNLOAD_PATH")
if s != "" {
return s, nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, "Downloads"), nil
}
func Move(sourcePath, destPath string) error {
fInfo, err := os.Stat(destPath)
if err == nil && fInfo.IsDir() {
destPath = filepath.Join(destPath, filepath.Base(sourcePath))
}
return os.Rename(sourcePath, destPath)
}
func LatestFile(files []fs.DirEntry) (os.FileInfo, error) {
var latestFile os.FileInfo
for _, file := range files {
fInfo, err := file.Info()
// Skip files that were deleted since listing
if os.IsNotExist(err) {
continue
}
// Return on unexpected error
if err != nil {
return nil, err
}
if latestFile == nil || fInfo.ModTime().After(latestFile.ModTime()) {
latestFile = fInfo
}
}
if latestFile == nil {
return nil, errors.New("no file found")
}
return latestFile, nil
}
func Size(f fs.FileInfo) string {
s := float64(f.Size())
if s > bytex.GB {
return fmt.Sprintf("%.2f GB", s/bytex.GB)
}
if s > bytex.MB {
return fmt.Sprintf("%.2f MB", s/bytex.MB)
}
if s > bytex.KB {
return fmt.Sprintf("%.2f KB", s/bytex.KB)
}
return fmt.Sprintf("%.2f B", s)
}