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): prefer geojson files to be downloaded BM-1048 #3316

Merged
merged 2 commits into from
Jul 11, 2024
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
52 changes: 51 additions & 1 deletion packages/lambda-tiler/src/routes/__tests__/imagery.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { before, beforeEach, describe, it } from 'node:test';

import { ConfigProviderMemory } from '@basemaps/config';
import { fsa, FsMemory } from '@chunkd/fs';

import { Imagery3857 } from '../../__tests__/config.data.js';
import { mockRequest } from '../../__tests__/xyz.util.js';
import { handler } from '../../index.js';
import { ConfigLoader } from '../../util/config.loader.js';
import { isAllowedFile } from '../imagery.js';

describe('ImageryRoute', () => {
const memory = new FsMemory();
const config = new ConfigProviderMemory();

before(() => {
fsa.register('memory://imagery/', memory);
});

beforeEach(() => {
config.assets = 'fake-s3://assets/';
});

it('should allow geojson and json files only', () => {
assert.equal(isAllowedFile('foo.geojson'), true);
assert.equal(isAllowedFile('foo.json'), true);
Expand All @@ -12,4 +30,36 @@ describe('ImageryRoute', () => {
assert.equal(isAllowedFile(''), false);
assert.equal(isAllowedFile(null as unknown as string), false);
});

it('should force download geojson files', async (t) => {
const fakeImagery = { ...Imagery3857 };
config.put(fakeImagery);
fakeImagery.uri = 'memory://imagery/';

t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));
const res404 = await handler.router.handle(mockRequest(`/v1/imagery/${fakeImagery.id}/capture-area.geojson`));
assert.equal(res404.status, 404);

await memory.write(fsa.toUrl('memory://imagery/capture-area.geojson'), JSON.stringify({ hello: 'world' }));

const res200 = await handler.router.handle(mockRequest(`/v1/imagery/${fakeImagery.id}/capture-area.geojson`));
assert.equal(res200.status, 200);
assert.equal(res200.headers.get('content-disposition'), 'attachment');
});

it('should fetch stac files', async (t) => {
const fakeImagery = { ...Imagery3857 };
config.put(fakeImagery);
fakeImagery.uri = 'memory://imagery/';

t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));
const res404 = await handler.router.handle(mockRequest(`/v1/imagery/${fakeImagery.id}/collection.json`));
assert.equal(res404.status, 404);

await memory.write(fsa.toUrl('memory://imagery/collection.json'), JSON.stringify({ hello: 'world' }));

const res200 = await handler.router.handle(mockRequest(`/v1/imagery/${fakeImagery.id}/collection.json`));
assert.equal(res200.status, 200);
assert.equal(res200.headers.get('content-disposition'), undefined);
});
});
4 changes: 4 additions & 0 deletions packages/lambda-tiler/src/routes/imagery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export async function imageryGet(req: LambdaHttpRequest<ImageryGet>): Promise<La
response.header(HttpHeader.ETag, cacheKey);
response.header(HttpHeader.ContentEncoding, 'gzip');
response.header(HttpHeader.CacheControl, 'public, max-age=604800, stale-while-revalidate=86400');
// Force geojson files to be downloaded
if (requestedFile.endsWith('.geojson')) {
response.header('Content-Disposition', 'attachment');
}
response.buffer(isGzip(buf) ? buf : await gzipP(buf), 'application/json');
req.set('bytes', buf.byteLength);
return response;
Expand Down
Loading