[nodebb-plugin-imgbed] Image Embedding with only a URL
-
I know I have access to this __default variable within the plugin, but that only gives me access to the root directory of the plugin. Is there a way to get to the actual uploads directory from a plugin?
-
@julian Thanks!
Now I'm seeing some weird behavior, I'm requiring the spawn child_process now, and calling curl, but when I load the forum (or when supervisor reloads it), it crashes 3-4 times before staying alive, and it's these kind of error messages:
NodeBB/src/webserver.js:376 var footer = templates.footer.parse({ ^ TypeError: Cannot call method 'parse' of undefined at module.exports.init.translator.translate.templates.logout (~/NodeBB/src/webserver.js:376:34) at Object.Plugins.fireHook (~/NodeBB/src/plugins.js:256:5) at module.exports.init (~/NodeBB/src/webserver.js:375:11) at Object.templates.ready (~/NodeBB/public/src/templates.js:47:5) at EventEmitter.nconf.file.file (~/NodeBB/app.js:124:18) at EventEmitter.g (events.js:185:14) at EventEmitter.emit (events.js:115:20) at Plugins.init (~/NodeBB/src/plugins.js:42:23) at Plugins.reload (~/NodeBB/src/plugins.js:89:5) at async.iterator.fn (~/NodeBB/node_modules/async/lib/async.js:579:34)
DEBUG: Program node app exited with code 1
-
Sure, here's the code snippet, from my library.js:
fs.exists(fullWgetPath, function(exist){ if (!exist){ console.log ("File not found for " + fullRelPath); // seeing weird behavior here, crashes a few times and then loads?? var file = fs.createWriteStream( fullWgetPath ); var curl = spawn('curl', [rawUrl]); curl.stdout.on('data', function(data) { file.write(data); }); curl.stdout.on('end', function(data) { file.end(); console.log("File downloaded! " + rawUrl); }); curl.on('exit', function(code) { if (code != 0) { console.log('Failed: ' + code); } }); // var wget = "wget " + rawUrl + " -O " + fullWgetPath; // var child = exec(wget, function (err, stdout, stderr){ // if (err){ // console.log(constants.name + ": Error wget-ing " + rawUrl); // console.log(err); // } // else { // console.log(constants.name + ": Successfully downloaded " + rawUrl + stdout); // } // }); } else { console.log("file exists: " + fullWgetPath); } });
-
I've made a few improvements to the plugin, including allowing custom extensions to be specified, and uploading to the server when an image is linked. I had some issues when in the downloading case: The post would be parsed, and the local url would be returned, but it was before the file was actually downloaded, so the image would show up as broken when you post an image. The way I resolved it was using the websockets, and signaling the client when the file was finished downloading.
I welcome any comments or concerns about the code, especially before I publish it to npm. I do still have a few more ideas for this plugin (progress bar, maybe making an option where the conversion to a local url can be made permanent, imgur uploading)... I remember that @psychobunny mentioned incorporating a CDN but that sounds like something for a separate plugin, to handle all other types of uploads.
-
Also, is there a global debug flag that you can reference on the server side?
-
Right, and the advantage that the markdown has is that the link doesn't need to have a specific format of matching the pattern of http*jpg|gif|png etc. I could change it to parse looking for img tags, and downloading those URLs
-
Oh this is cool, I never realized that you actually fetch the image and save a copy to your server. So potentially you could do something like GitHub does (ie. host all images on your own CDN), so you don't have to worry about serving non-secure images from random sites when you're on SSL
-
@psychobunny said:
...so you don't have to worry about serving non-secure images from random sites when you're on SSL
Yep, exactly. Well that and I didn't like that an image takedown would make my forum look crappy.
@Albus are you referring to the upload capability or the imgurl capability?