-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move context registration to own file, revert AmazonS3Resource.js
- Loading branch information
1 parent
25c4c1f
commit ffa4a96
Showing
5 changed files
with
96 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {getSignedUrl} from '@aws-sdk/s3-request-presigner'; | ||
import {S3Client, GetObjectCommand} from '@aws-sdk/client-s3'; | ||
import {Resource, defer} from 'cesium'; | ||
|
||
|
||
function keyFromUrl(val) { | ||
try { | ||
const url = new URL(val); | ||
// remove the first '/' from the path | ||
return url.pathname.slice(1); | ||
} catch (err) { | ||
return val; | ||
} | ||
} | ||
|
||
export default class AmazonS3Resource extends Resource { | ||
bucket; | ||
region; | ||
|
||
constructor(options, authService) { | ||
super(options); | ||
|
||
this.bucket = options.bucket; | ||
this.region = options.region || 'eu-west-1'; | ||
} | ||
|
||
clone(result) { | ||
if (!result) { | ||
result = new AmazonS3Resource({ | ||
url: this.url, | ||
bucket: this.bucket, | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
getSignedUrl(credentials) { | ||
const client = new S3Client({ | ||
region: this.region, | ||
credentials: credentials, | ||
}); | ||
const options = { | ||
Bucket: this.bucket, | ||
Key: keyFromUrl(this.url), | ||
}; | ||
const command = new GetObjectCommand(options); | ||
return getSignedUrl(client, command); | ||
} | ||
|
||
_makeRequest(options) { | ||
const credentialsPromise = this.authService.getCredentialsPromise(); | ||
if (credentialsPromise) { | ||
const deferred = defer(); | ||
credentialsPromise.then(credentials => { | ||
this.getSignedUrl(credentials).then(url => { | ||
this.url = url; | ||
const request = super._makeRequest(options); | ||
if (request) { | ||
request.then(value => deferred.resolve(value)); | ||
} | ||
}); | ||
}); | ||
return deferred.promise; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './client-config.context'; | ||
export * from './register-context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {LitElement} from 'lit'; | ||
import {Context, ContextProvider} from '@lit/context'; | ||
import {ClientConfig} from '../api/client-config'; | ||
import {apiClientContext, authServiceContext, clientConfigContext} from './client-config.context'; | ||
import {ApiClient} from '../api/api-client'; | ||
import AuthService from '../authService'; | ||
|
||
|
||
export const registerAppContext: (element: LitElement, clientConfig: ClientConfig) => ContextProvider<Context<unknown, unknown>, LitElement>[] | ||
= (element: LitElement, clientConfig: ClientConfig) => { | ||
|
||
const authService = new AuthService(); | ||
authService.clientConfig = clientConfig; | ||
authService.initialize(); | ||
const apiClient = new ApiClient(authService); | ||
|
||
return [ | ||
new ContextProvider(element, {context: clientConfigContext, initialValue: clientConfig}), | ||
new ContextProvider(element, {context: apiClientContext, initialValue: apiClient}), | ||
new ContextProvider(element, {context: authServiceContext, initialValue: authService}), | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters