-
Notifications
You must be signed in to change notification settings - Fork 149
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
[INTEG-1417] Update aiig create upload function to handle latest upload function for both spaceId and environmentId #5324
Changes from 6 commits
e3052ce
d27963f
378c23b
649b2ae
742b0ef
a6704d0
388a4ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,17 @@ import { expect } from 'chai'; | |
describe('UploadImages', () => { | ||
let uploadImages: UploadImages; | ||
const spaceId = 'spaceId'; | ||
const environmentId = 'environmentId'; | ||
const uploadId = 'uploadId'; | ||
const sourceUrl = 'http://www.example.com'; | ||
const uploadHost = 'upload.contentful.com'; | ||
const cmaClientStub = sinon.stub(); | ||
|
||
describe('execute', () => { | ||
afterEach(() => { | ||
cmaClientStub.reset(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
const imagePath = absolutePathToFile('./test/mocks/images/peaceful-cat.jpg'); | ||
const imageResponse = await responseFromFile(imagePath); | ||
|
@@ -31,6 +36,13 @@ describe('UploadImages', () => { | |
id: spaceId, | ||
}, | ||
}, | ||
environment: { | ||
sys: { | ||
type: 'Link', | ||
linkType: 'Environment', | ||
id: environmentId, | ||
}, | ||
}, | ||
expiresAt: '2015-05-18T11:29:46.809Z', | ||
createdAt: '2015-05-18T11:29:46.809Z', | ||
createdBy: { | ||
|
@@ -44,7 +56,13 @@ describe('UploadImages', () => { | |
}; | ||
|
||
const cmaClient = makeMockPlainClient([mockUploadApiResponse], cmaClientStub); | ||
uploadImages = new UploadImages(imagesWithStreams, cmaClient, spaceId, uploadHost); | ||
uploadImages = new UploadImages( | ||
imagesWithStreams, | ||
cmaClient, | ||
spaceId, | ||
environmentId, | ||
uploadHost | ||
); | ||
}); | ||
|
||
it('returns images with correct urls', async () => { | ||
|
@@ -56,8 +74,58 @@ describe('UploadImages', () => { | |
expect(relativeImageSizeDiff).to.below(0.1); | ||
expect(image.upload).to.have.property( | ||
'url', | ||
`https://s3.us-east-1.amazonaws.com/upload-api.contentful.com/${spaceId}!upload!${uploadId}` | ||
`https://s3.us-east-1.amazonaws.com/upload-api.contentful.com/${spaceId}!${environmentId}!upload!${uploadId}` | ||
); | ||
}); | ||
|
||
describe('when environment id not present in upload response', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep an explicit test that ensures correct behavior if for some reason the old upload (without environment id) is created. |
||
beforeEach(async () => { | ||
const imagePath = absolutePathToFile('./test/mocks/images/peaceful-cat.jpg'); | ||
const imageResponse = await responseFromFile(imagePath); | ||
const imagesWithStreams = [ | ||
{ url: sourceUrl, imageType: 'png', stream: toSharp(imageResponse.body) }, | ||
]; | ||
const mockUploadApiResponse = { | ||
sys: { | ||
type: 'Upload', | ||
id: uploadId, | ||
space: { | ||
sys: { | ||
type: 'Link', | ||
linkType: 'Space', | ||
id: spaceId, | ||
}, | ||
}, | ||
expiresAt: '2015-05-18T11:29:46.809Z', | ||
createdAt: '2015-05-18T11:29:46.809Z', | ||
createdBy: { | ||
sys: { | ||
type: 'Link', | ||
linkType: 'User', | ||
id: '4FLrUHftHW3v2BLi9fzfjU', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const cmaClient = makeMockPlainClient([mockUploadApiResponse], cmaClientStub); | ||
uploadImages = new UploadImages( | ||
imagesWithStreams, | ||
cmaClient, | ||
spaceId, | ||
environmentId, | ||
uploadHost | ||
); | ||
}); | ||
|
||
it('returns images without environment id in urls', async () => { | ||
const result = await uploadImages.execute(); | ||
const image = result[0]; | ||
expect(image.upload).to.have.property( | ||
'url', | ||
`https://s3.us-east-1.amazonaws.com/upload-api.contentful.com/${spaceId}!upload!${uploadId}` | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { PlainClientAPI, UploadProps } from 'contentful-management'; | ||
import { SysLink, UploadProps } from 'contentful-management'; | ||
import { AppActionCallContext } from '@contentful/node-apps-toolkit'; | ||
import { ImageWithStream, ImageWithUpload } from '../types'; | ||
import sharp from 'sharp'; | ||
import { toDimensions } from '../utils'; | ||
|
@@ -10,11 +11,17 @@ const UPLOAD_DOMAIN: Record<string, URL> = { | |
), | ||
}; | ||
|
||
type UploadPropsWithEnvironment = { | ||
// TODO: use upstream UplpadProps directly once environment id has been added | ||
sys: UploadProps['sys'] & { environment?: SysLink }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: typo in this comment, |
||
}; | ||
|
||
export class UploadImages { | ||
constructor( | ||
readonly imagesWithStreams: ImageWithStream[], | ||
readonly cmaClient: PlainClientAPI, | ||
readonly cmaClient: AppActionCallContext['cma'], | ||
readonly spaceId: string, | ||
readonly environmentId: string, | ||
readonly uploadHost: string | ||
) {} | ||
|
||
|
@@ -52,13 +59,20 @@ export class UploadImages { | |
return { dimensions, size }; | ||
} | ||
|
||
private async createUpload(file: sharp.Sharp): Promise<UploadProps> { | ||
return await this.cmaClient.upload.create({ spaceId: this.spaceId }, { file }); | ||
private async createUpload(file: sharp.Sharp): Promise<UploadPropsWithEnvironment> { | ||
return await this.cmaClient.upload.create( | ||
{ spaceId: this.spaceId, environmentId: this.environmentId }, | ||
{ file } | ||
); | ||
} | ||
|
||
private urlFromUpload(upload: UploadProps): string { | ||
private urlFromUpload(upload: UploadPropsWithEnvironment): string { | ||
const uploadId = upload.sys.id; | ||
const uploadPath = `${this.spaceId}!upload!${uploadId}`; | ||
const spaceId = upload.sys.space.sys.id; | ||
const environmentId = upload.sys.environment && upload.sys.environment.sys.id; | ||
const uploadPath = environmentId | ||
? `${spaceId}!${environmentId}!upload!${uploadId}` | ||
: `${spaceId}!upload!${uploadId}`; | ||
const uploadDomain = UPLOAD_DOMAIN[this.uploadHost]; | ||
if (!uploadDomain) | ||
throw new Error(`Invalid uploadHost '${this.uploadHost}' -- could not find upload bucket`); | ||
|
@@ -68,11 +82,18 @@ export class UploadImages { | |
|
||
export const uploadImages = async (params: { | ||
imagesWithStreams: ImageWithStream[]; | ||
cmaClient: PlainClientAPI; | ||
cmaClient: AppActionCallContext['cma']; | ||
spaceId: string; | ||
environmentId: string; | ||
uploadHost: string; | ||
}) => { | ||
const { imagesWithStreams, cmaClient, spaceId, uploadHost } = params; | ||
const imageUploader = new UploadImages(imagesWithStreams, cmaClient, spaceId, uploadHost); | ||
const { imagesWithStreams, cmaClient, spaceId, environmentId, uploadHost } = params; | ||
const imageUploader = new UploadImages( | ||
imagesWithStreams, | ||
cmaClient, | ||
spaceId, | ||
environmentId, | ||
uploadHost | ||
); | ||
return imageUploader.execute(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two are required by sharp 0.33 for our installation to work locally