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

fix(lambda-tiler): generate previews from config urls too #2937

Merged
merged 1 commit into from
Sep 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { LambdaAlbRequest, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs
import { ALBEvent, Context } from 'aws-lambda';
import o from 'ospec';

import { loadAndServeIndexHtml } from '../preview.index.js';
import { PreviewIndexGet, loadAndServeIndexHtml, previewIndexGet } from '../preview.index.js';
import { LocationUrl } from '@basemaps/geo';
import { FsMemory } from '@chunkd/source-memory';
import { ConfigProviderMemory } from '@basemaps/config';
import { FakeData } from '../../__tests__/config.data.js';

o.spec('/@*', async () => {
o.specTimeout(1000);
Expand Down Expand Up @@ -85,6 +87,46 @@ o.spec('/@*', async () => {
const resB = await loadAndServeIndexHtml(ctx, null, new Map([['og:title', '<fake tag />']]));
o(getBody(resB)?.toString().includes('<fake tag />')).equals(true);
});

o('should include config url', async () => {
const ctx = new LambdaAlbRequest(
{
...baseRequest,
queryStringParameters: { config: 'memory://linz-basemaps/config-latest.json', i: 'imagery-name' },
},
{} as Context,
LogConfig.get(),
) as unknown as LambdaHttpRequest<PreviewIndexGet>;
ctx.params = { location: '@-41.8900012,174.0492432,z5' };

// Create a fake config to use
const expectedConfig = new ConfigProviderMemory();
expectedConfig.put(FakeData.tileSetRaster('imagery-name'));
await fsa.write('memory://linz-basemaps/config-latest.json', JSON.stringify(expectedConfig.toJson()));

process.env[Env.StaticAssetLocation] = 'memory://assets/';

const indexHtml = V('html', [
V('head', [
V('meta', { property: 'og:title', content: 'LINZ Basemaps' }),
V('meta', { property: 'og:image', content: '/basemaps-card.jepg' }),
V('meta', { name: 'viewport' }),
]),
]).toString();

await fsa.write('memory://assets/index.html', indexHtml);

const res = await previewIndexGet(ctx);
o(res.status).equals(200);

const ogImage = getBody(res)
?.toString()
.split('\n')
.find((f) => f.includes('og:image'));
o(ogImage).equals(
'<meta name="twitter:image" property="og:image" content="/v1/preview/imagery-name/WebMercatorQuad/5/174.0492432/-41.8900012?config=QzX7ZsK6qG6p42wHZaF9dhihsgprX942gAuKwfryknM429iqxdDiRSGu" />',
);
});
});

function getBody(res: LambdaHttpResponse): Buffer | null {
Expand Down
5 changes: 4 additions & 1 deletion packages/lambda-tiler/src/routes/preview.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export async function previewIndexGet(req: LambdaHttpRequest<PreviewIndexGet>):
const short = LocationUrl.truncateLatLon(loc);
const shortLocation = [short.zoom, short.lon, short.lat].join('/');

const cfg = ConfigLoader.extract(req);
const queryParams = cfg == null ? '' : `?config=${cfg}`;

// Include tile matrix name eg "[NZTM2000Quad]" in the title if its not WebMercatorQuad
const tileMatrixId = tileMatrix.identifier === GoogleTms.identifier ? '' : ` [${tileMatrix.identifier}]`;
// List of tags to replace in the index.html
Expand All @@ -111,7 +114,7 @@ export async function previewIndexGet(req: LambdaHttpRequest<PreviewIndexGet>):
],
[
'og:image',
`<meta name="twitter:image" property="og:image" content="/v1/preview/${tileSet.name}/${tileMatrix.identifier}/${shortLocation}" />`,
`<meta name="twitter:image" property="og:image" content="/v1/preview/${tileSet.name}/${tileMatrix.identifier}/${shortLocation}${queryParams}" />`,
],
]);

Expand Down
Loading