-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
210 lines (172 loc) · 6.03 KB
/
index.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
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
// works for NODE > v10
const pug = require('pug');
const path = require('path');
require('dotenv').config()
const fs = require("fs");
const axios = require('axios');
var FormData = require('form-data');
//formidable takes the form data and saves the file, and parameterises the fields into JSON
const formidable = require('formidable')
const express = require('express');
const app = express();
var plateToken = process.env.plateToken;
var DVLAToken = process.env.DVLAToken;
//base64 conversion
var atob = require('atob');
// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
//set up pug as view engine
app.set('view engine','pug');
// https://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
return response.render('index');
});
app.get("/camera", (request, response) => {
response.sendFile(path.join(__dirname, '/public', 'camera.html'));
});
app.get("/MOT", (request, response) => {
response.redirect('/camera.html?response=mot');
});
app.get("/tax", (request, response) => {
response.redirect('/camera.html?response=tax');
});
app.post("/", (request, response) => {
//this sends in a form with an image
//formidable reads the form, saves the image
var form = new formidable.IncomingForm({maxFileSize : 2000 * 1024 * 1024}); //2 Gb
form.parse(request, (err, fields, files) => {
if (err) {
console.error('Error', err);
throw err;
}
var responseType = fields.responseType;
console.log("responsetype", responseType);
//console.log(fields);
//PARSED FORM
var newImagePath;
var base64Image = false;
//if from camera - the image comes in as base64
if(fields.file){
base64Image = true;
//base64 immage from camera app
console.log("fields", fields.file);
// Remove header
let base64String= fields.file;
let base64ImageFile = base64String.split(';base64,').pop();
//console.log(base64ImageFile);
fs.writeFile('image.png', base64ImageFile, {encoding: 'base64'}, function(err) {
console.log('File created');
});
newImagePath = 'image.png';
console.log("newImagePath",newImagePath);
makeRequest(newImagePath);
} else{
//uploaded image
console.log("file");
console.log("files data", JSON.stringify(files.imageSource));
var imageName = path.parse(files.imageSource.name).name;
var imagePath = files.imageSource.path;
var imageType = files.imageSource.type;
var imageSize = files.imageSource.size;
//FORMIDIABLE USES A RANDOM NAME ON UPLOAD. RENAME
newImagePath = imagePath+ imageName;
fs.rename(imagePath, newImagePath, function (err) {
if (err) throw err;
console.log('File uploaded and moved!');
//FILE IS RENAMED
//NOW UPLOAD IT TO MINDEE WITH THE MAKEREQUEST FUNCTION
makeRequest(newImagePath);
});
}
async function makeRequest(newImagePath) {
console.log("newImagePath",newImagePath);
let data = new FormData();
if(base64Image){
//add base64 image
data.append('file', fs.createReadStream(newImagePath));
} else {
//add the image that was uploaded
data.append('file', fs.createReadStream(newImagePath));
}
// console.log("form data ", data);
const config = {
method: 'POST',
url: 'https://api.mindee.net/products/license_plates/v1/predict',
headers: {
'X-Inferuser-Token':plateToken,
...data.getHeaders()
},
data
}
// console.log("config" ,config);
try {
let apiResponse = await axios(config)
console.log(" api response", apiResponse.data);
//pull out the data I want to show on the page
var predict = apiResponse.data.predictions[0];
console.log("predict",predict);
var plate = apiResponse.data.predictions[0].license_plates[0].value;
console.log("plate", plate);
//NOW WE HAVE THE PLATE NUMBER,
// CALL DVLA TO FIND OUT ABOUT THE CAR
data = {"registrationNumber": plate};
const DVLAconfig = {
method: 'POST',
url: 'https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles',
headers: {
'x-api-key':DVLAToken,
'Content-Type': 'application/json'
},
data
}
// console.log("DVLAconfig" ,DVLAconfig);
try {
let DVLAapiResponse = await axios(DVLAconfig)
console.log("DVLA response", DVLAapiResponse.data);
var motStatus = DVLAapiResponse.data.motStatus;
var motExpire = DVLAapiResponse.data.motExpiryDate;
var fuelType = DVLAapiResponse.data.fuelType;
var colour = DVLAapiResponse.data.colour;
var make = DVLAapiResponse.data.make;
var taxStatus = DVLAapiResponse.data.taxStatus;
var year = DVLAapiResponse.data.yearOfManufacture;
var taxDueDate = DVLAapiResponse.data.taxDueDate;
if(responseType){
console.log(responseType);
//there could be a bunch of different responses
if(responseType === "mot"){
return response.render('mot',{plate, motStatus, motExpire,fuelType, colour, make, taxStatus, year});
}
else if (responseType ==="tax"){
return response.render('tax',{plate,taxDueDate, motStatus, motExpire,fuelType, colour, make, taxStatus, year});
}
}else{
return response.render('receipt',{plate, motStatus, motExpire,fuelType, colour, make, taxStatus, year});
}
} catch (error) {
console.log("DVLA error");
console.log(error);
return response.render('error',{plate});
}
} catch (error) {
console.log(error)
}
}
//makeRequest()
});
});
// listen for requests :)
const listener = app.listen(3003, () => {
console.log("Your app is listening on port " + listener.address().port);
});
function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
}