Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ allows content-type for asset upload #82

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/stack/asset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ export function createFormData (data) {
formData.append('asset[title]', data.title)
}
const uploadStream = createReadStream(data.upload)
formData.append('asset[upload]', uploadStream)
if (typeof data.content_type === 'string') {
formData.append('asset[upload]', uploadStream, { contentType: data.content_type })
} else {
formData.append('asset[upload]', uploadStream)
}
return formData
}
}
24 changes: 24 additions & 0 deletions test/api/asset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ describe('Assets api Test', () => {
})
.catch(done)
})

it('Asset Upload in folder with contenttype', done => {
const asset = {
upload: path.join(__dirname, './mock/berries.jfif'),
title: 'customasset2 in Folder',
description: 'Custom Asset Desc in Folder',
parent_uid: folderUID,
tags: 'folder',
content_type: 'image/jpeg'
}
makeAsset().create(asset)
.then((asset) => {
publishAssetUID = asset.uid
expect(asset.uid).to.be.not.equal(null)
expect(asset.url).to.be.not.equal(null)
expect(asset.filename).to.be.equal('berries.jfif')
expect(asset.title).to.be.equal('customasset2 in Folder')
expect(asset.description).to.be.equal('Custom Asset Desc in Folder')
expect(asset.content_type).to.be.equal('image/jpeg')
expect(asset.parent_uid).to.be.equal(folderUID)
done()
})
.catch(done)
})
it('Replace asset ', done => {
const asset = {
upload: path.join(__dirname, './mock/upload.html')
Expand Down
Binary file added test/api/mock/berries.jfif
Binary file not shown.
5 changes: 3 additions & 2 deletions test/unit/asset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ describe('Contentstack Asset test', () => {
title: 'customasset',
description: 'Custom Asset Desc',
tags: ['Custom'],
parent_uid: 'UID'
parent_uid: 'UID',
content_type: 'text/html'
}
const form = createFormData(assetUpload)()
var boundary = form.getBoundary()
Expand Down Expand Up @@ -322,7 +323,7 @@ function makeAsset (data) {

function checkAsset (asset) {
checkSystemFields(asset)
expect(asset.content_type).to.be.equal('image/png')
expect(asset.content_type).to.be.equal('text/html')
expect(asset.file_size).to.be.equal('42670')
expect(asset.tags.length).to.be.equal(0)
expect(asset.filename).to.be.equal('file.png')
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mock/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const releaseMock = {
const assetMock = {
...systemFieldsMock,
...systemFieldsUserMock,
content_type: 'image/png',
content_type: 'text/html',
file_size: '42670',
tags: [],
filename: 'file.png',
Expand Down
71 changes: 35 additions & 36 deletions test/unit/user-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,25 @@ describe('Contentstack User test', () => {
})

user.getTasks()
.then((userTasks) => {
const assignment = userTasks.asassignments
expect(assignment.api_key).to.be.equal(stackHeadersMock.api_key)
expect(assignment.content_type).to.be.equal("CT_UID")
expect(assignment.entry_uid).to.be.equal("ETR_UID")
expect(assignment.locale).to.be.equal("en-us")
expect(assignment.org_uid).to.be.equal("orgUID")
expect(assignment.type).to.be.equal("workflow_stage")
expect(assignment.entry_locale).to.be.equal("en-us")
expect(assignment.version).to.be.equal(1)
expect(assignment.assigned_to[0]).to.be.equal("user_UID")
expect(assignment.assigned_at).to.be.equal("assign_date")
expect(assignment.assigned_by).to.be.equal("assign_by")
expect(assignment.due_date).to.be.equal("due_date")
done()
})
.catch(done)
.then((userTasks) => {
const assignment = userTasks.asassignments
expect(assignment.api_key).to.be.equal(stackHeadersMock.api_key)
expect(assignment.content_type).to.be.equal('CT_UID')
expect(assignment.entry_uid).to.be.equal('ETR_UID')
expect(assignment.locale).to.be.equal('en-us')
expect(assignment.org_uid).to.be.equal('orgUID')
expect(assignment.type).to.be.equal('workflow_stage')
expect(assignment.entry_locale).to.be.equal('en-us')
expect(assignment.version).to.be.equal(1)
expect(assignment.assigned_to[0]).to.be.equal('user_UID')
expect(assignment.assigned_at).to.be.equal('assign_date')
expect(assignment.assigned_by).to.be.equal('assign_by')
expect(assignment.due_date).to.be.equal('due_date')
done()
})
.catch(done)
})


it('User get task with params test', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/user/assignments').reply(200, {
Expand All @@ -194,24 +193,24 @@ describe('Contentstack User test', () => {
}
})

user.getTasks({sort: 'sort'})
.then((userTasks) => {
const assignment = userTasks.asassignments
expect(assignment.api_key).to.be.equal(stackHeadersMock.api_key)
expect(assignment.content_type).to.be.equal("CT_UID")
expect(assignment.entry_uid).to.be.equal("ETR_UID")
expect(assignment.locale).to.be.equal("en-us")
expect(assignment.org_uid).to.be.equal("orgUID")
expect(assignment.type).to.be.equal("workflow_stage")
expect(assignment.entry_locale).to.be.equal("en-us")
expect(assignment.version).to.be.equal(1)
expect(assignment.assigned_to[0]).to.be.equal("user_UID")
expect(assignment.assigned_at).to.be.equal("assign_date")
expect(assignment.assigned_by).to.be.equal("assign_by")
expect(assignment.due_date).to.be.equal("due_date")
done()
})
.catch(done)
user.getTasks({ sort: 'sort' })
.then((userTasks) => {
const assignment = userTasks.asassignments
expect(assignment.api_key).to.be.equal(stackHeadersMock.api_key)
expect(assignment.content_type).to.be.equal('CT_UID')
expect(assignment.entry_uid).to.be.equal('ETR_UID')
expect(assignment.locale).to.be.equal('en-us')
expect(assignment.org_uid).to.be.equal('orgUID')
expect(assignment.type).to.be.equal('workflow_stage')
expect(assignment.entry_locale).to.be.equal('en-us')
expect(assignment.version).to.be.equal(1)
expect(assignment.assigned_to[0]).to.be.equal('user_UID')
expect(assignment.assigned_at).to.be.equal('assign_date')
expect(assignment.assigned_by).to.be.equal('assign_by')
expect(assignment.due_date).to.be.equal('due_date')
done()
})
.catch(done)
})
})

Expand Down