-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
180 lines (170 loc) · 5.51 KB
/
server.js
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
const express = require("express");
const cors = require("cors");
const { open } = require("sqlite");
const path = require("path");
const sqlite3 = require("sqlite3");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser");
require("dotenv").config();
let database;
const app = express();
const corsOptions = {
origin: [
/^https:\/\/restorephotos\.netlify\.app\/.*/,
/^http:\/\/localhost/,
"https://restorephotos.netlify.app",
],
methods: "GET,POST",
credentials: true,
};
app.use(express.json());
app.use(cookieParser());
app.use(cors(corsOptions));
const initializeDBandServer = async () => {
try {
database = await open({
filename: path.join(__dirname, "restore.db"),
driver: sqlite3.Database,
});
// Create the 'user' table if it doesn't exist
const createTableResult = await database.run(`
CREATE TABLE IF NOT EXISTS user (
username TEXT NOT NULL,
password TEXT NOT NULL
);
`);
if (createTableResult.changes > 0) {
console.log("Table 'user' created successfully.");
}
app.listen(8080, () => {
console.log("Restore back is running on http://localhost:8080/");
});
} catch (error) {
console.log(`Database error is ${error.message}`);
process.exit(1);
}
};
initializeDBandServer();
//ap1 register user
app.post("/register", async (request, response) => {
const { username, password, secret } = request.body;
if (!secret || secret !== process.env.SECRET) {
return response.status(400).send("Invalid Secret");
}
const checkUser = `select username from user where username='${username}';`;
const dbUser = await database.get(checkUser);
console.log(dbUser);
if (dbUser !== undefined) {
response.status(400).send("User already exists");
} else {
if (password.length < 6) {
response.status(400);
response.send("Password is too short");
} else {
const hashedPassword = await bcrypt.hash(password, 10);
const requestQuery = `insert into user(username, password) values(
'${username}','${hashedPassword}');`;
await database.run(requestQuery);
response.status(200);
response.send("User created successfully");
}
}
});
//api2 login user
app.post("/login", async (request, response) => {
const { username, password } = request.body;
const checkUser = `select * from user where username='${username}';`;
const dbUserExist = await database.get(checkUser);
if (dbUserExist !== undefined) {
const checkPassword = await bcrypt.compare(password, dbUserExist.password);
if (checkPassword === true) {
const payload = { username: username };
const jwtToken = jwt.sign(payload, process.env.SECRET, {
expiresIn: "900000",
});
response
.cookie("access_token", jwtToken, {
httpOnly: true,
secure: true,
sameSite: "none",
expires: new Date(Date.now() + 900000),
})
.status(200)
.json({ username: username });
} else {
response.status(401);
response.send("Invalid password");
}
} else {
response.status(401);
response.send("Invalid user");
}
});
//authentication jwt token
const authenticationToken = (request, response, next) => {
const jwtToken = request.cookies.access_token;
if (!jwtToken) {
return response.status(401).send("Invalid JWT Token");
}
if (jwtToken !== undefined) {
jwt.verify(jwtToken, process.env.SECRET, async (error, payload) => {
if (error) {
response.status(401);
response.send("Invalid JWT Token");
} else {
request.username = payload.username;
next();
}
});
}
};
app.post("/generate", authenticationToken, async (request, response) => {
const imgUrl = request.body.imgUrl;
console.log(imgUrl);
// POST request to Replicate to start the image restoration generation process
let startResponse = await fetch("https://api.replicate.com/v1/predictions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.REPLICATE_API_KEY,
},
body: JSON.stringify({
version:
"0fbacf7afc6c144e5be9767cff80f25aff23e52b0708f17e20f9879b2f21516c",
input: { img: imgUrl, version: "v1.4", scale: 2 },
}),
});
console.log(JSON.stringify(startResponse))
let jsonStartResponse = await startResponse.json();
console.log("jsonStartResponse = ", jsonStartResponse);
if (!jsonStartResponse.urls) {
return response.status(401).json("Token Expired");
}
let endpointUrl = jsonStartResponse.urls.get;
// GET request to get the status of the image restoration process & return the result when it's ready
let restoredImage = null;
while (!restoredImage) {
// Loop in 1s intervals until the alt text is ready
console.log("polling for result...");
let finalResponse = await fetch(endpointUrl, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.REPLICATE_API_KEY,
},
});
let jsonFinalResponse = await finalResponse.json();
if (jsonFinalResponse.status === "succeeded") {
restoredImage = jsonFinalResponse.output;
} else if (jsonFinalResponse.status === "failed") {
response.status(400).json("Failed to restore image");
break;
} else {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
response
.status(200)
.json(restoredImage ? restoredImage : "Failed to restore image");
});