Skip to content

Instantly share code, notes, and snippets.

@gilpanal
Last active April 3, 2024 15:16

Revisions

  1. gilpanal revised this gist Jun 9, 2020. No changes.
  2. gilpanal revised this gist Jun 9, 2020. No changes.
  3. gilpanal created this gist Jun 9, 2020.
    32 changes: 32 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const TEL_PATH = '/music/file_352.mp3'
    const API_FILEDONWLOAD = 'http://localhost:3000/fileDownload?'

    const load = () => {

    return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest()

    xhr.open('GET', API_FILEDONWLOAD + TEL_PATH, true)
    xhr.responseType = 'arraybuffer'
    xhr.send()

    xhr.addEventListener('progress', (e) => {
    console.log(`${e.type}: ${e.loaded} bytes transferred\n`)
    })

    xhr.addEventListener('load', (e) => {
    const audioData = e.target.response || e.target.result
    resolve(audioData)
    })

    xhr.addEventListener('error', () => {
    reject(Error('Track ' + TEL_PATH + ' failed to load'))
    })
    })
    }

    load().then((audiData) => {
    console.log(audiData)
    }).catch((err) =>{
    console.log(err)
    })
    12 changes: 12 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    </head>
    <body>
    BODY
    <script src="app.js"></script>
    </body>
    </html>
    42 changes: 42 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    /* TESTED WITH NODE VERSION 14+ */

    const express = require('express')
    const app = express()
    const https = require('https')
    const port = process.env.PORT || 3000

    // Use an Environment Variable to Secure Token Value
    const BOT_TOKEN = <BOT_SECRET_TOKEN>


    // For better CORS: https://expressjs.com/en/resources/middleware/cors.html
    app.use( (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
    next()
    })

    app.get('/', (req, res) => {
    res.sendStatus(200)
    })

    // Inspired by: https://stackoverflow.com/a/21024737
    app.get('/fileDownload', (req, res) => {
    let uploadResponse = { ok: false, result: null, error: 404, description: 'Not Found' }
    if (req._parsedUrl && req._parsedUrl.query) {
    const tel_file_path = 'https://api.telegram.org/file/bot' + BOT_TOKEN + req._parsedUrl.query
    https.get(tel_file_path, (response) => {
    const data = []
    response.on('data', (chunk) => {
    data.push(chunk)
    }).on('end', () => {
    const buffer = Buffer.concat(data)
    res.send(buffer)
    })
    })
    } else {
    res.sendStatus(uploadResponse)
    }
    })

    app.listen(port)