Meteor-files: image processing : unable to open file

Created on 23 Aug 2017  路  9Comments  路  Source: veliovgroup/Meteor-Files

W20170823-18:26:18.635(5.5)? (STDERR) [createThumbnails] [img.resize] { [Error: Command failed: gm convert: Unable to open file (assets\app\uploads\images\YTWpDndzksMdtKK6Q.jpg) [No such file or directory]. W20170823-18:26:18.636(5.5)? (STDERR) ] code: 1, signal: null } I20170823-18:26:18.637(5.5)? file W20170823-18:26:18.637(5.5)? (STDERR) { [Error: Command failed: gm convert: Unable to open file (assets\app\uploads\images\YTWpDndzksMdtKK6Q.jpg) [No such file or directory]. W20170823-18:26:18.638(5.5)? (STDERR) ] code: 1, signal: null }

this is the error i am getting with gm , i m using gridfs for file storage

question

All 9 comments

Well, file is obviously is moved to GridFS, and not exists on your FS.
You should run gm over the file before moving it to GridFS

i am trying that ,and how do i add new thumbnail to gridfs
for image processing

import { check }  from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import fs from 'fs-extra';
import gm from 'gm';
const bound = Meteor.bindEnvironment((callback) => {
  return callback();
});
const createThumbnails = (collection, fileRef, cb) => {
  check(fileRef, Object);
  fs.exists(fileRef.path, (exists) => {
    bound(() => {
      if (!exists) {
        throw Meteor.log.error('File ' + fileRef.path + ' not found in [createThumbnails] Method');
      }
      const image = gm(fileRef.path);
      console.log(image);
      image.size((error, features) => {
        bound(() => {
          if (error) {
            console.error('[_app.createThumbnails] [_.each sizes]', error);
            cb && cb(Meteor.Error('[_app.createThumbnails] [image.size]', error));
            return;
          }
          // Update meta data if original image
          collection.collection.update(fileRef._id, {
            $set: {
              'meta.width': features.width,
              'meta.height': features.height,
              'versions.original.meta.width': features.width,
              'versions.original.meta.height': features.height
            }
          });
          const path = (collection.storagePath(fileRef)) + '/thumbnail-' + fileRef._id + '.' + fileRef.extension;
          const img = gm(fileRef.path)
            .quality(70)
            .define('filter:support=2')
            .define('jpeg:fancy-upsampling=false')
            .define('jpeg:fancy-upsampling=off')
            .define('png:compression-filter=5')
            .define('png:compression-level=9')
            .define('png:compression-strategy=1')
            .define('png:exclude-chunk=all')
            .autoOrient()
            .noProfile()
            .strip()
            .dither(false)
            .interlace('Line')
            .filter('Triangle');
          // Change width and height proportionally
          img.resize(250).write(path, (resizeError) => {
            bound(() => {
              if (resizeError) {
                console.error('[createThumbnails] [img.resize]', resizeError);
                cb && cb(resizeError);
                return;
              }
              fs.stat(path, (fsStatError, stat) => {
                bound(() => {
                  if (fsStatError) {
                    console.error('[_app.createThumbnails] [img.resize] [fs.stat]', fsStatError);
                    cb && cb(fsStatError);
                    return;
                  }
                  gm(path).size((gmSizeError, imgInfo) => {
                    bound(() => {
                      if (gmSizeError) {
                        console.error('[_app.createThumbnails] [_.each sizes] [img.resize] [fs.stat] [gm(path).size]', gmSizeError);
                        cb && cb(gmSizeError);
                        return;
                      }
                      fileRef.versions.thumbnail = {
                        path: path,
                        size: stat.size,
                        type: fileRef.type,
                        extension: fileRef.extension,
                        meta: {
                          width: imgInfo.width,
                          height: imgInfo.height
                        }
                      };
                      const upd = { $set: {} };
                      upd['$set']['versions.thumbnail'] = fileRef.versions.thumbnail;
                      collection.collection.update(fileRef._id, upd, (colUpdError) => {
                        if (cb) {
                          if (colUpdError) {
                            cb(colUpdError);
                          } else {
                            cb(void 0, fileRef);
                          }
                        }
                      });
                    });
                  });
                });
              });
            });
          });
        });
      });
    });
  });
  return true;
};
export default createThumbnails; 

and

