-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
397 lines (340 loc) · 10.9 KB
/
plugin.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* Plugin.js file, set configs, routes, hooks and events here
*
* see http://wejs.org/docs/we/plugin
*/
const fs = require('fs'),
path = require('path'),
request = require('request'),
cheerio = require('cheerio'),
uuid = require('uuid'),
gm = require('gm'),
mime = require('mime'),
rmdir = require('rimraf');
const urlService = require('./lib/urlService');
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
const we = plugin.we;
// set plugin configs
plugin.setConfigs({
wembed: {
refreshTime: 3600000,
filesPath: 'files/public/wembed/images/',
image: {
width: 200,
height: 200,
}
}
});
// ser plugin routes
plugin.setRoutes({
'get /api/v1/:wembedType(embed|json)': {
controller: 'wembed',
action: 'getEmbed',
model: 'wembed',
permission: true
}
});
plugin.getWembedSucessReponse = function getWembedSucessReponse(req, res) {
let wembedType = req.params.wembedType;
// force use of the cors *
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
if (wembedType === 'json') {
return res.send({ wembed: res.locals.data } );
} else {
let image;
if (res.locals.data.dataValues.images && res.locals.data.dataValues.images[0]) {
image = res.locals.data.dataValues.images[0].toJSON();
}
res.send(req.we.view.renderTemplate('wembed/embed', null, {
page: res.locals.data,
image: image,
locals: res.locals,
we: req.we
}));
}
}
/**
* get data and register one page
*/
plugin.registerPage = function registerPage(req, res, siteUrl, doneCallback) {
getPageHtml(siteUrl, function (err, $, domain) {
if (err) {
we.log.error('Error on getPageHtml:',err);
return res.notFound('Cant get this page');
}
let headTag = $('head');
let data = {};
data.title = plugin.getPageTitle(headTag);
data.description = plugin.getPageDescription($);
data.url = siteUrl;
data.cacheTime = new Date();
data.domain = domain;
data.images = [];
let pageUrlMetadata = urlService.getMetadataFromUrl(siteUrl);
data.provider = pageUrlMetadata.provider;
data.pageId = pageUrlMetadata.pageId;
data.pageType = pageUrlMetadata.type;
let pageRecord;
req.we.utils.async.series([
function createPage(done) {
// save the page on db
req.we.db.models.wembed
.create(data)
.then(function (page) {
pageRecord = page;
done();
})
.catch((err)=> {
console.log(err);
we.log.error('Error on create page:', {
error: err
});
done(err);
});
},
function createDir(done) {
// create one folder to this page
let p = path.resolve(we.config.wembed.filesPath, String(pageRecord.id));
we.utils.mkdirp(p, done);
},
function getPageImages(done) {
// download and save images on db
plugin.getPageImages($, pageRecord, function (err, imagesRecords) {
if (err) return done(err);
pageRecord.addImages(imagesRecords)
.then(function(){
imagesRecords.forEach(function (i) {
i.wembedId = pageRecord.id;
});
pageRecord.dataValues.images = imagesRecords;
done();
})
.catch((err)=> {
plugin.we.log.error('Error on save images:', {
error: err
});
done(err);
});
});
}
], function (err) {
doneCallback(err, pageRecord);
})
});
}
plugin.updatePage = function updatePage(req, res, pageRecord, doneCallback) {
const plugin = req.we.plugins['we-plugin-wembed-server'],
we = req.we;
let pageFolder = we.config.wembed.filesPath + pageRecord.id;
// empty the dir
plugin.emptyDir(pageFolder, function (err) {
if (err) return doneCallback(err);
getPageHtml(pageRecord.url, function (err, $) {
let headTag = $('head');
pageRecord.title = plugin.getPageTitle(headTag);
pageRecord.description = plugin.getPageDescription($);
pageRecord.cacheTime = new Date();
let pageUrlMetadata = urlService.getMetadataFromUrl(pageRecord.url);
pageRecord.provider = pageUrlMetadata.provider;
pageRecord.pageId = pageUrlMetadata.pageId;
pageRecord.pageType = pageUrlMetadata.type;
plugin.we.utils.async.series([
function removeOldImages(done) {
if (!pageRecord.images) return done();
pageRecord.removeImages( pageRecord.images )
.then(function(){
pageRecord.images = null;
done();
})
.catch(done);
},
function (done) {
// download and save new images
plugin.getPageImages($, pageRecord, function (err, imagesRecords) {
if (err) return done(err);
if (!imagesRecords) return done();
// get image ids to save in page
pageRecord.addImages(imagesRecords)
.then(function () {
imagesRecords.forEach(function (i) {
i.wembedId = pageRecord.id;
});
pageRecord.dataValues.images = imagesRecords;
done();
}).catch(done);
});
},
function (done) {
pageRecord.save()
.then(function() {
done(null);
})
.catch(done);
}
], function (err) {
doneCallback(err, pageRecord);
})
});
});
}
function getPageHtml(url, callback){
let options = getRequestOptions(url);
request(options, function (error, response, html) {
if(error){
return callback(error);
}
callback(null, cheerio.load(html), response.request.uri.host);
});
}
plugin.emptyDir = function emptyDir(dir, callback) {
rmdir(dir,function (err) {
if (err) return callback(err);
we.utils.mkdirp(dir, callback);
});
};
plugin.getPageTitle = function getPageTitle(headTag){
let title = headTag.find('meta[property="og:title"]').attr('content');
if (title) return title;
title = headTag.find('title').text();
return title;
}
plugin.getPageDescription = function getPageDescription($){
let description = $('meta[property="og:description"]').attr('content');
if (description) return description;
description = $('.site-slogan').text();
return description;
}
plugin.getPageImages = function getPageImages($, pageRecord, callback) {
const we = plugin.we;
let image = $('meta[property="og:image"]').attr('content');
let imagesSrc = [];
if (image) {
imagesSrc.push(image);
} else {
// TODO get only 3 images
$('img').each(function() {
let src = $(this).attr('src');
// check if has a valid image
if (/^http?:\/\//.test(src)) {
imagesSrc.push(src);
}
});
if(imagesSrc.length > 3 ){
imagesSrc = imagesSrc.splice(0 , 3);
}
}
let imgsPath = we.config.wembed.filesPath + pageRecord.id+'/';
let imageRecords = [];
// image order count
let orderNumber = 0;
// download every image file
we.utils.async.each(imagesSrc, function (src, nextImage) {
let imageFileName = uuid.v1();
downloadImage(
src,
path.resolve(plugin.we.projectPath, imgsPath ,imageFileName),
function (err, downloadedImage) {
if (err) {
we.log.error('Error on download image:',err);
return nextImage();
}
// set image data
downloadedImage.name = imageFileName;
downloadedImage.orderNumber = orderNumber;
orderNumber++;
// save image on db
we.db.models.wembedImage
.create(downloadedImage)
.then(function (imageRecord) {
imageRecords.push(imageRecord);
nextImage();
})
.catch(nextImage);
});
}, function (err) {
callback(err, imageRecords);
});
}
function downloadImage(uri, newFilePath, callback){
try {
request.head(uri, function(err, res){
if (err) {
plugin.we.log.error('Error on download image:',uri,err,res);
return callback(err);
}
let image = {};
image.mime = res.headers['content-type'];
image.size = res.headers['content-length'];
image.extension = mime.getExtension(image.mime);
image.originalFilename = uri;
newFilePath = newFilePath + '.' + mime.getExtension(image.mime);
let tempFilename = path.resolve(
plugin.we.projectPath,
'files/tmp/'+uuid.v1()
);
request(uri).pipe(fs.createWriteStream(tempFilename))
.on('close', function() {
resizeImage(tempFilename, newFilePath, image , function (err) {
if (err) return callback(err);
fs.unlink(tempFilename,function(err){
// return the callback
callback(err, image, newFilePath);
});
});
});
});
} catch (e) {
plugin.we.log.error('>> error on download image', e, uri, newFilePath);
callback();
}
}
/**
* Resize and return new image size and newPath
* @param {string} originalFile original file name
* @param {object} cords cords with cords.width and cords.heigth
* @param {Function} callback return callback(err, filepath)
*/
function resizeImage(originalFile, newFilePath, image, callback){
// resize and remove EXIF profile data
gm(originalFile)
.resize(
plugin.we.config.wembed.image.width,
plugin.we.config.wembed.image.height,
'^'
)
.gravity('Center')
.crop(
plugin.we.config.wembed.image.width,
plugin.we.config.wembed.image.height
)
.write(newFilePath, function (err) {
if (err) return callback(err);
// delete original file
callback(err, newFilePath);
});
}
plugin.addMethodIfDontHave = function addMethodIfDontHave(s){
// if user has not entered http:// https:// or ftp:// assume they mean http://
if(!/^(http|https):\/\//.test(s)) {
return 'http://'+s;
}else{
return s;
}
}
function getRequestOptions(url){
return {
url: url,
headers: {
// fake user agent
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0',
'Accept': '*/*',
'Content-Type': 'text/html',
'connection': 'Keep-Alive'
}
};
}
return plugin;
};