-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbox.go
162 lines (139 loc) · 3.06 KB
/
inbox.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
package main
import (
"archive/zip"
"fmt"
"io"
"log"
"math"
"os"
"path"
"path/filepath"
"strings"
"time"
)
func arr2cs(v []string) cs {
ts, _ := time.Parse("20060102 15:04 MST", v[0]+" "+v[1]+" IST")
return cs{
UTC: uint32(ts.Unix()),
O: uint32(math.Round(str2float64(v[2]) * 100)),
H: uint32(math.Round(str2float64(v[3]) * 100)),
L: uint32(math.Round(str2float64(v[4]) * 100)),
C: uint32(math.Round(str2float64(v[5]) * 100)),
V: str2u32(v[6]),
OI: str2u32(v[7]),
}
}
func processInTextFile(fp string) {
dbWrite := DBWriter{}
log.Printf("Processing Text, %s\n", fp)
s := fileNameWithNoDirNoExt(fp)
d := readCsvFile(fp)
for _, r := range d {
fmt.Printf("%d ", len(r))
if len(r) == 9 {
cs := arr2cs(r[1:])
s = r[0]
fmt.Printf("%v\n", cs)
dbWrite.Add(s, cs)
} else if len(r) == 8 {
cs := arr2cs(r)
fmt.Printf("%v\n", cs)
dbWrite.Add(s, cs)
}
}
dbWrite.Flushwrite()
os.Remove(fp)
}
// Closure to address file descriptors issue with all the deferred .Close() methods
func extractAndProcess(f *zip.File, dest string) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
fp := filepath.Join(dest, f.Name)
if _, err := os.Stat(filepath.Dir(fp)); os.IsNotExist(err) {
log.Println("creating dir, ", filepath.Dir(fp))
os.MkdirAll(filepath.Dir(fp), os.ModePerm)
}
// Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(fp, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", fp)
}
if f.FileInfo().IsDir() {
os.MkdirAll(fp, f.Mode())
} else {
os.MkdirAll(filepath.Dir(fp), f.Mode())
f, err := os.OpenFile(fp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
//log.Println("saved as, ", fp)
processInFile(fp)
}
return nil
}
func unzipAndProcess(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
for _, f := range r.File {
//log.Println("Unzipping", f.Name)
err := extractAndProcess(f, dest)
if err != nil {
log.Printf("Error Unzipping, %v\n", err)
return err
}
}
return nil
}
func processInZipFile(fp string) {
dir, _ := os.MkdirTemp("", "*")
log.Printf("Processing zip, %s. Extracting into temp dir, %s\n", fp, dir)
unzipAndProcess(fp, dir)
os.RemoveAll(dir)
os.Remove(fp)
}
func processInFile(fp string) {
if fi, err := os.Stat(fp); err == nil {
if !fi.IsDir() {
fpExt := path.Ext(fp)
if fpExt == ".zip" {
processInZipFile(fp)
} else if (fpExt == ".txt") || (fpExt == ".csv") {
processInTextFile(fp)
} else {
os.Remove(fp)
}
}
}
}
func processIncomingFiles() {
filepath.Walk(inboxPath, func(fp string, info os.FileInfo, err error) error {
if err != nil {
return err
}
processInFile(fp)
return nil
})
}