Home > Back-end >  Download file from HTTP Drive API v3 with real file name
Download file from HTTP Drive API v3 with real file name

Time:12-25

I have a custom REST Service in which a user from our platform downloads packages and binaries, but the problem is that the GDrive API downloads the file with the FileID as the file name:

async Download(fileID, res, type='stream') {
    var Google = await CloudStorage.Initialize();
    
    https.get(Google.API.STORAGE.Files.Download(fileID), {
        headers: {
            "Authorization": "Bearer "   Google.AccessToken.token,
            "Content-Type": "text/plain"
        },
        responseType: type
    }, (resApi) => {
        res.writeHead(resApi.statusCode);
        resApi.pipe(res);
    }).end();
}

Example: https://MyUrl.com/beta/plugins/download/ABCDE12345

Where ABCDE12345 is the FileID of the file required by Google Drive API in order to GET the file.

The pipe of the response from the API indeed makes the downloaded file be named ABCDE12345.

Is there a way to make the download similar as doing it directly from the Google Drive Link? When you download the file from the "Download" button through the Google Drive link it does download the file with the real name... How could I achieve this with my endpoint?

CodePudding user response:

So,I had to use my girlfriend as the "rubber duck" and explain her my code when it came to my head while explaining: Use a fake endpoint for the named files.

My solution was to trick the endpoint by assigning a filename in a path parameter:

router.get('/dl/:id/:filename', async (req, res) => {
    CloudStorage.Download(req.params.id, req.params.filename, res);
});

Now, having the :filename as a parameter allows me to verify it against the GDrive API since it's available for the given ID

In the end, it is now used as: https://MyUrl.com/beta/plugins/dl/ABCDE12345/Real File Name.ext

I can verify if Real File Name.ext is the real file name, if it is, then download it and the endpoint will allow me to create a file with this name :)

  • Related