forked from julianlam/nodebb-plugin-audio-embed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
128 lines (106 loc) · 4.17 KB
/
library.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
"use strict";
/* globals require, module */
var path = require('path'),
fs = require('fs'),
mkdirp = require('mkdirp'),
mv = require('mv'),
async = require('async'),
nconf = require.main.require('nconf'),
{resolve} = require('path'),
shell = require('any-shell-escape'),
{exec} = require('child_process'),
pathToFfmpeg = require('ffmpeg-static');
var db = require.main.require('./src/database');
var controllers = require('./lib/controllers');
var plugin = {
embedRegex: /\[video\/([\w\-_.]+)\]/g
},
app;
plugin.init = function(params, callback) {
var router = params.router,
hostMiddleware = params.middleware,
multiparty = require.main.require('connect-multiparty')();
app = params.app;
router.get('/admin/plugins/video-embed', hostMiddleware.admin.buildHeader, controllers.renderAdminPage);
router.get('/api/admin/plugins/video-embed', controllers.renderAdminPage);
router.post('/plugins/nodebb-plugin-video-embed/upload', multiparty, hostMiddleware.validateFiles, hostMiddleware.applyCSRF, controllers.upload);
mkdirp(path.join(nconf.get('upload_path'), 'video-embed'), callback);
};
plugin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/plugins/video-embed',
icon: 'fa-play',
name: 'Video Embed'
});
callback(null, header);
};
plugin.registerFormatting = function(payload, callback) {
payload.options.push({ name: 'video-embed', className: 'fa fa-video' });
callback(null, payload);
};
//todo check if classname needs to be changed
plugin.processUpload = function(payload, callback) {
if (payload.type.startsWith('video/')) {
var id = path.basename(payload.path),
uploadPath = path.join(nconf.get('upload_path'), 'video-embed', id);
async.waterfall([
async.apply(shell,[pathToFfmpeg,'-i',payload.path,'-c:v','libvpx-vp9','-r','24','s','480x270','b:v','128k','-threads','2','-speed','1','tile-columns','6',
'frame-parallel','1','-auto-alt-ref','1','-lag-in-frames','12','-c:a','libopus','-b:a','12k','-f','webm',uploadPath.substr(0, uploadPath.lastIndexOf(".")) + ".webm"
]),
//async.apply(mv, payload.path, uploadPath), delete of original
async.apply(db.setObject, 'video-embed:id:' + id, {
name: payload.name.substr(0, payload.name.lastIndexOf(".")) + ".webm",
size: /*payload.size*/fs.statSync(uploadPath.substr(0, uploadPath.lastIndexOf(".")) + ".webm").size
}),
async.apply(db.sortedSetAdd, 'video-embed:date', +new Date(), id)
], function(err) {
if (err) {
return callback(err);
}
callback(null, {
id: id
});
});
} else {
callback(new Error('invalid-file-type'));
}
};
plugin.parsePost = function(data, callback) {
if (!data || !data.postData || !data.postData.content) {
return callback(null, data);
}
plugin.parseRaw(data.postData.content, function(err, content) {
if (err) {
return callback(err);
}
data.postData.content = content;
callback(null, data);
});
};
plugin.parseRaw = function(content, callback) {
var matches = content.match(plugin.embedRegex);
if (!matches) {
return callback(null, content);
}
// Filter out duplicates
matches = matches.filter(function(match, idx) {
return idx === matches.indexOf(match);
}).map(function(match) {
return match.slice(7, -1);
});
async.filter(matches, plugin.exists, function(err, ids) {
async.reduce(ids, content, function(content, id, next) {
app.render('partials/video-embed', {
id: id,
path: path.join(nconf.get('relative_path'), '/uploads/video-embed', id)
}, function(err, html) {
content = content.replace('[video/' + id + ']', html);
next(err, content);
});
}, callback);
});
};
plugin.exists = function(id, callback) {
db.isSortedSetMember('video-embed:date', id, callback);
};
module.exports = plugin;