-
Notifications
You must be signed in to change notification settings - Fork 3
/
nic_generator.go
264 lines (220 loc) · 6.3 KB
/
nic_generator.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
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"encoding/base64"
"fmt"
"github.com/Pallinder/go-randomdata"
"github.com/boombuler/barcode/pdf417"
"github.com/gin-gonic/gin"
"image/png"
"io/ioutil"
"math"
"net/http"
"os"
"strings"
"time"
)
func generator(c *gin.Context) {
layout := "2006-01-02"
dqs := c.Query("date")
provinces := []string{"Western", "Central", "Southern", "Northern", "Eastern", "North Western", "North Central", "Uva", "Sabaragahmuwa"}
date, err := dateQueryHandler(dqs) // Date query string
if err != nil {
sendErrorJsonGenerator(c, err, http.StatusBadRequest)
return
}
sqs := c.Query("sex") // Sex query string
sex, sas, err := sexQueryHandler(sqs)
if err != nil {
sendErrorJsonGenerator(c, err, http.StatusBadRequest)
return
}
fdoy := time.Date(date.Year(), 1, 1, 0, 0, 0, 0, time.UTC) // First day of the year
doy := int(math.Ceil(date.Sub(fdoy).Hours()/24) + 1) // Day of the year
sn := randomdata.Number(0, 1000) // Serial number
if date.Year() >= 2000 {
sn = randomdata.Number(0, 10000)
}
cd := randomdata.Number(0, 10) // Check digit
sdoy := doy // Day of the year according to sex
if sex == false {
sdoy += 500
}
onic := generateONIC(date.Year(), sdoy, sn, cd)
osn := fmt.Sprintf("%03d", sn) // Old serial number
if len(onic) != 10 {
onic = ""
osn = ""
}
nnic := generateNNIC(date.Year(), sdoy, sn, cd)
nsn := fmt.Sprintf("%04d", sn) // New serial number
barcode := ""
barcodeContent := ""
if len(nnic) != 12 {
nnic = ""
nsn = ""
} else {
barcodeContent, barcode, err = generateBarcodeForNNIC(nnic, date, sas)
if err != nil {
sendErrorJsonGenerator(c, err, http.StatusBadRequest)
return
}
}
pn := randomdata.Number(0, 9) // Province number. This is not associated with the NIC number
ps := fmt.Sprintf("%v Province", provinces[pn]) // String of province
c.JSON(http.StatusOK, gin.H{
"status": true,
"date": date.Format(layout),
"doy": doy,
"sn": gin.H{
"old": osn, // Old serial number
"new": nsn, // New serial number
},
"cd": cd,
"sex": sas,
"onic": onic, // Old nic version
"nnic": nnic, // New nic version
"province": gin.H{ // Province is not associated with the NIC number
"number": pn + 1,
"name": ps,
},
"barcode": gin.H{
"content": barcodeContent,
"image": barcode,
},
})
}
// Handle error response if any error occurred for generator
func sendErrorJsonGenerator(c *gin.Context, err error, code int) {
c.JSON(code, gin.H{
"status": false,
"error": err.Error(),
"code": http.StatusText(code),
})
}
// Handles a date query param. If not available it auto generate random date
func dateQueryHandler(dqs string) (time.Time, error) {
layout := "2006-01-02"
date := time.Now()
var err error = nil
if len(dqs) > 0 {
date, err = time.Parse(layout, dqs)
} else {
db18 := time.Now().AddDate(-18, 0, 0).Format(layout) // Date 18 years before today
db118 := time.Now().AddDate(-118, 0, 0).Format(layout) // Date 118 years before today
date, err = time.Parse("Monday 2 Jan 2006", randomdata.FullDateInRange(db118, db18))
}
return date, err
}
// Handles the sex query param and return a boolean, string and error
func sexQueryHandler(sqs string) (bool, string, error) {
sqs = strings.ToLower(sqs)
switch sqs {
case "m", "male":
{
return true, "Male", nil
}
case "f", "female":
{
return false, "Female", nil
}
case "":
{
rs := randomdata.Boolean() // Random sex boolean
rss := "Male" // initialize the the sex string
// Change the sex string if rs is false
if rs == false {
rss = "Female"
}
return rs, rss, nil
}
default:
return false, "", fmt.Errorf("Sex parameter can not be parsed.")
}
}
// Generate old nic version according to year, day of the year, serial number and check digit
func generateONIC(year int, doy int, sn int, cd int) string {
if sn > 999 || year > 2000 {
return ""
}
sy := year % 100
ssy := fmt.Sprintf("%v", sy)
if sy < 10 {
ssy = fmt.Sprintf("0%v", sy)
}
return fmt.Sprintf("%v%03d%03d%d%v", ssy, doy, sn, cd, "V")
}
// Generate new nic version according to year, day of the year, serial number and check digit
func generateNNIC(year int, doy int, sn int, cd int) string {
return fmt.Sprintf("%d%03d%04d%d", year, doy, sn, cd)
}
// Generate the pdf417 barcode for the new NIC number
func generateBarcodeForNNIC(nnic string, date time.Time, sas string) (string, string, error) {
layoutOne := "2006-01-02"
layoutTwo := "02/01/2006"
fullName := ""
if sas == "Male" {
fullName = randomdata.FullName(randomdata.Male)
} else {
fullName = randomdata.FullName(randomdata.Female)
}
db := date.Format(layoutOne)
db16 := date.AddDate(16, 0, 0).Format(layoutOne) // Date 16 years after today
createdDate, err := time.Parse("Monday 2 Jan 2006", randomdata.FullDateInRange(db, db16))
if err != nil {
return "", "", err
}
barcodeStr := "00\n" +
nnic + "\n" +
date.Format(layoutTwo) + "\n" +
sas + "\n" +
createdDate.Format(layoutOne) + "\n" +
"00BFT-710\n" +
fullName + "\n" +
randomdata.Address() + "\n" +
randomdata.City() + "\n" +
"485406C0548FDD8FDDF300F312EE947D#"
currentTime := time.Now().Unix()
pdf417Code, err := pdf417.Encode(barcodeStr, 0)
if err != nil {
return "", "", err
}
// create the output file
file, err := os.Create(fmt.Sprintf("barcode-%v.png", currentTime))
if err != nil {
return "", "", err
}
defer file.Close()
err = png.Encode(file, pdf417Code)
if err != nil {
return "", "", err
}
bytes, err := ioutil.ReadFile(fmt.Sprintf("./barcode-%v.png", currentTime))
if err != nil {
return "", "", err
}
err = os.Remove(fmt.Sprintf("barcode-%v.png", currentTime))
if err != nil {
return "", "", err
}
mimeType := http.DetectContentType(bytes)
// Prepend the appropriate URI scheme header depending
// on the MIME type
base64Encoding := getMimeType(mimeType)
// Append the base64 encoded output
base64Encoding += toBase64(bytes)
// Print the full base64 representation of the image
return pdf417Code.Content(), base64Encoding, nil
}
func toBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func getMimeType(mimetype string) string {
base64Encoding := ""
switch mimetype {
case "image/jpeg":
base64Encoding = "data:image/jpeg;base64,"
case "image/png":
base64Encoding = "data:image/png;base64,"
}
return base64Encoding
}