Skip to content
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

feat: add the support of URLs for the nearImage() method #252

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
64 changes: 64 additions & 0 deletions src/utils/base64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { httpClient } from '../connection/http.js';
import { downloadImageFromURLAsBase64 } from './base64.js';

jest.mock('../connection/http.js');

describe('downloadImageFromURLAsBase64()', () => {
const mockHttpClient = {
externalGet: jest.fn(),
};

beforeEach(() => {
jest.clearAllMocks();
(httpClient as jest.Mock).mockReturnValue(mockHttpClient);
});

it('should convert a downloaded image to base64', async () => {
const mockUrl = 'https://example.com/image.jpg';
const mockImageData = Buffer.from('image binary data');

mockHttpClient.externalGet.mockResolvedValue(mockImageData);

const result = await downloadImageFromURLAsBase64(mockUrl);

expect(result).toBe(mockImageData.toString('base64'));
expect(httpClient).toHaveBeenCalledWith({ headers: { 'Content-Type': 'image/*' }, host: '' });
expect(mockHttpClient.externalGet).toHaveBeenCalledWith(mockUrl);
});

it('should throw an error if the URL is invalid', async () => {
const invalidUrl = 'invalid-url';

await expect(downloadImageFromURLAsBase64(invalidUrl)).rejects.toThrow('Invalid URL');
});

it('should throw an error if the image download fails', async () => {
const mockUrl = 'https://example.com/image.jpg';

mockHttpClient.externalGet.mockRejectedValue(new Error('Network error'));

await expect(downloadImageFromURLAsBase64(mockUrl)).rejects.toThrow('Failed to download image from URL');
expect(httpClient).toHaveBeenCalledWith({ headers: { 'Content-Type': 'image/*' }, host: '' });
expect(mockHttpClient.externalGet).toHaveBeenCalledWith(mockUrl);
});

it('should handle empty response data gracefully', async () => {
const mockUrl = 'https://example.com/image.jpg';

mockHttpClient.externalGet.mockResolvedValue(Buffer.alloc(0));

const result = await downloadImageFromURLAsBase64(mockUrl);

expect(result).toBe('');
expect(httpClient).toHaveBeenCalledWith({ headers: { 'Content-Type': 'image/*' }, host: '' });
expect(mockHttpClient.externalGet).toHaveBeenCalledWith(mockUrl);
});

it('should throw an error if the response is not a buffer', async () => {
const mockUrl = 'wrong-url.com';

mockHttpClient.externalGet.mockResolvedValue('not a buffer');

await expect(downloadImageFromURLAsBase64(mockUrl)).rejects.toThrow('Invalid URL');
});
});
38 changes: 37 additions & 1 deletion src/utils/base64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import { httpClient } from '../connection/http.js';

const isFilePromise = (file: string | Buffer): Promise<boolean> =>
new Promise((resolve, reject) => {
Expand All @@ -22,6 +23,39 @@ const isFilePromise = (file: string | Buffer): Promise<boolean> =>
});
});

const isUrl = (file: string | Buffer): boolean => {
tsmith023 marked this conversation as resolved.
Show resolved Hide resolved
if (typeof file !== 'string') return false;
try {
const url = new URL(file);
return !!url;
} catch {
return false;
}
};

export const downloadImageFromURLAsBase64 = async (url: string): Promise<string> => {
if (!isUrl(url)) {
throw new Error('Invalid URL');
}

try {
const client = httpClient({
headers: { 'Content-Type': 'image/*' },
host: '',
});

const response = await client.externalGet(url);

if (!Buffer.isBuffer(response)) {
throw new Error('Response is not a buffer');
}

return response.toString('base64');
} catch (error) {
throw new Error(`Failed to download image from URL: ${url}`);
}
};

const isBuffer = (file: string | Buffer): file is Buffer => file instanceof Buffer;

const fileToBase64 = (file: string | Buffer): Promise<string> =>
Expand All @@ -37,14 +71,16 @@ const fileToBase64 = (file: string | Buffer): Promise<string> =>
})
: isBuffer(file)
? Promise.resolve(file.toString('base64'))
: isUrl(file)
? downloadImageFromURLAsBase64(file)
: Promise.resolve(file)
);

/**
* This function converts a file buffer into a base64 string so that it can be
* sent to Weaviate and stored as a media field.
*
* @param {string | Buffer} file The media to convert either as a base64 string, a file path string, or as a buffer. If you passed a base64 string, the function does nothing and returns the string as is.
* @param {string | Buffer} file The media to convert either as a base64 string, a file path string, an url, or as a buffer. If you passed a base64 string, the function does nothing and returns the string as is.
* @returns {string} The base64 string
*/
export const toBase64FromMedia = (media: string | Buffer): Promise<string> => fileToBase64(media);
Loading