Skip to content

Commit 9251688

Browse files
committed
fix: honor --timeout for temporary OSS uploads
1 parent 8bbbbc7 commit 9251688

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎packages/core/src/client/client.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export class Client {
154154
return resolveFileUrl(source, this.requireApi().token, model, {
155155
...opts,
156156
identity: this.deps.identity,
157+
timeout: this.deps.settings.timeout,
157158
});
158159
}
159160

‎packages/core/src/files/upload.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ async function getUploadPolicy(
6262
return body.data;
6363
}
6464

65+
const DEFAULT_UPLOAD_TIMEOUT = 120;
66+
6567
/**
6668
* Step 2: Upload the file to OSS using the policy.
6769
*/
6870
async function uploadToOSS(
6971
policy: UploadPolicy,
7072
filePath: string,
7173
signal?: AbortSignal,
74+
timeoutSeconds: number = DEFAULT_UPLOAD_TIMEOUT,
7275
): Promise<string> {
7376
const fileName = basename(filePath);
7477
const key = `${policy.upload_dir}/${fileName}`;
@@ -85,7 +88,7 @@ async function uploadToOSS(
8588
form.append("success_action_status", "200");
8689
form.append("file", new Blob([fileData]), fileName);
8790

88-
const uploadSignal = combineWithTimeout(120_000, signal);
91+
const uploadSignal = combineWithTimeout(timeoutSeconds * 1000, signal);
8992
const res = await fetch(policy.upload_host, {
9093
method: "POST",
9194
body: form,
@@ -109,6 +112,7 @@ export interface UploadOptions {
109112
filePath: string;
110113
identity: TrackingIdentity;
111114
signal?: AbortSignal;
115+
timeout?: number;
112116
}
113117

114118
const IMAGE_MIME_TYPES: Readonly<Record<string, string>> = {
@@ -159,7 +163,7 @@ export function redactDataUri(input: string): string {
159163
* The URL is valid for 48 hours.
160164
*/
161165
export async function uploadFile(opts: UploadOptions): Promise<string> {
162-
const { apiKey, model, filePath, identity, signal } = opts;
166+
const { apiKey, model, filePath, identity, signal, timeout } = opts;
163167

164168
if (!existsSync(filePath)) {
165169
throw new BailianError(`File not found: ${filePath}`, ExitCode.USAGE);
@@ -171,7 +175,7 @@ export async function uploadFile(opts: UploadOptions): Promise<string> {
171175
}
172176

173177
const policy = await getUploadPolicy(apiKey, model, identity, signal);
174-
return uploadToOSS(policy, filePath, signal);
178+
return uploadToOSS(policy, filePath, signal, timeout);
175179
}
176180

177181
/**
@@ -192,7 +196,7 @@ export async function resolveFileUrl(
192196
input: string,
193197
apiKey: string,
194198
model: string,
195-
opts: { identity: TrackingIdentity; signal?: AbortSignal },
199+
opts: { identity: TrackingIdentity; signal?: AbortSignal; timeout?: number },
196200
): Promise<string> {
197201
if (!isLocalFile(input)) return input;
198202
return uploadFile({
@@ -201,6 +205,7 @@ export async function resolveFileUrl(
201205
filePath: input,
202206
identity: opts.identity,
203207
signal: opts.signal,
208+
timeout: opts.timeout,
204209
});
205210
}
206211

0 commit comments

Comments
 (0)