Imagesgs.Imagesgs.on('afterUpload', function(fileRef) {
  if (/png|jpe?g/i.test(fileRef.extension || '')) {
       createThumbnails(this, fileRef, (error, fileRef) => {
      if (error) {
        console.error(error);
        console.log("file");
      }
    });
}

Object.keys(fileRef.versions).forEach(versionName => {
    const metadata = { versionName, imageId: fileRef._id, storedAt: new Date() }; // Optional
    const writeStream = gfs.createWriteStream({ filename: fileRef.name, metadata });
    fs.createReadStream(fileRef.versions[versionName].path).pipe(writeStream);
    writeStream.on('close', Meteor.bindEnvironment(file => {
      const property = versions.${versionName}.meta.gridFsFileId;
      this.collection.update(fileRef._id, { $set: { [property]: file._id.toString() } });
      this.unlink(this.collection.findOne(fileRef._id), versionName); // Unlink files from FS
    }));
   });

thumbnail problem resolved , using future ,
just help me uploading this newly created thumb to gridfs

thumbnail problem resolved

What error you're getting now?

<img src="{{imageFile.link 'thumbnail'}}" alt="{{imageFile.name}}" />
returning original image not thumbnail

You may have run into the "feature" described here.

this is my image collection , i have both original and thumbnail versions
"_id" : "eL5nR6Mca9Bd9Yrpd", "size" : 59996, "type" : "image/jpeg", "name" : "21.jpg", "meta" : { "restaurantId" : "Hg3Mp6uJWzQ4wDEZE", "type" : "restCoverImage" }, "ext" : "jpg", "extension" : "jpg", "extensionWithDot" : ".jpg", "mime" : "image/jpeg", "mime-type" : "image/jpeg", "userId" : "i7CvP3R9idLPwFJec", "path" : "assets\\app\\uploads\\images\\eL5nR6Mca9Bd9Yrpd.jpg", "versions" : { "original" : { "path" : "assets\\app\\uploads\\images\\eL5nR6Mca9Bd9Yrpd.jpg", "size" : 59996, "type" : "image/jpeg", "extension" : "jpg", "meta" : { "gridFsFileId" : "599ea70516454dcd2cb49f7e" } }, "thumbnail" : { "path" : "assets\\app\\uploads\\images/thumbnail-eL5nR6Mca9Bd9Yrpd.jpg", "type" : "image/jpeg", "extension" : "jpg", "meta" : { "gridFsFileId" : "599ea70516454dcd2cb49f7f" } } }, "_downloadRoute" : "/cdn/storage", "_collectionName" : "images", "isVideo" : false, "isAudio" : false, "isImage" : true, "isText" : false, "isJSON" : false, "isPDF" : false, "_storagePath" : "assets\\app\\uploads\\images", "public" : false

this is fs.file collection
{ "_id" : ObjectId("599ea70516454dcd2cb49f7f"), "filename" : "21.jpg", "contentType" : "binary/octet-stream", "length" : 19685, "chunkSize" : 261120, "uploadDate" : ISODate("2017-08-24T10:14:29.378Z"), "aliases" : null, "metadata" : { "versionName" : "thumbnail", "imageId" : "eL5nR6Mca9Bd9Yrpd", "storedAt" : ISODate("2017-08-24T10:14:29.324Z") }, "md5" : "c31708a14177b7ff13a3f2b1a53d52df" } { "_id" : ObjectId("599ea70516454dcd2cb49f7e"), "filename" : "21.jpg", "contentType" : "binary/octet-stream", "length" : 59996, "chunkSize" : 261120, "uploadDate" : ISODate("2017-08-24T10:14:29.396Z"), "aliases" : null, "metadata" : { "versionName" : "original", "imageId" : "eL5nR6Mca9Bd9Yrpd", "storedAt" : ISODate("2017-08-24T10:14:29.322Z") }, "md5" : "f213e5d067025bd41cae3fcefa6a8189" }

still image file link
<img src="{{imageFile.link 'thumbnail'}}" alt="{{imageFile.name}}" />

giving me original image not thumbnail

<img src="{{imageFile.link }}" alt="{{imageFile.name}}" />
file not found

sorry , interceptDownload called from server making issue i just placed it in common folder , now its working , i just need to refresh page some time for thumbnails

Thanks for your precious time ,

@aayushsinghm16 I'm glad it's solved.

Please, support this project by:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rlhk picture rlhk  路  4Comments

sabedinia picture sabedinia  路  3Comments

tuarrep picture tuarrep  路  4Comments

RE-N-Y picture RE-N-Y  路  3Comments

stefanve picture stefanve  路  4Comments