vsivsi / meteor-file-collection

Extends Meteor Collections to handle file data using MongoDB gridFS.
http://atmospherejs.com/vsivsi/file-collection
Other
159 stars 37 forks source link

Same md5 after insert #92

Closed crapthings closed 8 years ago

crapthings commented 8 years ago

code

myFiles = new FileCollection('myFiles', {
    resumable: true, // Enable built-in resumable.js chunked upload support
    http: [ // Define HTTP route
        {
            method: 'get', // Enable a GET endpoint
            path: '/:md5', // this will be at route "/gridfs/myFiles/:md5"
            lookup: function (params, query) { // uses express style url params
                console.log(params, query)
                return {
                    md5: params.md5
                }; // a query mapping url to myFiles
            }
        }, {
            method: 'put', // Enable a PUT endpoint
            path: '/:md5', // this will be at route "/gridfs/myFiles/:md5"
            lookup: function (params, query) { // uses express style url params
                return {
                    md5: params.md5
                }; // a query mapping url to myFiles
            }
        }, {
            method: 'options', // Enable an OPTIONS endpoint (for CORS)
            path: '/:md5', // this will be at route "/gridfs/myFiles/:md5"
            lookup: function (params, query) { // uses express style url params
                return {
                    md5: params.md5
                }; // a query mapping url to myFiles
            },
            handler: function (req, res, next) { // Custom express.js handler for OPTIONS
                console.log(req, res)
                res.writeHead(200, {
                    'Content-Type': 'text/plain',
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Methods': 'GET, PUT'
                });
                res.end();
                return;
            }
        }
    ]
});

if (Meteor.isClient) {

    Template.filepicker.events({
        'change input': function (e, t) {
            console.log(1, 'your file', e.currentTarget.files)
            var file = e.currentTarget.files[0]

            myFiles.insert({
                _id: new Meteor.Collection.ObjectID(),
                filename: file.name,
                contentType: file.type
            }, function () {
                myFiles.resumable.upload()
            })

        }
    })

}

if (Meteor.isServer) {

    myFiles.allow({
        insert: function () {
            return true
        },

        write: function () {
            return true
        },

        remove: function () {
            return true
        },
        read: function () {
            return true
        }
    })

}
vsivsi commented 8 years ago

fc.insert() creates an initial empty placeholder (zero length) file. All zero length files have the same md5 sum = d41d8cd98f00b204e9800998ecf8427e. This is the correct behavior. The md5 value is automatically recalculated and updated after you have finished uploading data to the file.

vsivsi commented 8 years ago

If you want to see a working example, I suggest the sample app: https://github.com/vsivsi/meteor-file-sample-app

crapthings commented 8 years ago

i can get corrent md5 after change meteor template.$.events to

myFiles.resumable.assignBrowse($(".fileBrowse"));

now it's working

but why i can't bind event myself ?

.js

myFiles = new FileCollection('myFiles', {
    resumable: true, 
    http: [ 
        {
            method: 'get', 
            path: '/:md5', 
            lookup: function (params, query) {
                console.log(params, query)
                return {
                    md5: params.md5
                };
            }
        }, {
            method: 'put', 
            path: '/:md5', 
            lookup: function (params, query) { 
                return {
                    md5: params.md5
                }; 
            }
        }, {
            method: 'options',
            path: '/:md5', 
            lookup: function (params, query) {
                return {
                    md5: params.md5
                }; 
            },
            handler: function (req, res, next) { 
                console.log(req, res)
                res.writeHead(200, {
                    'Content-Type': 'text/plain',
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Methods': 'GET, PUT'
                });
                res.end();
                return;
            }
        }
    ]
});

if (Meteor.isClient) {

    Meteor.startup(function () {

        myFiles.resumable.assignBrowse($("#filepicker"))

        myFiles.resumable.on('fileAdded', function (file) {

            myFiles.insert({
                    _id: file.uniqueIdentifier, 
                    filename: file.fileName,
                    contentType: file.file.type
                },
                function (err, _id) { 
                    if (err) {
                        return console.error("File creation failed!", err);
                    }
                    myFiles.resumable.upload();
                }
            );
        });
    })

}

if (Meteor.isServer) {

    myFiles.allow({
        insert: function () {
            return true
        },

        write: function () {
            return true
        },

        remove: function () {
            return true
        },
        read: function () {
            return true
        }
    })

}

.html

<head>
  <title>cordovatest</title>
</head>

<body>
    {{> filepicker}}
</body>

<template name="filepicker">
    <input id="filepicker" type="file">
</template>
vsivsi commented 8 years ago

If you want to use resumable,js to upload, then you don't need to define a PUT route because the server-side configures its own POST route. In any case, you cannot use md5 as the selector for an upload route because all empty files have the same md5 sum (as I noted above). If you are using Cordova and need to use CORS, then you do need to define an OPTIONS route for the resumable.js endpoint at /_resumable/:id for POST requests. See: https://github.com/vsivsi/meteor-file-collection/issues/90