Skip to content

Commit 5182cd5

Browse files
authored
feat: preserve content type in versions (#177)
1 parent a665d4f commit 5182cd5

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

src/storage/version/put.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ export function getContentLength(body) {
3636
}
3737

3838
export async function putVersion(config, {
39-
Bucket, Org, Body, ID, Version, Ext, Metadata, ContentLength,
39+
Bucket, Org, Body, ID, Version, Ext, Metadata, ContentLength, ContentType,
4040
}, noneMatch = true) {
4141
const length = ContentLength ?? getContentLength(Body);
4242

4343
const client = noneMatch ? ifNoneMatch(config) : new S3Client(config);
4444
const input = {
45-
Bucket, Key: `${Org}/.da-versions/${ID}/${Version}.${Ext}`, Body, Metadata, ContentLength: length,
45+
Bucket, Key: `${Org}/.da-versions/${ID}/${Version}.${Ext}`, Body, Metadata, ContentLength: length, ContentType,
4646
};
4747
const command = new PutObjectCommand(input);
4848
try {
@@ -137,6 +137,7 @@ export async function putObjectWithVersion(env, daCtx, update, body, guid) {
137137
Org: daCtx.org,
138138
Body: (body || storeBody ? current.body : ''),
139139
ContentLength: (body || storeBody ? current.contentLength : undefined),
140+
ContentType: current.contentType,
140141
ID,
141142
Version,
142143
Ext: daCtx.ext,

test/storage/version/put.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,91 @@ describe('Version Put', () => {
779779
const resp3 = await putVersion({}, { Body: 'hello'});
780780
assert.equal(500, resp3.status);
781781
});
782+
783+
it('Test putVersion preserves ContentType', async () => {
784+
const sentCommands = [];
785+
const mockS3Client = {
786+
async send(cmd) {
787+
sentCommands.push(cmd);
788+
return {
789+
$metadata: { httpStatusCode: 200 }
790+
};
791+
}
792+
};
793+
794+
const { putVersion } = await esmock('../../../src/storage/version/put.js', {
795+
'../../../src/storage/utils/version.js': {
796+
ifNoneMatch: () => mockS3Client,
797+
},
798+
});
799+
800+
const testParams = {
801+
Bucket: 'test-bucket',
802+
Org: 'test-org',
803+
Body: 'test content',
804+
ID: 'test-id',
805+
Version: 'test-version',
806+
Ext: 'html',
807+
Metadata: { test: 'metadata' },
808+
ContentLength: 12,
809+
ContentType: 'text/html'
810+
};
811+
812+
await putVersion({}, testParams);
813+
814+
assert.strictEqual(sentCommands.length, 1);
815+
const putCommand = sentCommands[0];
816+
assert.strictEqual(putCommand.input.Bucket, 'test-bucket');
817+
assert.strictEqual(putCommand.input.Key, 'test-org/.da-versions/test-id/test-version.html');
818+
assert.strictEqual(putCommand.input.Body, 'test content');
819+
assert.strictEqual(putCommand.input.ContentLength, 12);
820+
assert.strictEqual(putCommand.input.ContentType, 'text/html');
821+
assert.deepStrictEqual(putCommand.input.Metadata, { test: 'metadata' });
822+
});
823+
824+
it('Test putObjectWithVersion passes ContentType to putVersion', async () => {
825+
const sentCommands = [];
826+
const mockS3Client = {
827+
async send(cmd) {
828+
sentCommands.push(cmd);
829+
return {
830+
$metadata: { httpStatusCode: 200 }
831+
};
832+
}
833+
};
834+
835+
const mockGetObject = async () => ({
836+
status: 200,
837+
body: 'test body',
838+
contentLength: 9,
839+
contentType: 'text/plain',
840+
metadata: { existing: 'metadata' }
841+
});
842+
843+
const { putObjectWithVersion } = await esmock('../../../src/storage/version/put.js', {
844+
'../../../src/storage/object/get.js': {
845+
default: mockGetObject
846+
},
847+
'../../../src/storage/utils/version.js': {
848+
ifNoneMatch: () => mockS3Client,
849+
generateId: () => 'generated-id',
850+
generateVersion: () => 'generated-version'
851+
},
852+
});
853+
854+
const env = {};
855+
const daCtx = {
856+
org: 'test-org',
857+
ext: 'txt',
858+
users: [{ email: 'test@example.com' }]
859+
};
860+
861+
await putObjectWithVersion(env, daCtx, { key: 'test-file.txt' }, 'test body', 'test-guid');
862+
863+
assert.strictEqual(sentCommands.length, 1);
864+
const putCommand = sentCommands[0];
865+
assert.strictEqual(putCommand.input.ContentType, 'text/plain');
866+
assert.strictEqual(putCommand.input.Body, 'test body');
867+
assert.strictEqual(putCommand.input.ContentLength, 9);
868+
});
782869
});

0 commit comments

Comments
 (0)