-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (132 loc) · 4.92 KB
/
index.js
File metadata and controls
157 lines (132 loc) · 4.92 KB
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
var express=require('express');
var app=express();
var mysql=require('mysql');
var cors=require('cors');
var bodyparser=require('body-parser');
var path=require('path');
var code = 200;
var fs = require('fs');
var connection=mysql.createConnection(
{
host:'localhost',
user:'root',
password:'',
database:'portal'
}
);
connection.connect(function(err){
if(err)
{
console.log('error');
}
else{
console.log('connected');
}
});
app.use(cors()); //add middlewere
//app.use(bodyparser.json());//bodyparser
app.use(bodyparser.json({
limit: '50mb'
}));
app.use(bodyparser.urlencoded({
limit: '50mb',
extended: false
}));
app.use(express.static('public'))
app.post('/upload',(req,res,next)=>{
const newRecord={
token_id: req.body.token_id,
email_id: req.body.nickname+'@gmail.com',
name: req.body.name,
picture: req.body.picture,
}
connection.query('select *from users where user_email="'+newRecord.email_id+'"',function(error,rows,fields){
if(!!error)
{
console.log('error in db');
}
else{
if(rows.length<=0)
{
var sql = 'INSERT INTO users (token_id,user_name,user_email,picture) VALUES ("'+req.body.token_id+'","'+req.body.name+'","'+newRecord.email_id+'","'+req.body.picture+'")';
console.log(sql);
connection.query(sql, function (err, result) {
if (err)
{throw err;
}
else{
console.log("1 record inserted");
res.json({ msg: 'sucess'});
}
});
}
else{
}
}
});
});
app.post('/admin_login',(req,res,next)=>{
const admin_user={
user_id: req.body.user_name,
password: req.body.password
}
connection.query('select *from users where user_name="'+admin_user.user_id+'" and token_id="'+admin_user.password+'"',function(error,rows,fields){
if(!!error)
{
console.log('error in db');
}
else{
if(rows.length>0)
{
res.send(admin_user);
}
else{
res.send('error');
}
}
});
});
app.get('/userlist',(req,res,next)=>{
connection.query("select *from users",function(error,rows,fields){
if(!!error)
{
console.log('error in db');
}
else{
res.json(rows);
}
});
});
app.get('/imglist',(req,res,next)=>{
connection.query("select *from records",function(error,rows,fields){
if(!!error)
{
console.log('error in db');
}
else{
res.json(rows);
}
});
});
app.post('/saveImageByEmail',(req,res,next)=>{
var model = req.body.model;
var dt = new Date();
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
var base64d = model.client_img.replace(/^data:image\/png;base64,/, "");
var path = "./public/images/" + text + dt.getDate() + dt.getMonth() + dt.getMilliseconds() + ".png";
var pathSub = "/images/" + text + dt.getDate() + dt.getMonth() + dt.getMilliseconds() + ".png";
fs.writeFile(path, base64d, 'base64', function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
var sql = 'INSERT INTO records (uploaded_by, img_des, img) VALUES ("'+model.email+'","'+model.description+'","'+ pathSub+'")';
connection.query(sql, function (err, result) {
if (err) throw err;
else res.json({status: "staus", code: code});
});
});
app.listen(3000);