From b7a6839d021d36b25274b2ad61dca883ba3caa2e Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Fri, 2 Feb 2024 10:38:44 -0500 Subject: [PATCH] fix: check if image is file path rather than b64 (#37) --- src/index.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 28b3f58..1449ff7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import * as utils from './utils.js' import 'whatwg-fetch' -import { promises, createReadStream } from 'fs' +import fs, { promises, createReadStream } from 'fs' import { join, resolve, dirname } from 'path' import { createHash } from 'crypto' import { homedir } from 'os' @@ -87,14 +87,17 @@ export class Ollama { const result = Buffer.from(image).toString('base64') return result } - const base64Pattern = /^[A-Za-z0-9+/]+={1,2}$/ // detect by checking for equals signs at the end - if (base64Pattern.test(image)) { - // the string is already base64 encoded - return image + try { + if (fs.existsSync(image)) { + // this is a filepath, read the file and convert it to base64 + const fileBuffer = await promises.readFile(resolve(image)) + return Buffer.from(fileBuffer).toString('base64') + } + } catch { + // continue } - // this is a filepath, read the file and convert it to base64 - const fileBuffer = await promises.readFile(resolve(image)) - return Buffer.from(fileBuffer).toString('base64') + // the string may be base64 encoded + return image } private async parseModelfile(