Juan Castillo
Juan Castillo

Reputation: 103

About Node Google Drive API

Currently i'm using the google drive api of NodeJS, this is the piece of code i'm using to download a file

try {
    
    const auth = await autenticacion();
    const drive = google.drive({ version: 'v3', auth });

    drive.files.get(
        { fileId, alt: 'media' },
        { responseType: 'stream' },
        ((err, respuesta) => { 
            if(err) {
                console.log(err);
                res.end();
            }
            respuesta.data
            .on('end', () => {
                res.end()
            })  
            .on('error', err => {
                res.end()
            })  
            .on('data', d => {
                res.write(d)
                //the next line below i'm trying to cancel the download but gives me an exception an the programm breaks
                res.on('close', da => {
                    res.end();
                })
            });
            

        })
    )
    
    
} catch (error) {
    console.log(error)
    
}

what i'm trying to achieve also is to cancel the current download, i'm using angular

this.descargaActual.unsubscribe();

when i'm trying to cancel the download clicking on the button, in frontend the download stops but in the backend process the file still being downloaded. Like i mention in a comment in the code, it gives me an exception enter image description here

is there a way to cancel a current download in the google drive api from node?

Upvotes: 0

Views: 82

Answers (1)

Heiko Theißen
Heiko Theißen

Reputation: 16666

To cancel the download, I would try to destroy the readable stream that I got from the Drive API. Otherwise, just pipe that stream into the response object.

res.on("close", function() {
  respuesta.data.destroy();
});
respuesta.data.pipe(res);

Upvotes: 1

Related Questions