I have a Go server that serves endpoints of an api. One of the endpoints creates an mp3 file and returns either the path of the converted file or all the bytes of the file. I am looking for one of two solutions.
The first solution would be to allow local file access for the 'href'. I have the output file string "/tmp/audio-hash123.mp3" returned from the Go server after it has done it's thing. The problem is that whenever I pass that to the 'href' to trigger the file download, it treats it as an absolute path such as "www.example.com/tmp/audio-hash123.mp3" instead of just allowing local access to the file. I have the converted audio file available locally but my tmp directory is obviously not externally accessible so this fails every time. Is this even possible?
The second solution would be for the client to allow for a triggered download based on the actual audio file returned from the server, not just the file path. I can't think of any way this is possible based on the fact that the html 'download' attribute is based on the path to the file to be downloaded. Is there a way to force a file download based on returning from the server the actual audio file?
Below is a small portion of the Go server that sends back either the file path or the actual audio file (in a byte array):
if ReturnFilePath == true {
//return the filepath for webpage to download
request.Send(outputFile)
} else {
//return the audio file
//get mime type
mimeType := GetMime(filepath)
if mimeType != "" {
req.Ctx.Response.Header.Set("Content-Type", mimeType)
}
//open image
file, err := os.Open(filepath)
if err != nil {
req.Send(err.Error())
return
}
//get bytes of image
bytes, err := ioutil.ReadAll(file)
if err != nil {
req.Send(err.Error())
return
}
req.Send(bytes)
}
With the data returned, I will facilitate the download in JavaScript. But, I need help in figuring out how to accomplish this. Any help is appreciated.