Skip to content

Commit

Permalink
fix: check if image is file path rather than b64
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMacD committed Feb 2, 2024
1 parent 0cd1bdd commit f2138f4
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f2138f4

Please sign in to comment.