Skip to content Skip to sidebar Skip to footer

Store A DataURL In MongoDB To Access It Via Local URL (JS)

So I don't know if my title is clear enough, what I want to do is, in my Meteor App, to have a tool that upload a file into my MongoDB, and output an URL that I could use it for ex

Solution 1:

This is very much possible and in a secure way using packages cfs:standard-packages and cfs:filesystem. Frankly speaking, I did not go in depth for your question.

It is a beautiful package. You can read about above here packages in depth.

CODE TO CREATE FILESYSTEM COLLECTION

var imageStore = new FS.Store.FileSystem("YOUR_COLLECTION_NAME");

YOUR_COLLECTION_NAME = new FS.Collection("YOUR_COLLECTION_NAME", {
  stores: [imageStore]
});

In Short. The files will be saved at the sibling to bundle/ location at cfs/files/YOUR_COLLECTION_NAME. Information about the file will be saved in the collection cfs.YOUR_COLLECTION_NAME.filerecord and temporary location will be used for internal purpose with collection cfs._tempstore.chunks as below.

If you save any file using above package. The metadata will be saved inside the cfs.YOUR_COLLECTION_NAME.filerecord as below

{
    "_id" : "TBmxbsL2cMCM2tEc7",
    "original" : {
        "name" : "photo.jpg",
        "updatedAt" : ISODate("2017-07-06T12:54:50.115Z"),
        "size" : 2261,
        "type" : "image/jpeg"
    },
    "uploadedAt" : ISODate("2017-07-08T06:58:32.433Z"),
    "copies" : {
        "YOUR_COLLECTION_NAME" : {
            "name" : "photo.jpg",
            "type" : "image/jpeg",
            "size" : 2261,
            "key" : "YOUR_COLLECTION_NAME-TBmxbsL2cMCM2tEc7-photo.jpg",
            "updatedAt" : ISODate("2017-07-08T06:58:32.475Z"),
            "createdAt" : ISODate("2017-07-08T06:58:32.475Z")
        }
    }
}

at Client side, you can get the link to the document file using

YOUR_COLLECTION_NAME.find({"_id" : "TBmxbsL2cMCM2tEc7"}).url();

This url is token based safe link to the file instead of direct location to your server. You can set additional allow/deny settings for download ans stuff just like normal collections. Hope this helps!


Post a Comment for "Store A DataURL In MongoDB To Access It Via Local URL (JS)"