flowjs / flow.js

A JavaScript library providing multiple simultaneous, stable, fault-tolerant and resumable/restartable file uploads via the HTML5 File API.
Other
2.96k stars 346 forks source link

Node.js sample doesn't work #17

Open ChristophHaag opened 10 years ago

ChristophHaag commented 10 years ago

First, the Readme is outdated. It says to do npm install express but this downloads express 4.0 now, but the code depends on express 3.x.

Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

So it should say npm install express@3.x until it is fixed for 4.x.

Then the upload simply never finishes on the server side.

I actually first tried the node.js sample from resume.js and after it worked fine I tried to change the directory of the uploaded files and at this point point it did the same which the sample here does:

The line POST done <filename> doesn't appear on the server side log (only partly_done for all the chunks), but on the browser side it says fileSuccess FlowFile [...] complete. The samples/Node.js/tmp directory remains empty.

I haven't really tested more and I don't think I have much motivation right now to do it.

node v0.10.26 chromium Version 35.0.1916.17

AidasK commented 10 years ago

Well yes, this is an old example and needs a good rewrite. Example could even be rewritten on other nodejs framework. Maybe somebody could collaborate on this?

thrownblown commented 10 years ago

i'm having a ton of issues with this too. i haven't found a good middleware to handle the multipart/formdata.

suggestions?

thrownblown commented 10 years ago

i made a pull request to fix this. i did not update the read.me in the pull.... maybe i'll make another.

also!

i'm having an issue assembling the binary from the blobs, if you're into stackoverflow points, you could answer my question (and the two other unanswered questions that are essentially the same question): http://stackoverflow.com/questions/23449065/reassemble-binary-after-flow-js-upload-on-node-express-server

AidasK commented 10 years ago

Reassembling all chunks is easy, just call this:

var stream = fs.createWriteStream(filename);
r.write(identifier, stream);

And that is it!

But other question is, when this method should be called? Maybe then all chunks are uploaded and present at tmp folder. https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js#L122 And another issue is solved.

But there is another issue with duplicate calls of done: https://github.com/flowjs/flow.js/issues/16 This can be solved by creating and locking the file, once all chunks exists. Then call r.write(identifier, stream); Then clean all chunks. https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js#L182 Then release the lock and close the file.

Same approuch is done in php server side library: https://github.com/flowjs/flow-php-server/blob/master/src/Flow/File.php#L102

Hope this helps, and I hope someone could collaborate and update node.js sample with these fixes.

ralyodio commented 10 years ago

It worked for me (now that I'm on the right server). But there is no method for deleting.

michaelryancaputo commented 10 years ago

@chovy Any chance this solution could be added to the example? I'm trying to figure it out, I understand what is happening here, but I can't seem to produce the result.

ralyodio commented 10 years ago

I decided not to use flow.js infavor of a custom handler.

michaelryancaputo commented 10 years ago

@AidasK Any chance you can provide a more complete answer with an example?

It looks like you've been pasting the same solution in a number of places (stackoverflow), but don't really go into any depth to explain it.

AidasK commented 10 years ago

It isn't me posting in stackoverflow with this solution, somebody else is trying to earn some points.

The reason, why I don't provide a normal nodejs solution, is because this sample is not written by me. And I hate how it looks https://github.com/flowjs/flow.js/blob/master/samples/Node.js/flow-node.js This sample needs a rewrite.

If you are just trying to play with flow.js or Nodejs is not neccessary, you should try php library instead: https://github.com/flowjs/flow-php-server

michaelryancaputo commented 10 years ago

@AidasK strange about the SO person.. what a lame thing to do. Anyway, I worked with this most of yesterday and came up with this, if anything, maybe it's a good starting point for someone more experienced than me:

exports.post = function (req, res, next) {

    flow.post(req, function(status, filename, original_filename, identifier) {

        console.log('status: '+ status, filename, original_filename, identifier);

        if(status==='done'){

            var s = fs.createWriteStream('./uploads/' + filename);
            s.on('finish', function() {

                res.send(200, {
                    // NOTE: Uncomment this funciton to enable cross-domain request.
                    //'Access-Control-Allow-Origin': '*'
                });

            });

            flow.write(identifier, s, {end: true});
        }

    });

};
AidasK commented 10 years ago

This might work, but be careful with simultaneous uploads, because status done might occur multiple times for the same file.

michaelryancaputo commented 10 years ago

@AidasK You're right, i've only needed it for a single file. Thank you for the feedback.

MilosStanic commented 9 years ago

Hi @AidasK and @flashpunk, I applied the solution from StackOverflow. However, I don't know how to set the destination directory programatically. I would like to write my files to directories in format /uploads/YYYY/mm/dd/filename . However, changing the destination directory from './tmp/' to '/uploads' broke the code.

It would be really, really nice if we had a working example of flow-node.js .

Thanks.

paulklemm commented 9 years ago

@MilosStanic You can set the chunk directory by modifying these two lines in the example: https://github.com/flowjs/flow.js/blob/master/samples/Node.js/app.js#L1 and https://github.com/flowjs/flow.js/blob/master/samples/Node.js/app.js#L6

To store the resulting file in a custom directory, I've added

var stream = fs.createWriteStream(UPLOAD_DIR + '/' + filename);
flow.write(identifier, stream);

here: https://github.com/flowjs/flow.js/blob/master/samples/Node.js/app.js#L19

Does this answer your question?

MilosStanic commented 9 years ago

@Powernap thanks, I solved my issues with Flow weeks ago. I don't really remember what I did. But I'm using Flow for my web app. And I like it.

ptgamr commented 8 years ago

Hope that this implementation is helpful for you guys (re-write in ES6 & use Bluebird Promise)

https://gist.github.com/ptgamr/4d1d07b770321a138c91

@AidasK Do you think it's good enough for a pull request?

AidasK commented 8 years ago

@ptgamr Looks awesome and clean. If you can, please update node.js sample with your code. I would be glad to accept it.