A while ago I wrote a blog post
about handling binary data in Foxx applications. That blog post provided a solution
for retrieving and serving non-UTF-8 data from a Foxx application. It was written
for the 2.x series of ArangoDB.
With the release of ArangoDB 3.0 that solution needs some adaption, and this
blog post provides the source code for the 3.0 version of the Foxx application.
The rawBodyBuffer() function of the request object is gone in 3.0, and to
retrieve the body as a blob we now need to use req.rawBody.
And as there are no controllers and applicationContexts in Foxx 3.0 anymore,
the original route code needs to be changed from
2.8 Foxx action that can handle binary input
12345678
controller.post('/receive-binary',function(req,res){// fetch request body into the buffervarbody=req.rawBodyBuffer();// create an absolute filename, local to the Foxx application directoryvarfilename=applicationContext.foxxFilename("body");require("fs").write(filename,body);});
to the following code:
3.0 Foxx action that can handle binary input
12345678
router.post('/receive-binary',function(req,res){// fetch request body into a Buffervarbody=req.rawBody;// create an absolute filename, local to the Foxx application directoryvarfilename=module.context.filename("body");require("fs").write(filename,body);});
To serve binary data from a Foxx action, the original route code needs to
be changed from
2.8 Foxx action that returns contents of a file
123456
controller.get('/provide-binary-file',function(req,res){// create an absolute filename, local to the Foxx application directoryvarfilename=applicationContext.foxxFilename("body");// send the contents, this will also set mime type "application/octet-stream"res.sendFile(filename);});
3.0 Foxx action that returns contents of a file
123456
router.get('/provide-binary-file',function(req,res){// create an absolute filename, local to the Foxx application directoryvarfilename=module.context.fileName("body");// send the contents, this will also set mime type "application/octet-stream"res.sendFile(filename);});
The adjusted Foxx application can be downloaded here
with full source code.