Meteor-Community-Packages / Meteor-CollectionFS

Reactive file manager for Meteor
MIT License
1.05k stars 237 forks source link

Example code does not work #912

Open sahanDissanayake opened 8 years ago

sahanDissanayake commented 8 years ago

Hi, I tried the example code here https://github.com/CollectionFS/Meteor-CollectionFS/tree/devel/packages/s3#client-server-and-s3-credentials

250 px width image copy gets saved, but not both

Any thoughts ?

if ( Meteor.isServer ) {
var avatarStoreLarge = new FS.Store.S3("avatarsLarge", {
  accessKeyId: Meteor.settings.AWSAccessKeyId, 
  secretAccessKey: Meteor.settings.AWSSecretAccessKey, 
  bucket: Meteor.settings.AWSBucket,
  transformWrite: function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name()).resize('250', '250').stream().pipe(writeStream)
  }
})

var avatarStoreSmall = new FS.Store.S3("avatarsSmall", {
  accessKeyId: Meteor.settings.AWSAccessKeyId, 
  secretAccessKey: Meteor.settings.AWSSecretAccessKey, 
  bucket: Meteor.settings.AWSBucket,
  beforeWrite: function(fileObj) {
    fileObj.size(20, {store: "avatarStoreSmall", save: false});
  },
  transformWrite: function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name()).resize('20', '20').stream().pipe(writeStream)
  }
})

Images = new FS.Collection("avatars", {
  stores: [avatarStoreSmall, avatarStoreLarge],
  filter: {
    allow: {
      contentTypes: ['image/*']
    }
  }
})

} else {
    var avatarStoreLarge = new FS.Store.S3("avatarsLarge");
    var avatarStoreSmall = new FS.Store.S3("avatarsSmall");

    Images = new FS.Collection("avatars", {
      stores: [avatarStoreSmall, avatarStoreLarge],
      filter: {
        allow: {
          contentTypes: ['image/*']
        }
      }
    })

}
sahanDissanayake commented 8 years ago

Interesting observation

I insert the files from the console for testing as follows

var newFile = new FS.File();
newFile.attachData('https://crossorigin.me/http://www.wallpaperup.com/uploads/wallpapers/2014/01/25/237083/47b7ca5bd727bcda5c2dff4086f1a6d0.jpg', function (error) {
  if (error) throw error;
  newFile.name("newOne.png");newFile.metadata = {foo: "bar"};
  Images.insert(newFile, function (error, fileObj) {
    //If !error, we have inserted new doc with ID fileObj._id, and
    //remote URL data will be downloaded and stored on the server. The
    //URL must support a HEAD request since we do one to get the 
    //content type, size, etc. for filtering inserts.
  });
});

Observation is, When I run this code multiple times, One image is created each time [ 2 should have been created ] but the image size toggles between the big and small everytime.. dont know exactly whats going on in here

EDIT: Seems like only the small [ 20x20 ] image gets saved in the bucket, not the larger images

sahanDissanayake commented 8 years ago

Ok seems like the issue I had was I was using the same bucket, When you use different buckets, its all good.. Any thoughts how to fix this ?

I want to use one bucket and different folders

sahanDissanayake commented 8 years ago

One other fix I'm running with right now

beforeWrite: function(fileObj) {
      return {
          name: 'different_name_1 '
      }
  },

For each instance I give a different name so then there are two file with different names