Search code examples
node.jstypescriptbinarybase64slack

Reading data from file download link in node js (typescript)


I'm currently working with slack and struggling to retrieve file data when listning for events. My issue is not exactly related to slack so experience with it isn't required to help me out (thanks in advance:) ).

I have a program that successfully listens for events of files being shared to my Slack channel, and retrieves all file data. This data includes a private URL and private url download link to access the file, but not the file itself. I need to use one of these links to access the file data and save the file as a base64 encoded string, and this is where I am struggling to find a solution. (Edit: I cannot save the file locally)

Anything that can read a link like this: https://files.slack.com/files-pri/T05BB0WBGCV-F05HU6Y2X1Q/asfhaslf.pdf and retireve the binary data or something similar would be the goal.

Edit: the slack URL's are private and don't seem to allow me to retrieve the file data using fetch, instead I get the whole HTML document, so now I'm trying to find another solution.

Thanks again


Solution

  • Use an ArrayBuffer:

    The ArrayBuffer object is used to represent a generic raw binary data buffer.

    This will store the data from the downloaded URL in memory. Then you can convert the buffer to a nodejs Buffer which has a .toString() function that accepts passing in 'base64' as a parameter to get a base64-encoded string.

    Here's an example:

      const response = await fetch("https://your.url/here.jpg")
      const buffer = Buffer.from(await response.arrayBuffer())
      const base64text = buffer.toString('base64')
      const resAsBase64 = `data:${response.type};base64,${base64text}`
      console.log(resAsBase64)
    

    Source