vsivsi / meteor-file-sample-app

Sample application for Meteor file-collection Atmosphere package
Other
15 stars 10 forks source link

Incomplete URL causes server to crash with: TypeError: Cannot read property 'contentType' of undefined #8

Closed nathanbrizzee closed 7 years ago

nathanbrizzee commented 7 years ago

Hi, I have found an error with the demo application that is present in my application also. If you enter a URL to a partial directory, the server crashes with the error: TypeError: Cannot read property 'contentType' of undefined.

I see the same problem in using this package in my own application besides this demo. The problem I have in my application is I receive this error all the time for any /md5/XXXXXXX URL. When I look in my database,, everything looks fine.

Steps to recreate with this demo:

  1. Clone this repo
  2. Satisfy new mongo requirement by installing bcrypt : >meteor npm install --save bcrypt
  3. Run Meteor: >meteor
  4. Open a browser to the running instance : http://localhost:3000/
  5. Drop a jpg image on the table. It doesn't matter whether you log in or not. You will get the same error. I am not logging in.
  6. The table should refresh showing the newly added image.
  7. Now click on the image to view it. The URL will change to something like http://localhost:3000/gridfs/fs/md5/fe22239f0a8f7348e9b4503efa26327b
  8. Next remove the md5 hash code from the URL just leaving the URL through /md5 (http://localhost:3000/gridfs/fs/md5)

You should see an error on both the server console and the web page:

TypeError: Cannot read property 'contentType' of undefined at FileCollection.get (packages/vsivsi_file-collection/src/http_access_server.coffee:195:45) at Layer.handle [as handle_request] (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/layer.js:95:5) at next (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/route.js:131:13) at next (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/route.js:125:14) at Route.dispatch (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/layer.js:95:5) at /home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/index.js:277:22 at param (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/index.js:349:14) at param (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/index.js:365:14) at Function.process_params (/home/nodeuser/.meteor/packages/vsivsi_file-collection/.1.3.6.zvb1qx++os+web.browser+web.cordova/npm/node_modules/express/lib/router/index.js:410:3)

fs.files contains the following: { "_id" : ObjectId("0f0f348544a7bf298e8e6513"), "length" : 52413, "md5" : "fe22239f0a8f7348e9b4503efa26327b", "uploadDate" : ISODate("2016-11-17T00:22:47.056Z"), "chunkSize" : 2096128, "filename" : "recent-images-11.jpg", "metadata" : { "_auth" : { "owner" : null } }, "aliases" : [], "contentType" : "image/jpeg" }

fs.chunks contains the following: { "_id" : ObjectId("582cf857573dd68424cf62cf"), "files_id" : ObjectId("0f0f348544a7bf298e8e6513"), "n" : 0, "data" : { "$binary" : "/9j/4AAQSkZJRgABAQAAAQAB {content removed to make it short} BN0Lc8NozQX//Z", "$type" : "00" } }

I am using vsivsi_file-collection version 1.3.6 at the time of this writing.

vsivsi commented 7 years ago

Thanks for reporting this, I'll look into it within the next 48 hours.

vsivsi commented 7 years ago

I've isolated the problem, have added a failing test to the file-collection package, and have a proposed fix. I need to do a bit more testing, but I anticipate releasing a new version of file-collection tonight. Once that is done, I'll update all of the sample apps to use the new version.

nathanbrizzee commented 7 years ago

I really appreciate your quick response. The error is preventing me from completing my application piece. I'm not sure why I get the error no matter what I try - md5 lookup or by ID. I wrote another http method to read by id which is what I really need to guarantee unique files when two people upload the same image. I'm using Angular-Meteor for my project.

import { Mongo } from 'meteor/mongo';

// Create a file collection, and enable file upload and download using HTTP
export const PartImages = new FileCollection('partImages',
  {
    resumable: true,   // Enable built-in resumable.js upload support
    resumableIndexName: 'test', // Don't use the default MongoDB index name, which is 94 chars long
    http: [
      {
        method: 'get',
        path: 'md5/:md5',  // this will be at route "/gridfs/partImages/md5/:md5"
        lookup: function (params, query) {  // uses express style url params
          console.log("method get md5 : " + params.md5);
          return { md5: params.md5 };       // a query mapping url to myFiles
        }
      },
      {
        method: 'get',
        path: 'id/:id',  // this will be at route "/gridfs/partImages/id/:id"
        lookup: function (params, query) {  // uses express style url params
          console.log("method get id : " + params.id);
          return {'_id':ObjectId(params.id)};       // a query mapping url to myFiles
        }
      }
    ]
  }
);

if (Meteor.isServer) {

  Meteor.users.deny({
    update: function() {
      console.log("Meteor users deny");
      return true;
    }
  });

  // Allow rules for security. Should look familiar!
  // Without these, no file writes would be allowed
  PartImages.allow({
    insert(userId, file) {
      file.metadata.owner = userId;
      console.log("PartImages.insert " + file.filename + ";" + file.metadata.owner);
      return true;
    },
    remove(userId, file) {
      console.log("PartImages.remove = true");
      return true;
    },
    // Only owners can retrieve a file via HTTP GET
    read(userId, file) {
      console.log("read userID=" + userId + "; owner=" + file.metadata.owner);
      console.log("PartImages.Read id: " + file._id);
      console.log("PartImages.Read md5: " + file.md5);
      console.log("PartImages.Read contentType: " + file.contentType);
      return true;
    },
    // This rule secures the HTTP REST interfaces' PUT/POST
    // Necessary to support Resumable.js
    write(userId, file, fields) {
      console.log("write userID=" + userId + "; owner=" + file.metadata.owner);
      console.log("PartImages.write = " + retval); 
      return true;
    }
  });
}
vsivsi commented 7 years ago

The error is in file-collection. I'm so surprised nobody has seen it before... Your app should start working once I publish the update.

vsivsi commented 7 years ago

Version 1.3.7 of file-collection is on Atmosphere now, fixes this issue. This sample app has been updated to use the newest version and demonstrates the correct behavior.