Stop fs.createWriteStream creating a writable stream when deleting a file

People. I am creating an Angular / Node app where users upload files using the appropriate thumbnail.

  • When uploading files, a small list is displayed indicating the upload - using the status-bar .
  • When the file is uploaded, a success message is displayed.
  • Each list item has a delete button that deletes files when clicked. This all works great.

Question . Like this post - when the delete button is clicked the idea is to stop the download - which is why I think I'll just delete the file.

However, I use fs.createWriteStream

, and when the file is deleted, the stream seems to continue regardless of the fact that the file does not exist. This then brings up a status state file.on('finish', function() {

and displays a success message.

To solve this problem, I check if the file path exists when the state finish

starts up so that it correctly displays the success message. This seems to be quite a hack, especially when uploading large files.

Is there a way to cancel a stream from a process when deleting a file?

+3


source to share


2 answers


After your comment "yes, just like that", I have one question. Obviously you are creating a file on the client system and writing to streams. How do you do it from the browser? Are you using any API that gives you access to the main node module in the browser? How to browserify .

Having said that, if my understanding is correct, you can achieve it like this

var http = require("http"),
        fs = require("fs"),
        stream = require("stream"),
        util = require("util"),
        abortStream=false,  // When user click on delete, update this flag to true
        ws,
        Transform;

ws = fs.createWriteStream('./op.jpg');

// Transform streams  read input, process data [n times], output processed data
// readStream ---pipe---> transformStream1 ---pipe---> ...transformStreamn ---pipe---> outputStream
// @api https://nodejs.org/api/stream.html#stream_class_stream_transform
// @exmpl https://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/ 
Transform = stream.Transform || require("readable-stream").Transform;

function InterruptedStream(options){
    if(!(this instanceof InterruptedStream)){
        return new InterruptedStream;
    }

    Transform.call(this, options);
}

util.inherits(InterruptedStream, Transform);

InterruptedStream.prototype._transform = function (chunkdata, encoding, done) {
    // This is just for illustration, giving you the idea
    // Do not hard code the condition here.
    // Suggested to give the condition during constructor call, may be
    if(abortStream===true){
        // Take care of this part.
        // Your logic might try to write in the stream after it is closed.
        // You can catch the exception but before that try not to write in the first place
        this.end(); // Stops the stream
    }
    this.push(chunkdata, encoding);
  done();
};


var is=new InterruptedStream();
is.pipe(ws);

// Download large file
http.get("http://www.zastavki.com/pictures/1920x1200/2011/Space_Huge_explosion_031412_.jpg", function(res) {
        res.on('data', function(data) {
            is.write(data);
            // Simulates click on delete button
            setTimeout(function(){
                abortStream=false;
                res.destroy();
                // Delete the file, I think you have the logic in place
            }, 2000);
        }).on('end', function() {
                console.log("end");
        });
    });

      



The above code snippet gives you a general idea of ​​how to do this. You can just copy it, run it (it will work) and make changes.

If we are not on the same page please let me know, I am trying to correct my answer.

+1


source


I think you can fire an event when your file is deleted and capture that event in

var wt = fs.createWriteStream();
wt.on('eventName',function(){
                wt.emit('close');                                     
})

      



this will close your writeableStream.

and the event deletion must be fired from the client side.

0


source







All Articles