Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Generate text and images via the reverse-engineered Gemini Web API with multi-turn conversation support.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/gemini-webapi/utils/upload-file.ts
1import fs from 'node:fs';2import path from 'node:path';3import { readFile } from 'node:fs/promises';45import { Endpoint, Headers } from '../constants.js';67export async function upload_file(file: string, _proxy?: string | null): Promise<string> {8if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {9throw new Error(`${file} is not a valid file.`);10}1112const filename = path.basename(file);13const content = await readFile(file);1415const form = new FormData();16form.append('file', new Blob([content]), filename);1718const res = await fetch(Endpoint.UPLOAD, {19method: 'POST',20headers: { ...Headers.UPLOAD },21body: form,22redirect: 'follow',23});2425if (!res.ok) {26throw new Error(`Upload failed: ${res.status} ${res.statusText}`);27}2829return await res.text();30}3132export function parse_file_name(file: string): string {33if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {34throw new Error(`${file} is not a valid file.`);35}36return path.basename(file);37}3839export const uploadFile = upload_file;40export const parseFileName = parse_file_name;4142