Node.js & GM library - node.js

I'm working on a Node.js application and I'm facing a critical problem right now which it is Resizing img after uploading .. the thing is everything goes right starting from uploading to getting the path of the uploaded img but I can't understand why this is happening .. here's my code and the error
var fileName = req.file.filename;
var filpath = "/uploads/" + fileName;
gm(filpath)
.resize(240, 240)
.write('/uploads/'+'resized-'+fileName, function (err) {
if (err) console.log(err);
else console.log("HOOLA");
});
The error is
{ [Error: Command failed: Invalid Parameter - /uploaded_file-1474832730810.png
] code: 4, signal: null }
The log
UPDATE:
first of all I would like to thank #r-a-lucas,and here's the full answer for Windows users:
npm install gm --save
download the latest ImageMagick from here ImageMagic EXE
Make sure it is excusable (advance system settings -> Advance -> Environment Variables -> add Path most likely in C:\Program Files\ImageMagick-*** )
In your JS file make sure to add (__dirname) to get the full path of the img. gm(__dirname+filpath)

It looks like you may need to install imagemagick for Windows. Check out this answer here: imagemagick with nodejs not working. Hopefully that helps. Best.

Related

node.js gm write command giving me "Error: spawn ENOTDIR"

I'm trying to create a little script to resize images. But what ever i do, i get the Error: spawn ENOTDIR
See the rwx file settings here:
I have striped the code to this. And the error still occurs:
const gm = require('gm');
gm('./test-photo.jpg').write('./output.jpg', (err) => {
console.log(err);
});
My package.json looks like this:
This is what i get:
node version:
I checked allot of examples and searched for others having the same problem. Could not find any, so i must be me forgetting something really silly.

nodeJS:fs.write callback and fs.writeFile not working

I knew nothing about fs until I was learning to use casperjs to scrape some content from a website and save them to a file. Following some examples on the web, I write this file scrape.js (The json data has been tested so it has nothing to do with the issue):
var fs = require('fs');
var url = "http://w.nycweb.io/index.php?option=com_k2&view=itemlist&id=4&Itemid=209&format=json";
var casper = require('casper').create();
casper.start(url,function(){
var json = JSON.parse(this.fetchText('pre'));
var jsonOfItems={},items = json.items;
items.forEach(function(item){
jsonOfItems[item.id] = item.introtext.split('\n');
})
fs.write('videoLinks.json',JSON.stringify(jsonOfItems),function(err){
if (err) console.log(err);
console.log('videoLinks.json saved');
})
});
casper.run();
When I do casperjs scrape.js in command line of my Ubuntu 14.04 server, I won't see the file saved message as expected, although the file is properly saved. So this is the first question: why the callback isn't running at all?
Secondly, I also tried fs.writeFile, but when I replace fs.write with it, the file isn't saved at all, nor is there any error information.
I do notice that in casper documentation it's said that casper is not a node.js module and some module of node.js won't be available, but I doubt it has anything to do with my issues. And I think it worths to mention that previously when I run this script I only get a respond like
I'm 'fs' module.
I had to follow this question to reinstall fs module globally to get it working.
fs.write expects a file descriptor where you are trying to give it a filename. Try fs.writeFile. https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback
Edit: Oh you tried that. Are you sure it didn't write it somewhere like the root directory? Tried a full path in there?
And what version of node are you running?

NodeJS: Set path to FFMPEG binaries for module Fluent-FFMPEG

I am building a application that uses the module node-fluent-ffmpeg. https://github.com/schaermu/node-fluent-ffmpeg
And I'm trying to package the ffmpeg binaries along with my application.
I want to do this so (especially on Windows) the user does not have to install FFMPEG manually.
Sadly everything I've tried results in errors. I've tried the following:
ffmpeg.setFfmpegPath : Gives an error saying setFfmpegPath is not a method
and:
proc.setFfmpegPath : Gives a createproces error.
It seems I'm doing something wrong. Could someone point out my mistake.
Thanks a lot.
I fix it!
I did not know I had to include the binary itself in the path. So I made something like this:
if(os.platform() === 'win32'){
var ffmpegPath = './bin/ffmpeg/ffmpeg.exe'
}else{
var ffmpegPath = './bin/ffmpeg/ffmpeg'
}
proc = new ffmpeg({ source: movieUrl, nolog: true, timeout: FFMPEG_TIMEOUT })
proc.setFfmpegPath(ffmpegPath)
proc.addOptions(opts)
proc.writeToStream(response, function(return_code, error){
In my case I have downloaded the npm i -S ffmpeg-binaries~ and after that I just set the process.env.FFMPEG_PATH to './node_modules/ffmpeg-binaries/bin/ffmpeg.exe'. This worked for me.

nodejitsu 400 Error: ENOENT, open '/opt/run/snapshot/package/images/tmp/72118-89rld0.png

I'm using nodejitsu to deploy a simple image upload program with express. In my code I've changed the default upload directory by following command
app.use(express.bodyParser({
uploadDir: __dirname + "/images/tmp"
}));
It's working fine on my localhost but when I'm using nodejitsu I'm getting this error
400 Error: ENOENT, open '/opt/run/snapshot/package/images/tmp/72118-89rld0.png.
Can anybody tell me how to make it work on nodejitsu as well? Actually I'm new to node as well as nodejitsu.
I had the same problem. Try to check directory at application start:
var fs = require('fs'),
upload = __dirname + "/images/tmp";
fs.exists(upload, function (exist) {
if (!exist) {
fs.mkdir(upload);
}
});
It was helpful for me, may be it would helpful for you.
make sure that directory /opt/run/snapshot/package/images/tmp/ exists. Otherways just mkdir those directory
At first check that the directory exist or not. If not then create it and follow the command
sudo jitsu deploy
I think the problem will be solved.

How to upload file using easy-ftp in node?

I am trying to upload a file into my hosting server using node and easy-ftp.
I try with the following code:
var EasyFtp = require ("easy-ftp");
var ftp = new EasyFtp();
var config = {
host:'homexxxxx.1and1-data.host',
type:'SFTP',
port:'22',
username:'u90xxxx',
password:"mypass"
};
ftp.connect(config);
ftp.upload("/test/test.txt", "/test.txt", function(err){
if (err) throw err;
ftp.close();
});
No error message but no file uploaded
I tried the same using promises
const EasyFTP = require('easy-ftp-extra')
const ftp = new EasyFTP()
const config = {
host:'homexxxxx.1and1-data.host',
type:'SFTP',
port:'22',
username:'u90xxxx',
password:"mypass"
};
ftp.connect(config);
ftp.upload('/test.txt', '/test.txt')
.then(console.log)
.catch(console.error)
ftp.upload()
The same issued. No file is uploaded. No error in node console.
The config is the same used in filezilla to transfer files. SFTP protocol. Everything working well with filezilla.
What I am doing wrong?
Looks like you may have a path problem over here.
"/test/test.txt"
The path specified will try to take file from root folder like "c:\test\test.txt".
Assuming you want the file to be taken from your project folder try this path:
"./test/test.txt"
Other things in your code are precisely the same as in mine and mine works.
For me, it was just silently failing, and intelli-sense was not available.
npm remove easy-ftp
npm install easy-ftp
npm audit fix --force until no more vulnerabilities
Afterwards, intelli-sense was available and it started working.

Resources