-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
251 lines (210 loc) · 4.58 KB
/
main.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
244
245
246
247
248
249
250
251
package main
import (
"crypto/sha1"
"encoding/csv"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/prologic/bitcask"
"github.com/urfave/cli"
"html/template"
"io"
"log"
"net/http"
"os"
"strings"
)
var dataDir string
var dbDir string
var ipRestriction string
var DB *bitcask.Bitcask
func upload(c echo.Context) error {
// Read form fields
name := c.FormValue("name")
email := c.FormValue("email")
ip := c.FormValue("IP")
log.Println(name, ip)
candi, err := getDB(ip)
if ipRestriction == "true" {
if err == nil {
//return c.HTML(http.StatusOK, fmt.Sprintf("<p>File already submitted with fields </br>name=%s </br>email=%s.</p>", name, email))
return c.Render(http.StatusOK, "upload.html", struct {
Candidate candidate
Status string
}{
candi,
"File already submitted ",
})
}
}
//-----------
// Read file
//-----------
// Source
file, err := c.FormFile("file")
if err != nil {
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
hash := getshah(name + email)
name_parts := strings.Split(file.Filename, ".")
ext := "." + name_parts[len(name_parts)-1]
if len(name_parts) == 1 {
ext = ""
}
// Destination
dst_path := dataDir + "/" + hash + ext
dst, err := os.Create(dst_path)
if err != nil {
return err
}
defer dst.Close()
// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
candi = candidate{
Name: name,
Email: email,
IPAddr: ip,
Filename: hash + ext,
}
saveDB(candi)
return c.Render(http.StatusOK, "upload.html", struct {
Candidate candidate
Status string
}{
candi,
"File uploaded successfully",
})
//return c.HTML(http.StatusOK, fmt.Sprintf("<p>File %s uploaded successfully with fields </br>name=%s </br>email=%s.</p>", file.Filename, name, email))
}
func InitFlags() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "dbDir",
Usage: "db dir path",
Destination: &dbDir,
EnvVar: "DB_DIR",
Value: "db/",
},
cli.StringFlag{
Name: "dataDir",
Usage: "data dir path",
Destination: &dataDir,
EnvVar: "DATA_DIR",
Value: "data/",
},
cli.StringFlag{
Name: "ipRestriction",
Usage: "enable ip restriction",
Destination: &ipRestriction,
EnvVar: "IP_REST",
Value: "true",
},
}
app.Action = func(c *cli.Context) error {
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Println(err)
}
}
func getshah(in string) string {
h := sha1.New()
h.Write([]byte(in))
sha1_hash := hex.EncodeToString(h.Sum(nil))
return sha1_hash
}
func saveDB(in candidate) {
out, _ := json.Marshal(in)
DB.Put(in.IPAddr, out)
}
func getDB(ip string) (candidate, error) {
data, err := DB.Get(ip)
if err != nil {
fmt.Println(err)
return candidate{}, err
}
out := candidate{}
json.Unmarshal(data, &out)
return out, nil
}
func readAll() {
// Create a csv file
f, err := os.Create(dataDir + "/people.csv")
if err != nil {
fmt.Println(err)
}
defer f.Close()
// Write Unmarshaled json data to CSV file
w := csv.NewWriter(f)
for key := range DB.Keys() {
log.Println(key)
data, _ := DB.Get(key)
candi := candidate{}
json.Unmarshal(data, &candi)
var record []string
record = append(record, candi.Name)
record = append(record, candi.Email)
record = append(record, candi.IPAddr)
record = append(record, candi.Filename)
w.Write(record)
log.Println(candi)
}
w.Flush()
}
type candidate struct {
Name string `json:"name"`
Email string `json:"email"`
IPAddr string `json:"ip_addr"`
Filename string `json:"filename"`
}
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
InitFlags()
//dbFile = "db/db"
log.Println("data dir:", dataDir)
log.Println("db dir:", dbDir)
log.Println("ip restriction:", ipRestriction)
var err error
DB, err = bitcask.Open(dbDir)
if err != nil {
log.Println(err)
}
log.Println(len(os.Args))
if len(os.Args) == 2 {
arg := os.Args[1]
if arg == "csv" {
readAll()
return
}
}
//readAll()
defer DB.Close()
//dataDir = "data/"
t := &Template{
templates: template.Must(template.ParseGlob("public/views/*.html")),
}
e := echo.New()
e.Renderer = t
e.Use(middleware.BodyLimit("1M"))
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/", "public")
e.POST("/upload", upload)
e.Logger.Fatal(e.Start(":1323"))
}