1

My issue:

I need to extract the binary (01001) or whatever String I can extract from a file and send it via HTTP as my response (could be in a JSON or an XML, I don't care). I believe I am able to extract the contents of the file and send it to the client, but for some reason after my client writes it into its file system it can't be correctly executed and the contents of the file (in my case an mp3) used (in my case listened by the human user).




Diagram explaining what I am doing: enter image description here




Easy to replicate:

The answer you give can be in any language, if you ask for what I am using, it is Rust for the server and Swift for the client, but because those languages are not as widely used as say JS, I am cool with any language for the answer as this question is more about the concept and implementation in general than implementation in a specific device/language.

I have replicated a little express server you can copy and paste in your machine, but you can get the mp3 file through your language of choice with this link https://download.samplelib.com/mp3/sample-3s.mp3 or any other link that sends back an mp3 file... the one I am actually using requires private keys but you can use any other, preferably a public one like the one above so that you can share it with me, but if you do not wanna do that, that is fine.

Sample Express JS Server (Does not matter what language you use for your answer):

app.js:

const express = require('express')
const app = express()
const port = 3000
const fetch = require("./fetch");

app.get('/', async (req, res) => {
    let audioFile = await fetch.fetch("https://download.samplelib.com/mp3/sample-3s.mp3")
    console.log(audioFile.body)
    res.send(audioFile.body["_readableState"]["buffer"]["head"])
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

fetch.js:

exports.fetch = async function (url, init) {
    const {default: fetch} = await import("node-fetch");
    return await fetch(url, init);
};

File structure for this simple specific replication: enter image description here




MODERATORS: Even if you se JavaScript in this post, it does not mean it has anything to do with it, it is used as an example and this question is language-agnostic. Please do not add any language tags in the post just because you saw JS in it, thank you.




Not Important At All Note (SKIP IT):

Each 1 and 0 is still one byte in memory, so if instead of getting the binary from the mp3 another string is extracted from it, like hex or whatever that takes up less bytes that the long full binary string in the HTTP response, would definitely be better. So if you want to add a RECOMMENDATION in that, you are welcome to do so.

sivafe9540
  • 23
  • 7
  • Why do you want to represent a potentially large binary file (your .mp3) as text (i.e., convert it to JSON/XML)? That will likely increase the size as you'll need to convert it to base64. And you don't execute a sound file - you have something interpret the bytes for you - i.e. a media player of some sort. – stdunbar Aug 20 '22 at 23:35
  • @stdunbar this is all done to avoid the hosting of these sound files myself as I am using a private key to get them from the server that is actually hosting them. So it is better if I just get the file from them and send the binary to the client so they can write it into the file system themselves, not get it from my server. Basically, I avoid the computations of writing and hosting files in my server. – sivafe9540 Aug 20 '22 at 23:40

0 Answers0