db.collection('images.files').find().forEach(function (file) {
       
            var bucket = 'images';
            
            gfs.remove({filename: 'thumb_'+file.filename, root: bucket}, function (err) {
              if (err) return handleError(err);
              console.log('success');
            });

            var rs = gfs.createReadStream({
               _id: file._id,
               root: bucket,
               content_type: file.contextType
            });
            
            var ws = gfs.createWriteStream({
                filename: 'thumb_'+file.filename,
                root: bucket,
                mode: 'w',
                content_type:  file.contentType
            });

            var resize = im().resize('1024x').quality(80);
            rs.pipe(resize).pipe(ws);

      });
Beispiel #2
0
exports.upload = function(request, reply) {
  var data = request.payload;
  if (data.file) {
    var name = data.file.hapi.filename;
    var ext = Path.extname(name);
    var thumb = Math.random().toString(36).slice(2);
    var path = Config.uploadsPath + '/' + thumb  + ext;

    var file = Fs.createWriteStream(path);
    file.on('error', function (err) { 
      return reply(Boom.badImplementation(err));
    });
    var resize = Im().resize('300x300').gravity('center').crop('150x150+0+0').quality(90);
    var out = data.file.pipe(resize).pipe(file);
    out.on('error', function (err) { 
      return reply(Boom.badImplementation(err));
    });
    out.on('finish', function () { 
      var res = {
        filename: data.file.hapi.filename,
        thumb: thumb + ext,
        headers: data.file.hapi.headers
      };
      reply(res);
    });
  }
};
Beispiel #3
0
 backend.get(image, (err,  stream) => {
   if (err) console.log(err);
   if (!stream) return res.status(404).send({error: 'File Not Found'});
   // res.setHeader('Expires', new Date(Date.now() + 604800000));
   const resize = im().resize('750x450\>').quality(50);
   stream.pipe(crypto.createDecipher(algorithm, secret.secret)).pipe(resize).pipe(res)
 });
    form.on('part', part => {
      if (part.filename) {
        const filenameLarge = `${uuid.v1()}.jpg`;
        const resizeLarge = im()
          .set('background', '#ffffff')
          .set('alpha', 'remove')
          .resize('2048x2048')
          .quality(90)
          .autoOrient()
          .outputFormat('jpg');

        part.on('error', utils.safeReject(reject));
        bucket
          .writeImage(
            part.pipe(resizeLarge),
            'images/',
            filenameLarge.replace(/-/gu, '')
          )
          .then(image => {
            resolve({
              imageUrl: image,
            });
          })
          .catch(reject);
      }
    });
Beispiel #5
0
gallery.map(function (photo) {
  if (!photo.thumb) {
    console.log('Fetching thumbnail for ', photo.source)
    var fetchStream = superagent.get(photo.source)
    var id = shortid.generate()
    var thumbnailPath = 'images/gallery/' + id + '.jpg'
    var writeStream = fs.createWriteStream(thumbnailPath)
    var resize = im().resize('500x500').crop('350x350').quality(90)
    fetchStream.pipe(resize).pipe(writeStream)
    photo.thumb = thumbnailPath
  }
  return photo
})
Beispiel #6
0
      return _thumbnail(staticFilesPath, pathFromReq, function(err, thumb){
        if (err){
          return common.error(req, res, next, 404, 'No thumbnail found for this album', err);
        }

        var fstream = fs.createReadStream(path.join(thumb));

        var cachedResizedKey, cacheWriteStream, cachedResult,
        resizer, dimensions, w, h;
          w = (config.thumbnail && config.thumbnail.width) || 200;
          h = (config.thumbnail && config.thumbnail.height) || 200;
          dimensions = w + 'x' + h;

        
        cachedResizedKey = path.join(thumb) + dimensions;
        cachedResizedKey = crypto.createHash('md5').update(cachedResizedKey).digest('hex');
        
        // Check the cache for a previously rezized tn of matching file path and dimensions
        cachedResult = cache.get(cachedResizedKey)
        // TODO - eventualyl should just try the fs.read on cachedResult, existsSync is a bad hack
        if (cachedResult && fs.existsSync(cachedResult)){
          // cache hit - read & return
          var cacheReadStream = fs.createReadStream(cachedResult);
          cacheReadStream.on('error', function(){
            return common.error(req, res, next, 404, 'File not found', err);
          });
          return cacheReadStream.pipe(res);  
        }
        
        // No result, create a write stream so we don't have to reize this image again
        cacheWritePath = path.join('/tmp', cachedResizedKey);
        cacheWriteStream = fs.createWriteStream(cacheWritePath);
        
        
        
        resizer = im().resize(dimensions).quality(40);
        resizer.on('error', function(err){
          return common.error(req, res, next, 500, 'Error in IM/GM converting file', err);
        });
        
        var resizestream = fstream.pipe(resizer);
        
        // Pipe to our tmp cache file, so we can use this in future
        resizestream.pipe(cacheWriteStream);
        cache.put(cachedResizedKey, cacheWritePath);
        
        // Also stream the resized result back to the requestee
        return resizestream.pipe(res);       

      });
