-
Notifications
You must be signed in to change notification settings - Fork 9
/
routes.js
159 lines (97 loc) · 2.92 KB
/
routes.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
var bodyParser = require('body-parser');
var fs = require("fs");
var multer = require('multer');
var path = require('path');
var mime = require('mime');
var magic = require('stream-mmmagic');
var request = require('request');
module.exports = function(app, express) {
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use("/", express.static(__dirname + '/public'));
app.set('root', __dirname + '/public');
var upload = multer({
dest: app.set('root') + '/uploads/'
});
app.get('/', function(req, res) {
res.sendFile(app.set('root') + '/index.html');
});
//request to get the images url from the server
app.get('/images/get', function(req, res) {
var uploadsDir = 'uploads';
fs.readdir(__dirname + '/public/uploads', function(err, files) {
for (var i = 0; i < files.length; ++i) {
files[i] = `${uploadsDir}/${files[i]}`;
}
// console.log("all files: ", files);
res.end(JSON.stringify({
filesUrl: files
}));
});
});
//run request to run the neural style program
app.post('/neural-style/run', function(req, res) {
});
//upload files
app.post('/upload', upload.array('uploads', 12), function(req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
console.log("post multiple /upload hit");
res.end("you have upload files");
});
app.post('/upload/url', function(req, res) {
console.log("/upload/url/ upload file by url hit");
console.log("req: ", req.body);
var file = req.body.imageUrl;
var dest = app.set("root") + "/uploads/";
var filename = path.basename(file);
var mimetype = mime.lookup(file);
var destFilePath = dest + filename;
console.log("filename: ", filename);
console.log("mimetype: ", mimetype);
console.log("dest+filename : ", dest + filename);
function isVaildImageType(type){
console.log("type: ",type);
if(type == "image/jpeg" ||
type == "image/png")
return true;
return false;
}
if(isVaildImageType(mimetype))
{
var validUrl = require('valid-url');
suspect = file;
if (validUrl.isUri(suspect)){
console.log('Looks like an URI');
var r = request(file);
r.on('response',function(resp) {
if(resp.statusCode === 200)
{
var uploadsDir = 'uploads';
var stream = r.pipe(fs.createWriteStream(destFilePath));
stream.on('finish',function(){
res.end(JSON.stringify({
uploadedImageUrl: `${uploadsDir}/${filename}`
}));
});
}
});
r.on('err',function(err) {
if(err) throw err;
});
} else {
console.log('Not a URI');
}
}
});
app.get('/*', function(req, res) {
return false;
});
}