|
| 1 | +--- |
| 2 | +title: Handling Images |
| 3 | +--- |
| 4 | + |
| 5 | +OpenFn jobs run in Javascript, and most commonly we're handling JSON data from |
| 6 | +REST APIs or webhooks. We receive JSON, manipulate it with Javascript, then send |
| 7 | +JSON to some other REST API. Sometimes, however, you need to work with images or |
| 8 | +other binaries. This page explains how you do it. |
| 9 | + |
| 10 | +:::success The tl;dr: |
| 11 | + |
| 12 | +Images and other binaries mostly **_Just Work™️_**. Edges cases might |
| 13 | +need additions to adaptors. |
| 14 | + |
| 15 | +::: |
| 16 | + |
| 17 | +## Base64 (standard handling) |
| 18 | + |
| 19 | +In essence, the way to deal with images/PDFs/other files and be able to save |
| 20 | +them to `state` and pass them from step to step in an OpenFn workflow is to |
| 21 | +encode them as base64 and then turn them back into Buffers before sending them |
| 22 | +to a downstream system's API. |
| 23 | + |
| 24 | +The HTTP adaptor already contains everything you need to do this. Check out: |
| 25 | + |
| 26 | +1. [Request Options (`parseAs`)](https://docs.openfn.org/adaptors/packages/http-docs#requestoptions) |
| 27 | +2. [Encode](https://docs.openfn.org/adaptors/packages/http-docs#util_encode) a |
| 28 | + given string into Base64 format. |
| 29 | +3. [Decode](https://docs.openfn.org/adaptors/packages/http-docs#util_decode) a |
| 30 | + Base64 encoded string back to its original format. |
| 31 | + |
| 32 | +## Adaptor Native Support |
| 33 | + |
| 34 | +Some adaptors (DHIS2, FHIR-4, Sunbird-RC) have built in binary handling for |
| 35 | +known image/file endpoints. When you request a file (and image, a PDF, etc.) the |
| 36 | +response will be automatically converted to a base64 encoded string. |
| 37 | + |
| 38 | +## Working with Buffers |
| 39 | + |
| 40 | +You can also work directly with buffers in OpenFn job code via code like: |
| 41 | + |
| 42 | +```js |
| 43 | +fn(state => { |
| 44 | + const encoded = Buffer.from(state.data.myBase64string, 'base64'); |
| 45 | + return { ...state, encodedImage }; |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +or... |
| 50 | + |
| 51 | +```js |
| 52 | +fn(state => { |
| 53 | + const decoded = state.data.myBuffer.toString('base64'); |
| 54 | + return { ...state, decoded }; |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +## Summary |
| 59 | + |
| 60 | +Most use cases should **_Just Work ™️_**. If you have a specific need involving |
| 61 | +large file sizes or high volumes and you need to process images, rather than |
| 62 | +just moving them from place to place, you might need to make a change to your |
| 63 | +adaptor. |
0 commit comments