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

fix: check if image is file path rather than b64 #37

Merged
merged 1 commit into from
Feb 2, 2024
Merged
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
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 @@ -44,7 +44,7 @@

private async processStreamableRequest<T extends object>(
endpoint: string,
request: { stream?: boolean } & Record<string, any>,

Check warning on line 47 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 47 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 47 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
): Promise<T | AsyncGenerator<T>> {
request.stream = request.stream ?? false
const response = await utils.post(this.fetch, `${this.config.host}/api/${endpoint}`, {
Expand All @@ -66,7 +66,7 @@
yield message
// message will be done in the case of chat and generate
// message will be success in the case of a progress response (pull, push, create)
if ((message as any).done || (message as any).status === 'success') {

Check warning on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type

Check warning on line 69 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
return
}
}
Expand All @@ -74,7 +74,7 @@
})()
} else {
const message = await itr.next()
if (!message.value.done && (message.value as any).status !== 'success') {

Check warning on line 77 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 77 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 77 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
throw new Error('Expected a completed response.')
}
return message.value
Expand All @@ -87,14 +87,17 @@
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
Loading