aheckmann / gridform

Stream formidable uploads into MongoDB GridFS
http://aheckmann.github.com/gridform/
MIT License
59 stars 17 forks source link

How to append PNG files from GridForm using GraphicsMagic #7

Closed toddhickerson closed 11 years ago

toddhickerson commented 11 years ago

I've got lots of PNG files stored in mongo using GridForm and am trying to read several and use GM's append() command to combine them before sending them out. The append() command seems to be suited toward physical files on the disk not streams read from GridForm.

I noticed @aheckmann contributes to both repos, so I'm hoping you'll be able to give me a hint. Here are the guts of the code:

var Async = require('async');
var GridForm = require('gridform');
var Mongoose = require ('mongoose');
var GM = require('gm').subClass({ imageMagick: true });

....

var fileNames = ['file1.png', 'file2.png', 'file3.png'];
var gfs = GridForm.gridfsStream(Mongoose.connection.db, Mongoose.mongo);

gfs.files.find({ filename: { $in: fileNames } }).toArray(function (err, files) {

    var combinedFile = null;
    Async.eachSeries(
        files,
        function(file, callback) {
            var readstream = gfs.createReadStream(file._id);
            var newFile = null;
            readstream.pipe(newFile);
            if(combinedFile == null) {
                combinedFile = newFile;
            } else {
                GM(combinedFile).append(newFile);
            }
            console.log('combined file ' + file._id);
            callback(null, null);
        },
        function(err, results) {
            console.log("Sent all files");
        }
    );
});
aheckmann commented 11 years ago

Not sure if graphicsmagick / imagemagick support streams (docs don't specify). If you can get it working with the gm/convert binaries directly, please open a ticket in the gm repo with a working example and we'll work something out.