Skip to content

Commit 819501f

Browse files
authoredMay 9, 2024··
Fix bug when no file name is provided for upload (#135)
1 parent 6227ee7 commit 819501f

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed
 

‎.changeset/dirty-pillows-vanish.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@google/generative-ai": patch
3+
---
4+
5+
Fix a bug that caused file uploads to be named "undefined" if no file name is provided.

‎packages/main/src/files/file-manager.test.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
* limitations under the License.
1616
*/
1717
import { expect, use } from "chai";
18-
import { GoogleAIFileManager } from "./file-manager";
18+
import { GoogleAIFileManager, getUploadMetadata } from "./file-manager";
1919
import * as sinonChai from "sinon-chai";
2020
import * as chaiAsPromised from "chai-as-promised";
2121
import { restore, stub } from "sinon";
2222
import * as request from "./request";
2323
import { FilesTask } from "./constants";
2424
import { DEFAULT_API_VERSION } from "../requests/request";
25+
import { FileMetadata } from "./types";
2526

2627
use(sinonChai);
2728
use(chaiAsPromised);
@@ -254,4 +255,38 @@ describe("GoogleAIFileManager", () => {
254255
"Invalid fileId",
255256
);
256257
});
258+
259+
describe("getUploadMetadata", () => {
260+
it("getUploadMetadata with only mimeType", () => {
261+
const uploadMetadata = getUploadMetadata({ mimeType: "image/jpeg" });
262+
expect(uploadMetadata.mimeType).to.equal("image/jpeg");
263+
expect(uploadMetadata.displayName).be.undefined;
264+
expect(uploadMetadata.name).be.undefined;
265+
});
266+
it("getUploadMetadata with no mimeType", () => {
267+
expect(() => getUploadMetadata({} as FileMetadata)).to.throw(
268+
"Must provide a mimeType.",
269+
);
270+
});
271+
it("getUploadMetadata with all fields defined", () => {
272+
const uploadMetadata = getUploadMetadata({
273+
mimeType: "image/jpeg",
274+
displayName: "display name",
275+
name: "filename",
276+
});
277+
expect(uploadMetadata.mimeType).to.equal("image/jpeg");
278+
expect(uploadMetadata.displayName).to.equal("display name");
279+
expect(uploadMetadata.name).to.equal("files/filename");
280+
});
281+
it("getUploadMetadata with full file path", () => {
282+
const uploadMetadata = getUploadMetadata({
283+
mimeType: "image/jpeg",
284+
displayName: "display name",
285+
name: "custom/path/filename",
286+
});
287+
expect(uploadMetadata.mimeType).to.equal("image/jpeg");
288+
expect(uploadMetadata.displayName).to.equal("display name");
289+
expect(uploadMetadata.name).to.equal("custom/path/filename");
290+
});
291+
});
257292
});

‎packages/main/src/files/file-manager.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import {
2626
UploadFileResponse,
2727
} from "./types";
2828
import { FilesTask } from "./constants";
29-
import { GoogleGenerativeAIError } from "../errors";
29+
import {
30+
GoogleGenerativeAIError,
31+
GoogleGenerativeAIRequestInputError,
32+
} from "../errors";
3033

3134
// Internal type, metadata sent in the upload
3235
export interface UploadMetadata {
@@ -66,13 +69,7 @@ export class GoogleAIFileManager {
6669
`multipart/related; boundary=${boundary}`,
6770
);
6871

69-
const uploadMetadata: FileMetadata = {
70-
mimeType: fileMetadata.mimeType,
71-
displayName: fileMetadata.displayName,
72-
name: fileMetadata.name?.includes("/")
73-
? fileMetadata.name
74-
: `files/${fileMetadata.name}`,
75-
};
72+
const uploadMetadata = getUploadMetadata(fileMetadata);
7673

7774
// Multipart formatting code taken from @firebase/storage
7875
const metadataString = JSON.stringify({ file: uploadMetadata });
@@ -169,3 +166,20 @@ function generateBoundary(): string {
169166
}
170167
return str;
171168
}
169+
170+
export function getUploadMetadata(inputMetadata: FileMetadata): FileMetadata {
171+
if (!inputMetadata.mimeType) {
172+
throw new GoogleGenerativeAIRequestInputError("Must provide a mimeType.");
173+
}
174+
const uploadMetadata: FileMetadata = {
175+
mimeType: inputMetadata.mimeType,
176+
displayName: inputMetadata.displayName,
177+
};
178+
179+
if (inputMetadata.name) {
180+
uploadMetadata.name = inputMetadata.name.includes("/")
181+
? inputMetadata.name
182+
: `files/${inputMetadata.name}`;
183+
}
184+
return uploadMetadata;
185+
}

0 commit comments

Comments
 (0)
Please sign in to comment.