Search code examples
node.jsmeteorangular-meteor

ostrio:files package is working, but can’t access image


I got the ostrio:files package working in my application, so my images are uploading to the server (as far as I know) and the DB has a reference to them, but I don’t know how to actually serve them up. For example, I uploaded an image this morning. The DB contains the original name, and the file path that it exists at now:

assets/app/uploads/Images/twLXDmCTiqyGy4Yq7.jpg

The problem is, when I put that into the url, it just returns my app’s homepage. I tried changing the upload directory to be /images/ but still have the same issue. I can’t seem to find a clear answer in Meteor’s documentation on how to open up a path for images, aside from including them when you build the package, but that defeats the purpose of user-input.

I also have noticed that the programs/web.browser/program.json file in my application seems to control what is allowed to be visible, but ideally, I would just like to open up my /images directory to view any image (only direct paths, of course - I don’t want a directory listing). Is that a possibility?

Furthermore, I found the config.public configuration option in the documentation, and set it to true, as well as setting the storagePath and downloadRoute paths. The paths I set are coming from the base of my server (using / for the start of the paths) but I still cannot seem to view any of the images that I upload - it just loads my default home page for my app

----EDIT----

Adding more detail for help in figuring this out.

I set up my FileCollection in my /imports/api/images.js file, like this:

export const Images = new FilesCollection({

Later on, in another file, I import that api and do the upload, using Images.insert({ and then use the upload.on('end', function to get it uploaded. I'm guessing that is where I'm missing saving the file to my filesystem? Hopefully this helps and provides more detail for debugging. Also, when I do have a FileCursor object, I try the link() method and it just returns link is not a function. I basically just followed the Meteor Files GitHub Example.

--- EDIT 2 ---

As pointed out by @Rolljee, I had other packages that were still in my project that I hadn't removed. They were interfering and causing this to not work.


Solution

  • When you store a new record in the FilesCollection collection, it automatically create the link() method you can use to get the path where it serve your file.

    You should be able to get it like so:

    const theUrl = MyFilesCollection.findOne(someId).link();
    

    it is also possible to create the like yourself, it follow this simple naming convention:

    link: `${Meteor.absoluteUrl() + file._downloadRoute}/${file._collectionName}/${file._id}/original/${file._id}.${file.extension}`,