Skip to content

Commit ffa4a96

Browse files
committed
Move context registration to own file, revert AmazonS3Resource.js
1 parent 25c4c1f commit ffa4a96

File tree

5 files changed

+96
-91
lines changed

5 files changed

+96
-91
lines changed

ui/src/AmazonS3Resource.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {getSignedUrl} from '@aws-sdk/s3-request-presigner';
2+
import {S3Client, GetObjectCommand} from '@aws-sdk/client-s3';
3+
import {Resource, defer} from 'cesium';
4+
5+
6+
function keyFromUrl(val) {
7+
try {
8+
const url = new URL(val);
9+
// remove the first '/' from the path
10+
return url.pathname.slice(1);
11+
} catch (err) {
12+
return val;
13+
}
14+
}
15+
16+
export default class AmazonS3Resource extends Resource {
17+
bucket;
18+
region;
19+
20+
constructor(options, authService) {
21+
super(options);
22+
23+
this.bucket = options.bucket;
24+
this.region = options.region || 'eu-west-1';
25+
}
26+
27+
clone(result) {
28+
if (!result) {
29+
result = new AmazonS3Resource({
30+
url: this.url,
31+
bucket: this.bucket,
32+
});
33+
}
34+
return result;
35+
}
36+
37+
getSignedUrl(credentials) {
38+
const client = new S3Client({
39+
region: this.region,
40+
credentials: credentials,
41+
});
42+
const options = {
43+
Bucket: this.bucket,
44+
Key: keyFromUrl(this.url),
45+
};
46+
const command = new GetObjectCommand(options);
47+
return getSignedUrl(client, command);
48+
}
49+
50+
_makeRequest(options) {
51+
const credentialsPromise = this.authService.getCredentialsPromise();
52+
if (credentialsPromise) {
53+
const deferred = defer();
54+
credentialsPromise.then(credentials => {
55+
this.getSignedUrl(credentials).then(url => {
56+
this.url = url;
57+
const request = super._makeRequest(options);
58+
if (request) {
59+
request.then(value => deferred.resolve(value));
60+
}
61+
});
62+
});
63+
return deferred.promise;
64+
}
65+
}
66+
}

ui/src/AmazonS3Resource.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

ui/src/context/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './client-config.context';
2+
export * from './register-context';

ui/src/context/register-context.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {LitElement} from 'lit';
2+
import {Context, ContextProvider} from '@lit/context';
3+
import {ClientConfig} from '../api/client-config';
4+
import {apiClientContext, authServiceContext, clientConfigContext} from './client-config.context';
5+
import {ApiClient} from '../api/api-client';
6+
import AuthService from '../authService';
7+
8+
9+
export const registerAppContext: (element: LitElement, clientConfig: ClientConfig) => ContextProvider<Context<unknown, unknown>, LitElement>[]
10+
= (element: LitElement, clientConfig: ClientConfig) => {
11+
12+
const authService = new AuthService();
13+
authService.clientConfig = clientConfig;
14+
authService.initialize();
15+
const apiClient = new ApiClient(authService);
16+
17+
return [
18+
new ContextProvider(element, {context: clientConfigContext, initialValue: clientConfig}),
19+
new ContextProvider(element, {context: apiClientContext, initialValue: apiClient}),
20+
new ContextProvider(element, {context: authServiceContext, initialValue: authService}),
21+
];
22+
};

ui/src/ngm-app-boot.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,37 @@ import {LitElement, html} from 'lit';
22
import {customElement} from 'lit/decorators.js';
33
import './ngm-app';
44
import {Task} from '@lit/task';
5-
import {provide} from '@lit/context';
65

76
import {ClientConfig} from './api/client-config';
8-
import {apiClientContext, authServiceContext, clientConfigContext} from './context';
7+
import {registerAppContext} from './context';
98
import {ConfigService} from './api/config.service';
10-
import AuthService from './authService';
11-
import {ApiClient} from './api/api-client';
129

1310

1411
@customElement('ngm-app-boot')
1512
export class NgmAppBoot extends LitElement {
16-
@provide({context: clientConfigContext})
17-
private accessor clientConfig!: ClientConfig;
18-
@provide({context: authServiceContext})
19-
accessor authService = new AuthService();
20-
@provide({context: apiClientContext})
21-
accessor apiClient: ApiClient = null!;
22-
23-
2413
private viewerInitialization = new Task(this, {
2514
task: async () => {
26-
this.clientConfig = await new ConfigService().getConfig() as ClientConfig;
27-
if (!this.clientConfig) {
15+
const clientConfig = await new ConfigService().getConfig() as ClientConfig;
16+
if (!clientConfig) {
2817
console.error('Failed to load client config');
2918
return;
3019
}
3120

32-
this.authService.clientConfig = this.clientConfig;
33-
this.authService.initialize();
34-
this.apiClient = new ApiClient(this.authService);
21+
registerAppContext(this, clientConfig);
3522
},
3623
args: () => [],
3724
});
3825

3926
render() {
4027
return this.viewerInitialization.render({
4128
pending: () => html`<p>Loading</p>`,
42-
complete: () => html`<ngm-app></ngm-app>`,
29+
complete: () => html`
30+
<ngm-app></ngm-app>`,
4331
error: (e) => html`<p>Error: ${e}</p>`
4432
});
4533
}
4634

47-
// This deactivates shadow DOM. Because this is done for all other components, we have to add in for the time being.
35+
// This deactivates shadow DOM. Because this is done for all other components, we have to add it for the time being.
4836
createRenderRoot() {
4937
return this;
5038
}

0 commit comments

Comments
 (0)