Meteor-Community-Packages / Meteor-CollectionFS

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

how to store image from external URL with content-type attachment #814

Open Nits7029 opened 9 years ago

Nits7029 commented 9 years ago

Hello,

I am fetching images in bulk from external URL, but from some of URLs images are downloading directly, I think it's content-type is attachment so. below is the code that I am using

1) code is working fine

var newFile = new FS.File();
newFile.attachData("https://d3kjp0zrek7zit.cloudfront.net/uploads/product/image/4533/large_detail_1410557285.png", function(error) {
  console.log(error);
  newFile.name("test.png");
  Images.insert(newFile, function(error, fileObj) {
    console.log(error, fileObj);
  });
});

2) getting error - Error: FS.Collection insert: file does not pass collection filters https://d3kjp0zrek7zit.cloudfront.net/uploads/product/image/4533/large_detail_1410557285.png has attachment content type.

var newFile = new FS.File();
newFile.attachData("https://d3kjp0zrek7zit.cloudfront.net/uploads/product/image/4533/large_detail_1410557285.png", function(error) {
  console.log(error);
  newFile.name("test.png");
  Images.insert(newFile, function(error, fileObj) {
    console.log(error, fileObj);
  });
});

Please help

dhavalrajani92 commented 9 years ago

+1

nooitaf commented 9 years ago

I don't see any difference in 1) and 2) but you might want to check your collection filters and make sure you accept the right content types, in your case binary/octet-stream.

var imageStore = new FS.Store.GridFS("images", {});

Images = new FS.Collection("images", {
  stores: [imageStore],
  filter: {
    maxSize: 1048576, // in bytes
    allow: {
      contentTypes: ['binary/octet-stream'], // <<<<------------------
      extensions: ['png']
    },
    deny: {
      contentTypes: [],
      extensions: []
    },
    onInvalid: function (message) {
      console.log(message);
    }
  }
});

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
    var newFile = new FS.File();
    newFile.attachData("https://d3kjp0zrek7zit.cloudfront.net/uploads/product/image/4533/large_detail_1410557285.png", function(error) {
      console.log(error);
      newFile.name("test.png");
      Images.insert(newFile, function(error, fileObj) {
        console.log(error, fileObj);
      });
    });    
  });
}
Nits7029 commented 9 years ago

Thanks for you reply, I really appreciate it.

One more thing, but in my case content type will be "image" or "binary/octet-stream"

So how to allow multiple content type in configuration.

Thanks.

Nits7029 commented 9 years ago

is it something like ['image/*', 'binary/octet-stream'] ?

nooitaf commented 9 years ago

yes, but the link you provided is a binary/octet-stream.

Nits7029 commented 9 years ago

ok, but i have lots of image URLs and I don't all images content type, that's why.

One more thing.

My site is running on http, so can i store images which are from https.

I run you code and it's giving me a below error:

timeout and Error: FS.Collection insert: file does not pass collection filters.