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

Can't get custom resumable working #136

Closed hamedhemmati closed 8 years ago

hamedhemmati commented 8 years ago

I have everything working fine with the built-in resumable but when I try to use my custom one it creates a file with 0 length. When I was reading the documentation it says that the custom resumable has to match both on the client and the server side but I don't see any setting for resumable on the server side. The only thing is the target that needs to match the collection name right? Or am I missing something?

Here is what I have on my client

Template.flow.onRendered(function () {
    r.assignDrop($(".dropZone"));
});
Meteor.startup(function() {
    r = new Resumable({
        target: '/gridfs/avatars/_resumable',
        generateUniqueIdentifier: function(file){ return '${new Mongo.ObjectID()}'; },
        fileParameterName: 'file',
        chunkSize: 2*1024*1024,
        testChunks: true,
        testMethod: 'HEAD',
        permanentErrors: [204, 404, 415, 500, 501],
        simultaneousUploads: 1,
        maxFiles: undefined,
        maxFilesErrorCallback: undefined,
        prioritizeFirstAndLastChunk: false,
        query: undefined,
        headers: {},
        maxChunkRetries: 5,
        withCredentials: true
    });

    r.on('fileAdded', function (file) {
        Session.set(file.uniqueIdentifier, 0);
        console.log(file);
        Avatars.insert({
                _id: file.uniqueIdentifier,  // This is the ID resumable will use
                filename: file.fileName,
                contentType: file.file.type
            },
            function (err, _id) {  // Callback to .insert
                if (err) { return console.error("File creation failed!", err); }
                // Once the file exists on the server, start uploading
                r.upload();
            }
        );
    });
    r.on('fileProgress', function (file) {
        Session.set(file.uniqueIdentifier, Math.floor(100*file.progress()));
    });
    r.on('fileSuccess', function (file) {
        Session.set(file.uniqueIdentifier, undefined);
    });
    r.on ('fileError', function(file) {
            console.warn("Error uploading", file.uniqueIdentifier);
            Session.set(file.uniqueIdentifier, undefined);
        });
    Tracker.autorun(function () {
        Meteor.subscribe('myAvatar', Meteor.userId());
        Cookie.set('X-Auth-Token', Accounts._storedLoginToken(), { path: '/' });
    });
});

and this is what I have on the server side.

Avatars = new FileCollection('avatars',
    { resumable: true,   // Enable built-in resumable.js upload support
        http: [
            { method: 'get',
                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
                }
            }
        ]
    }
);
Meteor.publish('myAvatar',
    function (clientUserId) {
        if (clientUserId === this.userId) {
            return Avatars.find({ 'metadata._Resumable': { $exists: false },
                'metadata.owner': this.userId });
        } else {  
            return null; 
        }
    }
);

Avatars.allow({
    insert: function (userId, file) {
        file.metadata = file.metadata || {};
        file.metadata.owner = userId;
        return true;
    },
    remove: function (userId, file) {

        return (userId === file.metadata.owner);
    },
    read: function (userId, file) {
        return (userId === file.metadata.owner);
    },
    write: function (userId, file, fields) {
        return (userId === file.metadata.owner);
    }
});
hamedhemmati commented 8 years ago

figured it out the generateUniqueIdentifier should be function(file){ return "" + (new Mongo.ObjectID()); }

not function(file){ return '${new Mongo.ObjectID()}'; }

I took it from the issue #102 without paying attention.

vsivsi commented 8 years ago

Glad you solved it!