Skip to content

Support for GCS URIs in image resolver #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-carpets-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@nutshelllabs/image': major
---

Support for GS URIs in image resolving
1 change: 1 addition & 0 deletions packages/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@babel/runtime": "^7.20.13",
"@google-cloud/storage": "^7.15.0",
"@nutshelllabs/png-js": "^2.2.1",
"cross-fetch": "^3.1.5"
},
Expand Down
47 changes: 44 additions & 3 deletions packages/image/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ import createCache from './cache';

export const IMAGE_CACHE = createCache({ limit: 30 });

let GCS;
if (!BROWSER) {
// eslint-disable-next-line global-require
GCS = require('@google-cloud/storage');
}

const createGcsStorage = emulatorHost => {
if (emulatorHost) {
return new GCS.Storage({
apiEndpoint: `http://${emulatorHost}`,
customEndpoint: true,
});
}

return new GCS.Storage();
};

let storage;
export const configureStorage = (
emulatorHost = process.env.GCS_EMULATOR_HOST,
) => {
storage = createGcsStorage(emulatorHost);
};

const getAbsoluteLocalPath = src => {
if (BROWSER) {
throw new Error('Cannot check local paths in client-side environment');
Expand Down Expand Up @@ -143,13 +167,30 @@ const getImageFormat = body => {
return extension;
};

const resolveImageFromGcs = async uri => {
if (BROWSER) {
throw new Error('Cannot download from GCS in client-side environment');
}

if (!storage) {
configureStorage();
}

const response = await GCS.File.from(uri, storage).download();
return response[0];
};

const resolveImageFromUrl = async src => {
const { uri, body, headers, method = 'GET' } = src;

const data =
!BROWSER && getAbsoluteLocalPath(uri)
? await fetchLocalFile(uri)
let data;
if (!BROWSER && getAbsoluteLocalPath(uri)) {
data = await fetchLocalFile(uri);
} else {
data = uri.startsWith('gs://')
? await resolveImageFromGcs(uri)
: await fetchRemoteFile(uri, { body, headers, method });
}

const extension = getImageFormat(data);

Expand Down
14 changes: 13 additions & 1 deletion packages/image/tests/resolve.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';

import resolveImage, { IMAGE_CACHE } from '../src/resolve';
import resolveImage, { IMAGE_CACHE, configureStorage } from '../src/resolve';

const jpgImageUrl = 'https://react-pdf.org/static/images/quijote1.jpg';
const pngImageUrl = 'https://react-pdf.org/static/images/quijote2.png';
Expand Down Expand Up @@ -70,6 +70,18 @@ describe('image resolveImage', () => {
expect(image.height).toBeGreaterThan(0);
});

test.skip('[Manual] Should render a png image from GCS', async () => {
configureStorage('localhost:4443');

const gcsImageUrl =
'gs://irisflow_input/1/1/brokerage_statement/e3eec7c7-96ff-4825-9485-06decad51544/test.jpg';
const image = await resolveImage({ uri: gcsImageUrl });

expect(image.data).toBeTruthy();
expect(image.width).toBeGreaterThan(0);
expect(image.height).toBeGreaterThan(0);
});

test('Should render a local image from src object', async () => {
const image = await resolveImage({
uri: './packages/layout/tests/assets/test.jpg',
Expand Down
Loading
Loading