generated from AlexanderMac/go-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
175 lines (157 loc) · 3.44 KB
/
worker.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gofixt
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/h2non/filetype"
"golang.org/x/sync/errgroup"
)
type _FileInfo struct {
filePath string
filePathCut string
mime string
oExt string
realExt string
fixRequired bool
fixed bool
err string
}
func Scan(dirPath string, silent bool) error {
return worker(dirPath, silent, false)
}
func Fix(dirPath string, silent bool) error {
return worker(dirPath, silent, true)
}
func worker(dirPath string, silent bool, needFix bool) error {
dirPathAbs, err := filepath.Abs(dirPath)
if err != nil {
return err
}
if _, err := os.Stat(dirPathAbs); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s doesn't exist", dirPathAbs)
}
return err
}
var mutex sync.Mutex
ctx := context.TODO()
errGrp, _ := errgroup.WithContext(ctx)
start := time.Now()
tableWriter := NewTableWriter()
if !silent {
tableWriter.AddHeader()
}
var procFileCnt int
var fixedFileCnt int
err = filepath.WalkDir(dirPathAbs, func(filePath string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}
if dirEntry.IsDir() || filePath == "" {
return nil
}
errGrp.Go(func() error {
fileInfo, err := getFileInfo(dirPathAbs, filePath)
if err != nil {
return err
}
if fileInfo.err == "" {
fileInfo.fixRequired = fileInfo.mime != "" && fileInfo.oExt != fileInfo.realExt
if needFix {
err := fixFileExt(&fileInfo)
if err != nil {
return err
}
if fileInfo.fixed {
fixedFileCnt++
}
}
}
if !silent {
mutex.Lock()
defer mutex.Unlock()
tableWriter.AddRow(&fileInfo)
}
procFileCnt++
return nil
})
return nil
})
if err != nil {
return err
}
err = errGrp.Wait()
if err != nil {
return err
}
if !silent {
err = tableWriter.Finish()
if err != nil {
return err
}
}
duration := time.Since(start)
fmt.Printf("\n%d file(s) processed and %d file(s) fixed in %v\n", procFileCnt, fixedFileCnt, duration)
return nil
}
func getFileInfo(dirPath string, filePath string) (_FileInfo, error) {
result := _FileInfo{
filePath: filePath,
filePathCut: strings.Replace(filePath, dirPath, "<dir>", 1),
mime: "unknown",
oExt: filepath.Ext(filePath),
}
file, err := os.Open(filePath)
if err != nil {
return _FileInfo{}, err
}
defer file.Close()
head := make([]byte, 512)
_, err = file.Read(head)
if err != nil {
if errors.Is(err, io.EOF) {
result.err = "File is empty"
return result, nil
}
/* TODO: process more expected errors:
- Access is denied.
- The process cannot access the file because it is being used by another process.
*/
return _FileInfo{}, err
}
fileType, err := filetype.Get(head)
if err != nil {
return _FileInfo{}, err
}
if fileType.MIME.Value != "" {
result.mime = fileType.MIME.Value
result.realExt = "." + fileType.Extension
}
return result, nil
}
func fixFileExt(fileInfo *_FileInfo) error {
if fileInfo.fixRequired {
oldPath := fileInfo.filePath
newPath := strings.TrimSuffix(fileInfo.filePath, fileInfo.oExt) + fileInfo.realExt
fileStat, err := os.Stat(newPath)
if err != nil && !os.IsNotExist(err) {
return err
}
if fileStat != nil {
fileInfo.err = "File with the same name is already exists"
return nil
}
if err = os.Rename(oldPath, newPath); err != nil {
return err
}
fileInfo.fixed = true
}
return nil
}