Skip to content

Commit

Permalink
Merge pull request #4922 from contentful/feature/region-specific-uplo…
Browse files Browse the repository at this point in the history
…ad-url

Support EU region in upload URLs
  • Loading branch information
jsdalton authored Sep 22, 2023
2 parents 1620b0e + 2b1e3c3 commit e0c6644
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/ai-image-generator/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/ai-image-generator/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"call-hosted-app-action": "npx ts-node test/scripts/call-hosted-app-action.ts"
},
"dependencies": {
"@contentful/node-apps-toolkit": "^2.2.0",
"@contentful/node-apps-toolkit": "^2.6.0",
"contentful-management": "^10.44.0",
"openai": "^4.0.0",
"sharp": "^0.32.5"
Expand Down
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 },
appActionCallContext: { appInstallationId, spaceId, uploadHost },
} = context;

let images: ImageWithUpload[];
Expand Down Expand Up @@ -72,6 +72,7 @@ export const handler = async (
imagesWithStreams: processedImages,
cmaClient: cma,
spaceId,
uploadHost,
});
} catch (e) {
if (!(e instanceof Error)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('UploadImages', () => {
const spaceId = 'spaceId';
const uploadId = 'uploadId';
const sourceUrl = 'http://www.example.com';
const uploadHost = 'upload.contentful.com';
const cmaClientStub = sinon.stub();

describe('execute', () => {
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('UploadImages', () => {
};

const cmaClient = makeMockPlainClient([mockUploadApiResponse], cmaClientStub);
uploadImages = new UploadImages(imagesWithStreams, cmaClient, spaceId);
uploadImages = new UploadImages(imagesWithStreams, cmaClient, spaceId, uploadHost);
});

it('returns images with correct urls', async () => {
Expand Down
20 changes: 13 additions & 7 deletions apps/ai-image-generator/backend/src/helpers/upload-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { PlainClientAPI, UploadProps } from 'contentful-management';
import { ImageWithStream, ImageWithUpload } from '../types';

const UPLOAD_DOMAIN: Record<string, URL> = {
us: new URL('https://s3.us-east-1.amazonaws.com/upload-api.contentful.com'),
eu: new URL('https://s3.us-east-1.amazonaws.com/upload-api.contentful.com'),
'upload.contentful.com': new URL('https://s3.us-east-1.amazonaws.com/upload-api.contentful.com'),
'upload.eu.contentful.com': new URL(
'https://s3.eu-west-1.amazonaws.com/upload-api.eu.contentful.com'
),
};

export class UploadImages {
constructor(
readonly imagesWithStreams: ImageWithStream[],
readonly cmaClient: PlainClientAPI,
readonly spaceId: string
readonly spaceId: string,
readonly uploadHost: string
) {}

async execute(): Promise<ImageWithUpload[]> {
Expand All @@ -35,20 +38,23 @@ export class UploadImages {
return await this.cmaClient.upload.create({ spaceId: this.spaceId }, { file });
}

// TODO Handle eu!
private urlFromUpload(upload: UploadProps): string {
const uploadId = upload.sys.id;
const uploadPath = `${this.spaceId}!upload!${uploadId}`;
return [UPLOAD_DOMAIN.us.toString(), uploadPath].join('/');
const uploadDomain = UPLOAD_DOMAIN[this.uploadHost];
if (!uploadDomain)
throw new Error(`Invalid uploadHost '${this.uploadHost}' -- could not find upload bucket`);
return [uploadDomain, uploadPath].join('/');
}
}

export const uploadImages = async (params: {
imagesWithStreams: ImageWithStream[];
cmaClient: PlainClientAPI;
spaceId: string;
uploadHost: string;
}) => {
const { imagesWithStreams, cmaClient, spaceId } = params;
const imageUploader = new UploadImages(imagesWithStreams, cmaClient, spaceId);
const { imagesWithStreams, cmaClient, spaceId, uploadHost } = params;
const imageUploader = new UploadImages(imagesWithStreams, cmaClient, spaceId, uploadHost);
return imageUploader.execute();
};
2 changes: 2 additions & 0 deletions apps/ai-image-generator/backend/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const makeMockAppActionCallContext = (
environmentId: 'environment-id',
appInstallationId: 'app-installation-id',
userId: 'user-id',
cmaHost: 'api.contentful.com',
uploadHost: 'upload.contentful.com',
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppActionRunner {
this.client = createClient(
{
accessToken: this.accessToken,
host: 'api.eu.contentful.com',
},
{ type: 'plain' }
);
Expand Down

0 comments on commit e0c6644

Please sign in to comment.