I'm trying to upload a file and convert it with fontforge, to send it back to user, all these with formidable. My code worked on my first machine, but then I created an Ubuntu-Server VM with Vagrant, SSHed on it, DLed fontforge, formidable (and nodejs basically), and retried. That's what my server.js file looks like:
var formidable = require('formidable'),
http = require('http'),
exec = require("child_process").exec,
fs = require("fs");
http.createServer(function(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
console.log(files);
console.log(fields);
exec("fontforge -script convert.sh -format \"." + fields.format + "\"" + " " + files.upload.path, function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
res.setHeader('Content-disposition', 'attachment; filename=' + files.upload.name.replace('svg', fields.format));
res.writeHead(200, {'Content-Type': 'application/x-font-' + fields.format});
res.end(files.upload.path + "." + fields.format);
fs.unlink(files.upload.path, function (err) {
if (err) throw err;
console.log('successfully deleted ' + files.upload.path);
});
fs.unlink(files.upload.path + "." + fields.format, function (err) {
if (err) throw err;
console.log('successfully deleted ' + files.upload.path + "." + fields.format);
});
});
});
return;
}).listen(8080);
The form appears correctly on the client (some basic index.html page with a post method), but sends me back a 31 bytes file (instead of 6.1k), with the right name and extension, but absolutely not the right content.
The console log displays:
{ upload:
{ domain: null,
_events: {},
_maxListeners: 10,
size: 29526,
path: '/tmp/bcb357fa7f8e5fcc075964c6bcbbe9bb',
name: 'hamburg.svg',
type: 'image/svg+xml',
hash: null,
lastModifiedDate: Mon Jul 21 2014 11:04:00 GMT+0000 (UTC),
_writeStream:
{ _writableState: [Object],
writable: true,
domain: null,
_events: {},
_maxListeners: 10,
path: '/tmp/bcb357fa7f8e5fcc075964c6bcbbe9bb',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
pos: undefined,
bytesWritten: 29526,
closed: true } } }
{ format: 'ttf' }
Your problem was that you were sending the path to the converted file. To send the file itself, you need to use:
fs.readFile(files.upload.path + '.' + fields.format, function (err, data) {
if (err) {
res.end('Error: ' + err);
} else {
res.end(data);
}
});