Skip to content

Commit d701ee2

Browse files
image handling (#716)
1 parent 9e0342c commit d701ee2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/jobs/image-handling.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.

sidebars-main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = {
4545
'jobs/javascript',
4646
'jobs/job-examples',
4747
'jobs/job-snippets',
48+
'jobs/image-handling',
4849
'build-for-developers/security-for-devs',
4950
],
5051
},

0 commit comments

Comments
 (0)