jalik/meteor-jalik-ufs

Uploaded images have size zero

Opened this issue · 2 comments

I have an issue uploading images with GridFS, here is my code

import { MongoObservable } from 'meteor-rxjs';
import { UploadFS } from 'meteor/jalik:ufs';
import { Meteor } from 'meteor/meteor';
import gm from 'gm';
import { Picture, DEFAULT_PICTURE_URL } from '../models';

export interface PicturesCollection<T> extends MongoObservable.Collection<T> {
    getPictureUrl(selector?: Object | string): string;
}

export const Pictures =
    new MongoObservable.Collection<Picture>('pictures') as PicturesCollection<Picture>;

export const PicturesStore = new UploadFS.store.GridFS({
    collection: Pictures.collection,
    name: 'pictures',
    chunkSize: 1024*255,
    filter: new UploadFS.Filter({
      minSize: 1,
      maxSize: 1024 * 10000, // 10MB,
      contentTypes: ['image/*'],
      extensions: ['jpg', 'png', 'gif']
    }),
    permissions: new UploadFS.StorePermissions({
        insert: picturesPermissions,
        update: picturesPermissions,
        remove: picturesPermissions
    }),
    // Transform file when reading
    transformRead(from, to, fileId, file, request) {
        from.pipe(to); // this returns the raw data
    },
    transformWrite(from, to, fileId, file) {
      let gm = Npm.require('gm');
      if (gm) {
        gm(from)
            .resize(400, 400)
            .gravity('Center')
            .extent(400, 400)
            .quality(75)
            .stream().pipe(to);
      } else {
          console.error("gm is not available", file);
      }
    }
});

Pictures.getPictureUrl = function (selector) {
    const picture = this.findOne(selector) || {};
    return picture.url || DEFAULT_PICTURE_URL;
};

function picturesPermissions(userId: string): boolean {
    return Meteor.isServer || !!userId;
}

actually the picture is being saved on Mongo, but with size zero.
If I remove "transformWrite" everything works like a charm. Maybe is a problem related to gm, but i did not found any solution.

Note: using localFS produce the same issue.

I have installed gm with

meteor npm install -g gm

Thanks

Installing gm is not enough, you also have to install the graphicsmagick package on your OS.
For example on Debian/Ubuntu: apt update && apt install graphicsmagick, or through your graphical package manager.

jalik commented

To complete @macman31 's anwer, gm is a node package that expose an API (methods) to do some processing on images (resize, crop, compress...), but it does nothing on its own, instead it requires graphicsmagick which is a system package that you can install with apt-get on Debian based OSes.