Meteor-Community-Packages / Meteor-CollectionFS

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

Generating fixture #960

Open nick-lehmann opened 8 years ago

nick-lehmann commented 8 years ago

Unfortunately, I did not found any way to insert files on the server side so I could generate fixtures after reseting Meteor. I have already tried some approaches.

The first one proposed to use a little workaround to create HTML5-like file objects which can then be used in combination with Images.insert() (where Images is an CFS collection). Unfortunately, XMLHttpRequest() is not available.

Furthermore, you could try to use the Asset API to load images from the application's /private folder, but the returned object is not valid when passing it to the insert function because the DataMan constructor requires a type argument when passed a Uint8Array.

Using the core http package in combination with the http-extra package by aldeed. Unfortunately, it only accepts http and https urls. I also did not find a way to use jQuery methods.

Are there any possible solutions to generate fixture on server start?

aspin commented 8 years ago

FYI if you're still looking:

  // for images, with Images as the collection

  const url = '/whatever.png';
  const image = new Buffer(Assets.getBinary(url));
  const base64 = image.toString('base64');
  const newFile = new FS.File();
  newFile.attachData(new Buffer(base64, 'base64'), { type: 'image/png' }, (error) => {
    if (error) throw error;
    Images.insert(newFile);
  });
}

This is assuming you store your image in the private/ folder. If it's in public/, then use one of the node FS streams to read it. The conversion back and forth between base64 is probably unnecessary, but I haven't tested otherwise since I was using this method to store pictures from an iOS app.

nick-lehmann commented 8 years ago

Ok thanks, I will give it a try. Unfortunately, CFS is now deprecated, maybe looking for an alternative with more future.