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

refactor: update uploadFile method to accept both file paths and Buffers #195

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/dry-dodos-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": patch
---

the code was refactored to accept both file paths and Buffers as inputs for uploading files with metadata
4 changes: 3 additions & 1 deletion common/api-review/generative-ai-server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

```ts

/// <reference types="node" />

// @public
export interface CachedContent extends CachedContentBase {
createTime?: string;
Expand Down Expand Up @@ -357,7 +359,7 @@ export class GoogleAIFileManager {
deleteFile(fileId: string): Promise<void>;
getFile(fileId: string): Promise<FileMetadataResponse>;
listFiles(listParams?: ListParams): Promise<ListFilesResponse>;
uploadFile(filePath: string, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
uploadFile(fileInput: string | Buffer, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
}

// @public
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/server/generative-ai.googleaifilemanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export declare class GoogleAIFileManager
| [deleteFile(fileId)](./generative-ai.googleaifilemanager.deletefile.md) | | Delete file with given ID |
| [getFile(fileId)](./generative-ai.googleaifilemanager.getfile.md) | | Get metadata for file with given ID |
| [listFiles(listParams)](./generative-ai.googleaifilemanager.listfiles.md) | | List all uploaded files |
| [uploadFile(filePath, fileMetadata)](./generative-ai.googleaifilemanager.uploadfile.md) | | Upload a file |
| [uploadFile(fileInput, fileMetadata)](./generative-ai.googleaifilemanager.uploadfile.md) | | Upload a file |

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Upload a file
**Signature:**

```typescript
uploadFile(filePath: string, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
uploadFile(fileInput: string | Buffer, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| filePath | string | |
| fileInput | string \| Buffer | |
| fileMetadata | [FileMetadata](./generative-ai.filemetadata.md) | |

**Returns:**
Expand Down
14 changes: 11 additions & 3 deletions packages/main/src/server/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { RequestOptions } from "../../types";
import { readFileSync } from "fs";
import { readFile } from "fs/promises";
import { FilesRequestUrl, getHeaders, makeServerRequest } from "./request";
import {
FileMetadata,
Expand Down Expand Up @@ -51,10 +51,18 @@ export class GoogleAIFileManager {
* Upload a file
*/
async uploadFile(
filePath: string,
fileInput: string | Buffer,
fileMetadata: FileMetadata,
): Promise<UploadFileResponse> {
const file = readFileSync(filePath);
let file: Buffer | string;

if (typeof fileInput === "string") {
file = await readFile(fileInput);
} else if (Buffer.isBuffer(fileInput)) {
file = fileInput;
} else {
throw Error("fileInput must be filePath or buffer");
}
const url = new FilesRequestUrl(
RpcTask.UPLOAD,
this.apiKey,
Expand Down