Beispiel #7
0
    request(url).pipe(concat(function(res) {
        var $ = cheerio.load(res.toString());
        var trs = $('table#metadataTable tr');
        var time = 0;
        var pos = [];

        trs.each(function() {
            var td = $(this).find('td');
            if ($(td[0]).text() == 'Start Time') {
                time = moment($(td[1]).text(), 'YYYY:DDD:HH:MM:').unix();
            }
            if ($(td[0]).text() == 'NW Corner Long dec') {
                pos[0] = +($(td[1]).text());
            }
            if ($(td[0]).text() == 'NW Corner Lat dec') {
                pos[1] = +($(td[1]).text());
            }
            if ($(td[0]).text() == 'SE Corner Long dec') {
                pos[2] = +($(td[1]).text());
            }
            if ($(td[0]).text() == 'SE Corner Lat dec') {
                pos[3] = +($(td[1]).text());
            }
        });

        var src = $('img#browse1').attr('src');

        if (pos.length == 4 && time && src) {
            try {
                var r = request(src);
                r.pipe(im().options({
                    transparent: 'black',
                    fuzz: '20%'
                })
                .resize('200x200'))
                    .pipe(fs.createWriteStream('samples/' + time + ',' + pos.join(',') + ',' + id + '.png'))
                r.on('end', callback);
            } catch(e) {
                callback();
            }
        } else {
            callback();
        }
    }));
    modelGridForm(model).parse(req, function (err, fields, files) {
      if (err) {
        res.send(500);
      } else {
        var myFile = files.files;
        var id = myFile.id.toString();

        // Options for creating the thumbnail
        var options = {
          mode: 'w', content_type: myFile.type,
          filename: 'thumbnail_' + myFile.path,
          root: myFile.root,
          metadata: { original_id: id } // original id is needed to remove thumbnail afterwards
        };

        var mongo = dataform.mongoose.mongo;
        var resize = ims().resize('100x100').quality(90);
        var resource = dataform.getResource(model);
        var gfs = Grid(resource.model.db.db, mongo);

        var BSON = mongo.BSONPure;
        var o_id = new BSON.ObjectID(myFile.id);

        var readstream = gfs.createReadStream({_id: id, root: myFile.root, fsync: true});
        var writestream = gfs.createWriteStream(options);

        // Use 'close' because of stream spec v1.0 (not finish)
        writestream.on('close', function(file){
          res.send({files:[{
            name: myFile.name,
            size: myFile.size,
            url: '/file/' + model + '/' + id,
            thumbnailUrl: '/file/' + model + '/thumbnail/' + file._id,
            deleteUrl: '/file/' + model + '/' + id,
            deleteType: "DELETE"
          }]});
        });

        // Apply resize transformation as it passes through
        readstream.pipe(resize).pipe(writestream);

      }
    });
Beispiel #9
0
function makeThumbStream(localFile) {
  var passthrough = new stream.PassThrough();
  im(localFile).op('gravity', 'center').op('thumbnail', '500x500^').op('extent', '500x500').op('format', 'png').pipe(passthrough);
  return passthrough;
}
Beispiel #10
0
function flipImageStream(localFile) {
  var passthrough = new stream.PassThrough();
  im(localFile).op('flop').pipe(passthrough);
  return passthrough;
}
Beispiel #11
0
 fs.stat(filePath, function(err){
   if (err){
     return common.error(req, res, next, 404, 'File not found', err);
   }
   fstream = fs.createReadStream(filePath);
   fstream.on('error', function(err){
     return common.error(req, res, next, 404, 'File not found', err);
   });
   
   if (!req.query.tn){
     // return the full size file
     return fstream.pipe(res);
   }else{
     // streaming resize our file
     var cachedResizedKey, cacheWriteStream, cachedResult,
     resizer, dimensions, w, h;
     if (req.query.tn.toString() === '1'){
       w = (config.thumbnail && config.thumbnail.width) || 200;
       h = (config.thumbnail && config.thumbnail.height) || 200;
       dimensions = w + 'x' + h;
     }else {
       //w = (config.image && config.image.width) || '100%';
       //h = (config.image && config.image.height) || '100%';
       //dimensions = w + 'x' + h;
       // return the full size file
       return fstream.pipe(res);
     }
     
     cachedResizedKey = filePath + dimensions;
     cachedResizedKey = crypto.createHash('md5').update(cachedResizedKey).digest('hex');
     
     // Check the cache for a previously rezized tn of matching file path and dimensions
     cachedResult = cache.get(cachedResizedKey)
     // TODO - eventualyl should just try the fs.read on cachedResult, existsSync is a bad hack
     if (cachedResult && fs.existsSync(cachedResult)){
       // cache hit - read & return
       var cacheReadStream = fs.createReadStream(cachedResult);
       cacheReadStream.on('error', function(){
         return common.error(req, res, next, 404, 'File not found', err);
       });
       return cacheReadStream.pipe(res);  
     }
     
     // No result, create a write stream so we don't have to reize this image again
     cacheWritePath = path.join('/tmp', cachedResizedKey);
     cacheWriteStream = fs.createWriteStream(cacheWritePath);
     
     
     
     resizer = im().resize(dimensions).quality(40);
     resizer.on('error', function(err){
       return common.error(req, res, next, 500, 'Error in IM/GM converting file', err);
     });
     
     var resizestream = fstream.pipe(resizer);
     
     // Pipe to our tmp cache file, so we can use this in future
     resizestream.pipe(cacheWriteStream);
     cache.put(cachedResizedKey, cacheWritePath);
     
     // Also stream the resized result back to the requestee
     return resizestream.pipe(res);
   }
 });