-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_handlers.go
251 lines (191 loc) · 6.69 KB
/
endpoint_handlers.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 (
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"time"
"math/rand"
"github.com/go-gomail/gomail"
"github.com/gofiber/fiber/v2"
"golang.org/x/crypto/bcrypt"
)
func users(db *sql.DB) ([]byte, error) {
get_users := `SELECT "username", "email" FROM "Users" WHERE "verified" = true`;
results, err := db.Query(get_users);
if err != nil {
return nil, err;
}
defer results.Close();
var users []User;
for results.Next() {
var user User;
if err := results.Scan(&user.Username, &user.Email); err != nil {
return nil, err;
}
users = append(users, user);
}
return json.Marshal(users);
}
func register(c *fiber.Ctx, db *sql.DB) error {
create_db_if_none_exists := `CREATE TABLE IF NOT EXISTS "Users"(
"id" SERIAL PRIMARY KEY,
"fname" VARCHAR(100) NOT NULL,
"lname" VARCHAR(100) NOT NULL,
"email" VARCHAR(100) UNIQUE NOT NULL,
"username" VARCHAR(100) UNIQUE NOT NULL,
"password" VARCHAR(100) NOT NULL,
"verification_code" INT,
"verified" BOOLEAN
)`;
_, err := db.Exec(create_db_if_none_exists);
if err != nil {
return err;
}
hashed_pass, err :=
bcrypt.GenerateFromPassword([]byte(c.FormValue("password")), bcrypt.DefaultCost);
if err != nil {
return err;
}
// NOTE: For uniformity, all verification codes should be 6-digit
verification_code := rand.Intn(899999) + 100000;
register_user := `INSERT INTO "Users"("fname", "lname", "email", "username", "password", "verification_code", "verified")
VALUES($1, $2, $3, $4, $5, $6, $7)`;
_, err = db.Exec(register_user,
c.FormValue("fname"),
c.FormValue("lname"),
c.FormValue("email"),
c.FormValue("username"),
string(hashed_pass),
verification_code,
false );
if err != nil {
return err;
}
verification_mes := gomail.NewMessage();
verification_mes.SetHeader("From", os.Getenv("EMAIL_USER"));
verification_mes.SetHeader("To", c.FormValue("email"));
verification_mes.SetHeader("Subject", "Verify your Account");
verification_mes.SetBody("text/html", `
<h1>Welcome to BlindSight, ` + c.FormValue("fname") + ` ` + c.FormValue("lname") +`!</h1>
<p>To complete your registration, please enter the following code in the BlindSight app:</p>
<p><b>` + fmt.Sprint(verification_code) + `</b></p>
<p>If you do not remember creating an account with BlindSight, please ignore this email.</p>
<br>
<p>Sincerely,</p>
<p>The BlindSight Team</p>
`);
email_dialer := gomail.NewDialer("smtp.gmail.com", 465, os.Getenv("EMAIL_USER"), os.Getenv("EMAIL_PASS"));
if err := email_dialer.DialAndSend(verification_mes); err != nil {
return err;
}
return nil;
}
func verify(c *fiber.Ctx, db *sql.DB) ([]byte, AuthError) {
verify_user := `UPDATE "Users" SET "verified" = true, "verification_code" = NULL WHERE "verification_code" = $1
RETURNING "fname", "lname", "email", "username", "password"`;
verification_code, err := strconv.Atoi(c.FormValue("verification_code"));
if err != nil {
return nil, AuthError(NotANumberError);
}
results, err := db.Query(verify_user, verification_code);
if err != nil {
return nil, AuthError(WrongCredentialsError);
}
for results.Next() {
var user User;
results.Scan(&user.Fname, &user.Lname, &user.Email, &user.Username, &user.Password);
userJSON, err := json.Marshal(user);
if err != nil {
Log(err.Error())
return nil, AuthError(UnkownError);
}
return userJSON, AuthError(Ok);
}
return nil, AuthError(UnkownError);
}
func login(c *fiber.Ctx, db *sql.DB) ([]byte, AuthError) {
results, err := db.Query(`SELECT DISTINCT
"fname",
"lname",
"email",
"username",
"password"
FROM "Users"
WHERE ("username" = '` + c.FormValue("username") +
`' OR "email" = '` + c.FormValue("username") + `') AND
"verified" = true`);
if err != nil {
Log(err.Error());
return nil, AuthError(WrongCredentialsError);
}
for results.Next() {
var user User;
if err := results.Scan(&user.Fname, &user.Lname, &user.Email,
&user.Username, &user.Password); err != nil {
Log(err.Error());
return nil, AuthError(UnkownError);
}
password_match := bcrypt.CompareHashAndPassword(
[]byte(user.Password),
[]byte(c.FormValue("password")));
// NOTE: Checks if no errors ocurred i.e. passwords match
if password_match == nil {
user_data, err := json.Marshal(user);
if err != nil {
Log(err.Error());
return nil, AuthError(UnkownError);
}
return user_data, AuthError(Ok);
} else {
return nil, AuthError(WrongCredentialsError);
}
}
return nil, AuthError(UnkownError);
}
func saveImage(c *fiber.Ctx) ([]byte, error) {
image := ImageFile {
name: c.FormValue("name"),
bytes: c.FormValue("image"),
};
dec, err := base64.StdEncoding.DecodeString(image.bytes);
if err != nil {
return nil, errors.New("Couldn't decode file!\n");
}
const input_dir = "/midas/input/";
const output_dir = "/midas/output/";
const model = "dpt_swin2_tiny_256";
const model_path = "/midas/weights/dpt_swin2_tiny_256.pt";
_, err = os.Stat(input_dir);
if err != nil {
if os.IsNotExist(err) {
os.Mkdir(input_dir, os.ModePerm);
} else {
return nil, err;
}
}
file, err := os.Create(input_dir + image.name + ".png");
if err != nil {
return nil, errors.New("Couldn't create file! \n");
}
defer file.Close();
_, err = file.Write(dec);
if err != nil {
return nil, errors.New("Couldn't write to file!\n");
}
err = file.Sync();
if err != nil {
return nil, errors.New("Couldn't sync file to disk!\n");
}
time.Sleep(time.Millisecond * 1500);
defer os.Remove(output_dir + image.name + "-" + model + ".png");
instruction, err := exec.Command("depth_analyzer", output_dir + image.name + "-" + model + ".png").Output();
if err != nil {
return nil, err;
}
return instruction, nil;
}