-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir.go
243 lines (218 loc) · 5.07 KB
/
dir.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"context"
"encoding/csv"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
)
type zipFile struct {
path string
password string
}
const (
filenameColName = "File Name"
passwordColName = "Zip Password"
)
func unzipDir(ctx context.Context, dirPath string, csvFilePath string) error {
lines, err := readCSVFile(csvFilePath)
if err != nil {
return err
}
err = validateCSVFile(lines)
if err != nil {
return err
}
files := parseZipFiles(dirPath, lines)
err = validateZipFiles(files)
if err != nil {
return err
}
return unzipFiles(ctx, files)
}
func readCSVFile(csvFilePath string) ([][]string, error) {
file, err := os.Open(csvFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open csv file: %w", err)
}
defer file.Close()
reader := csv.NewReader(file)
lines, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("failed to read csv file: %w", err)
}
return lines, nil
}
func validateCSVFile(lines [][]string) error {
err := validateLineCount(lines)
if err != nil {
return err
}
err = validateHeaderColCount(lines)
if err != nil {
return err
}
err = checkEmptyFilenames(lines)
if err != nil {
return err
}
err = checkDuplicateFilenames(lines)
if err != nil {
return err
}
return nil
}
func validateLineCount(lines [][]string) error {
if len(lines) < 2 {
return fmt.Errorf("csv file doesn't have any data")
}
return nil
}
func validateHeaderColCount(lines [][]string) error {
header := lines[0]
if len(header) < 2 {
return errors.New("unexpected column count found on header line")
}
return nil
}
func checkEmptyFilenames(lines [][]string) error {
header := lines[0]
filenameColIndex := findFilenameColIndex(header)
var invalidLineNums []int
for i, line := range lines {
filename := line[filenameColIndex]
if len(filename) == 0 {
invalidLineNums = append(invalidLineNums, i+1)
}
}
if len(invalidLineNums) > 0 {
return fmt.Errorf("empty filename found on line(s) %s", joinLineNums(invalidLineNums))
}
return nil
}
func checkDuplicateFilenames(lines [][]string) error {
header := lines[0]
filenameColIndex := findFilenameColIndex(header)
var duplicateLineNums []int
for i := 0; i < len(lines); i++ {
lineNum := i + 1
if lineNumExists(lineNum, duplicateLineNums) {
continue
}
filename := lines[i][filenameColIndex]
for j := i + 1; j < len(lines); j++ {
existingLineNum := j + 1
if lineNumExists(existingLineNum, duplicateLineNums) {
continue
}
existingFilaname := lines[j][filenameColIndex]
if filename == existingFilaname {
if !lineNumExists(lineNum, duplicateLineNums) {
duplicateLineNums = append(duplicateLineNums, lineNum)
}
duplicateLineNums = append(duplicateLineNums, existingLineNum)
}
}
}
if len(duplicateLineNums) > 0 {
return fmt.Errorf("duplicate filenames found on lines %s", joinLineNums(duplicateLineNums))
}
return nil
}
func parseZipFiles(dirPath string, lines [][]string) []zipFile {
header := lines[0]
filenameColIndex := findFilenameColIndex(header)
passwordColIndex := findPasswordColIndex(header)
var files []zipFile
for i := 1; i < len(lines); i++ {
line := lines[i]
filename := line[filenameColIndex]
filePath := filepath.Join(dirPath, filename)
password := line[passwordColIndex]
file := zipFile{
path: filePath,
password: password,
}
files = append(files, file)
}
return files
}
func validateZipFiles(files []zipFile) error {
var errs []error
for _, file := range files {
fileInfo, err := os.Stat(file.path)
if err != nil {
errs = append(errs, fmt.Errorf("failed to get file info for '%s': %w", file.path, err))
continue
}
if !fileInfo.Mode().IsRegular() {
errs = append(errs, fmt.Errorf("'%s' is not a regular file", file.path))
}
}
if len(errs) > 0 {
return makeMultiErr("failed to validate zip files in csv file", errs)
}
return nil
}
func unzipFiles(ctx context.Context, files []zipFile) error {
var mu sync.Mutex
var errs []error
maxConcurrency := runtime.NumCPU()
sem := newSemaphore(maxConcurrency)
for _, file := range files {
sem.acquire()
go func(filePath string, password string) {
err := unzipFile(ctx, filePath, password)
if err != nil {
mu.Lock()
errs = append(errs, err)
mu.Unlock()
}
sem.release()
}(file.path, file.password)
}
sem.wait()
if len(errs) > 0 {
return joinMultiErrs(errs)
}
return nil
}
func findFilenameColIndex(header []string) int {
for i, col := range header {
if col == filenameColName {
return i
}
}
return 0
}
func findPasswordColIndex(header []string) int {
for i, col := range header {
if col == passwordColName {
return i
}
}
return len(header) - 1
}
func joinLineNums(lineNums []int) string {
var builder strings.Builder
for i, lineNum := range lineNums {
builder.WriteString(strconv.Itoa(lineNum))
if i < len(lineNums)-1 {
builder.WriteString(", ")
}
}
return builder.String()
}
func lineNumExists(lineNum int, lineNums []int) bool {
for _, existingLineNum := range lineNums {
if lineNum == existingLineNum {
return true
}
}
return false
}