Skip to content

Commit

Permalink
Update aiig create upload function to handle latest upload function f…
Browse files Browse the repository at this point in the history
…or both spaceId and environmentId
  • Loading branch information
ryunsong-contentful committed Oct 30, 2023
1 parent 6e9ca27 commit 9a9fa4d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const handler = async (
): Promise<AppActionCallResponse<ImageEditResult>> => {
const {
cma,
appActionCallContext: { appInstallationId, spaceId, uploadHost },
appActionCallContext: { appInstallationId, spaceId, environmentId, uploadHost },
} = context;

let images: ImageWithUpload[];
Expand Down Expand Up @@ -72,6 +72,7 @@ export const handler = async (
imagesWithStreams: processedImages,
cmaClient: cma,
spaceId,
environmentId,
uploadHost,
});
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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';
Expand Down Expand Up @@ -44,7 +45,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 () => {
Expand Down
17 changes: 14 additions & 3 deletions apps/ai-image-generator/backend/src/helpers/upload-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class UploadImages {
readonly imagesWithStreams: ImageWithStream[],
readonly cmaClient: PlainClientAPI,
readonly spaceId: string,
readonly environmentId: string,
readonly uploadHost: string
) {}

Expand Down Expand Up @@ -53,7 +54,10 @@ export class UploadImages {
}

private async createUpload(file: sharp.Sharp): Promise<UploadProps> {
return await this.cmaClient.upload.create({ spaceId: this.spaceId }, { file });
return await this.cmaClient.upload.create(
{ spaceId: this.spaceId, environmentId: this.environmentId },
{ file }
);
}

private urlFromUpload(upload: UploadProps): string {
Expand All @@ -70,9 +74,16 @@ export const uploadImages = async (params: {
imagesWithStreams: ImageWithStream[];
cmaClient: PlainClientAPI;
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();
};

0 comments on commit 9a9fa4d

Please sign in to comment.