Skip to content

Commit 1c27fd3

Browse files
committed
writer contentDisposition
1 parent 8bc550f commit 1c27fd3

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/bun.js/webcore/Blob.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,13 +2636,22 @@ pub fn getWriter(
26362636
}
26372637
}
26382638
}
2639+
var content_disposition_str: ?ZigString.Slice = null;
2640+
defer if (content_disposition_str) |cd| cd.deinit();
2641+
if (try options.getTruthy(globalThis, "contentDisposition")) |content_disposition| {
2642+
if (!content_disposition.isString()) {
2643+
return globalThis.throwInvalidArgumentType("write", "options.contentDisposition", "string");
2644+
}
2645+
content_disposition_str = try content_disposition.toSlice(globalThis, bun.default_allocator);
2646+
}
26392647
const credentialsWithOptions = try s3.getCredentialsWithOptions(options, globalThis);
26402648
return try S3.writableStream(
26412649
credentialsWithOptions.credentials.dupe(),
26422650
path,
26432651
globalThis,
26442652
credentialsWithOptions.options,
26452653
this.contentTypeOrMimeType(),
2654+
if (content_disposition_str) |cd| cd.slice() else null,
26462655
proxy_url,
26472656
credentialsWithOptions.storage_class,
26482657
);
@@ -2654,6 +2663,7 @@ pub fn getWriter(
26542663
globalThis,
26552664
.{},
26562665
this.contentTypeOrMimeType(),
2666+
null,
26572667
proxy_url,
26582668
null,
26592669
);

src/s3/client.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ pub fn writableStream(
254254
globalThis: *jsc.JSGlobalObject,
255255
options: MultiPartUploadOptions,
256256
content_type: ?[]const u8,
257+
content_disposition: ?[]const u8,
257258
proxy: ?[]const u8,
258259
storage_class: ?StorageClass,
259260
) bun.JSError!jsc.JSValue {
@@ -297,6 +298,7 @@ pub fn writableStream(
297298
.path = bun.handleOom(bun.default_allocator.dupe(u8, path)),
298299
.proxy = if (proxy_url.len > 0) bun.handleOom(bun.default_allocator.dupe(u8, proxy_url)) else "",
299300
.content_type = if (content_type) |ct| bun.handleOom(bun.default_allocator.dupe(u8, ct)) else null,
301+
.content_disposition = if (content_disposition) |cd| bun.handleOom(bun.default_allocator.dupe(u8, cd)) else null,
300302
.storage_class = storage_class,
301303

302304
.callback = @ptrCast(&Wrapper.callback),

test/js/bun/s3/s3.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,19 @@ for (let credentials of allCredentials) {
431431
expect(response.headers.get("content-disposition")).toBe('attachment; filename="report.pdf"');
432432
}
433433
});
434+
it("should be able to set content-disposition in writer", async () => {
435+
await using tmpfile = await tmp();
436+
{
437+
const s3file = bucket.file(tmpfile.name, options!);
438+
const writer = s3file.writer({
439+
contentDisposition: 'attachment; filename="test.txt"',
440+
});
441+
writer.write("Hello Bun!!");
442+
await writer.end();
443+
const response = await fetch(s3file.presign());
444+
expect(response.headers.get("content-disposition")).toBe('attachment; filename="test.txt"');
445+
}
446+
});
434447

435448
it("should be able to upload large files using bucket.write + readable Request", async () => {
436449
await using tmpfile = await tmp();

0 commit comments

Comments
 (0)