-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[vertex] Add PDF/plein texts support (#1520)
* [vertex] Add PDF support * [vertex] Fix lint * [vertex] Add support for text/plain
- Loading branch information
1 parent
db229c6
commit cc5dfd4
Showing
6 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { MessageFile } from "$lib/types/Message"; | ||
import { z } from "zod"; | ||
|
||
export interface FileProcessorOptions<TMimeType extends string = string> { | ||
supportedMimeTypes: TMimeType[]; | ||
maxSizeInMB: number; | ||
} | ||
|
||
export type ImageProcessor<TMimeType extends string = string> = (file: MessageFile) => Promise<{ | ||
file: Buffer; | ||
mime: TMimeType; | ||
}>; | ||
|
||
export const createDocumentProcessorOptionsValidator = <TMimeType extends string = string>( | ||
defaults: FileProcessorOptions<TMimeType> | ||
) => { | ||
return z | ||
.object({ | ||
supportedMimeTypes: z | ||
.array( | ||
z.enum<string, [TMimeType, ...TMimeType[]]>([ | ||
defaults.supportedMimeTypes[0], | ||
...defaults.supportedMimeTypes.slice(1), | ||
]) | ||
) | ||
.default(defaults.supportedMimeTypes), | ||
maxSizeInMB: z.number().positive().default(defaults.maxSizeInMB), | ||
}) | ||
.default(defaults); | ||
}; | ||
|
||
export type DocumentProcessor<TMimeType extends string = string> = (file: MessageFile) => { | ||
file: Buffer; | ||
mime: TMimeType; | ||
}; | ||
|
||
export function makeDocumentProcessor<TMimeType extends string = string>( | ||
options: FileProcessorOptions<TMimeType> | ||
): DocumentProcessor<TMimeType> { | ||
return (file) => { | ||
const { supportedMimeTypes, maxSizeInMB } = options; | ||
const { mime, value } = file; | ||
|
||
const buffer = Buffer.from(value, "base64"); | ||
|
||
const tooLargeInBytes = buffer.byteLength > maxSizeInMB * 1000 * 1000; | ||
|
||
if (tooLargeInBytes) { | ||
throw Error("Document is too large"); | ||
} | ||
|
||
const outputMime = validateMimeType(supportedMimeTypes, mime); | ||
|
||
return { file: buffer, mime: outputMime }; | ||
}; | ||
} | ||
|
||
const validateMimeType = <T extends readonly string[]>( | ||
supportedMimes: T, | ||
mime: string | ||
): T[number] => { | ||
if (!supportedMimes.includes(mime)) { | ||
const supportedMimesStr = supportedMimes.join(", "); | ||
|
||
throw Error(`Mimetype "${mime}" not found in supported mimes: ${supportedMimesStr}`); | ||
} | ||
|
||
return mime; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters