Skip to content

Commit

Permalink
Merge pull request #222 from tloncorp/add-token-upload
Browse files Browse the repository at this point in the history
storage: add token-based upload for hosting
  • Loading branch information
arthyn authored Oct 23, 2023
2 parents 8671e37 + a5109ef commit 4b7ce11
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 229 deletions.
39 changes: 34 additions & 5 deletions desk/app/storage.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
+$ versioned-state
$% state-zero
state-one
state-two
==
::
+$ state-zero [%0 =credentials:zero:past =configuration:zero:past]
+$ state-one [%1 =credentials =configuration]
+$ state-one [%1 =credentials:one:past =configuration:one:past]
+$ state-two [%2 =credentials =configuration]
--
::
=| state-one
=| state-two
=* state -
::
%- agent:dbug
Expand All @@ -41,13 +43,15 @@
++ on-save !>(state)
++ on-load
|= =vase
|^
=/ old ((soft versioned-state) q.vase)
?~ old on-init
=/ old u.old
|^
|-
?- -.old
%1 `this(state old)
%0 `this(state (state-0-to-1 old))
%0 $(old (state-0-to-1 old))
%1 $(old (state-1-to-2 old))
%2 `this(state old)
==
++ state-0-to-1
|= zer=state-zero
Expand All @@ -56,12 +60,31 @@
credentials.zer
(configuration-0-to-1 configuration.zer)
==
::
++ configuration-0-to-1
|= conf=configuration:zero:past
^- configuration:one:past
:* buckets.conf
current-bucket.conf
''
==
::
++ state-1-to-2
|= one=state-one
^- state-two
:* %2
credentials.one
(configuration-1-to-2 configuration.one)
==
::
++ configuration-1-to-2
|= conf=configuration:one:past
^- ^configuration
:* buckets.conf
current-bucket.conf
region.conf
''
%credentials
==
--
::
Expand Down Expand Up @@ -127,6 +150,12 @@
::
%remove-bucket
state(buckets.configuration (~(del in buckets.configuration) bucket.act))
::
%set-presigned-url
state(presigned-url.configuration url.act)
::
%toggle-service
state(service.configuration service.act)
==
--
::
Expand Down
6 changes: 6 additions & 0 deletions desk/lib/storage-json.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
[%add-bucket so:dejs]
[%remove-bucket so:dejs]
[%set-current-bucket so:dejs]
[%set-presigned-url so:dejs]
[%toggle-service (su:dejs (perk %presigned-url %credentials ~))]
==
--
::
Expand All @@ -30,6 +32,8 @@
%remove-bucket [%'removeBucket' s+bucket.upd]
%set-endpoint [%'setEndpoint' s+endpoint.upd]
%set-access-key-id [%'setAccessKeyId' s+access-key-id.upd]
%set-presigned-url [%'setPresignedUrl' s+url.upd]
%toggle-service [%'toggleService' s+service.upd]
%set-secret-access-key
[%'setSecretAccessKey' s+secret-access-key.upd]
::
Expand All @@ -47,6 +51,8 @@
:~ [%buckets a+(turn ~(tap in buckets.configuration.upd) |=(a=@t s+a))]
[%'currentBucket' s+current-bucket.configuration.upd]
[%'region' s+region.configuration.upd]
[%'service' s+service.configuration.upd]
[%'presignedUrl' s+presigned-url.configuration.upd]
==
==
==
Expand Down
29 changes: 29 additions & 0 deletions desk/sur/storage-1.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
|%
+$ credentials
$: endpoint=@t
access-key-id=@t
secret-access-key=@t
==
::
+$ configuration
$: buckets=(set @t)
current-bucket=@t
region=@t
==
::
+$ action
$% [%set-endpoint endpoint=@t]
[%set-access-key-id access-key-id=@t]
[%set-secret-access-key secret-access-key=@t]
[%add-bucket bucket=@t]
[%remove-bucket bucket=@t]
[%set-current-bucket bucket=@t]
[%set-region region=@t]
==
::
+$ update
$% [%credentials =credentials]
[%configuration =configuration]
action
==
--
16 changes: 15 additions & 1 deletion desk/sur/storage.hoon
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
/- zer=storage-0
/- zer=storage-0, uno=storage-1
|%
++ past
|%
++ zero zer
++ one uno
--
+$ service ?(%presigned-url %credentials)
+$ credentials
$: endpoint=@t
access-key-id=@t
secret-access-key=@t
==
::
:: $configuration: the upload configuration
::
:: $buckets: the buckets available
:: $current-bucket: the current bucket we use to upload
:: $region: the region of the current bucket
:: $presigned-url: the presigned url endpoint
:: $service: whether to use a presigned url service or direct S3 uploads
::
+$ configuration
$: buckets=(set @t)
current-bucket=@t
region=@t
presigned-url=@t
=service
==
::
+$ action
Expand All @@ -24,6 +36,8 @@
[%remove-bucket bucket=@t]
[%set-current-bucket bucket=@t]
[%set-region region=@t]
[%set-presigned-url url=@t]
[%toggle-service =service]
==
::
+$ update
Expand Down
91 changes: 57 additions & 34 deletions ui/src/gear/storage/lib.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,75 @@
import { Poke } from '@urbit/http-api';
import { StorageUpdate, StorageUpdateCurrentBucket, StorageUpdateAddBucket, StorageUpdateRemoveBucket, StorageUpdateEndpoint, StorageUpdateAccessKeyId, StorageUpdateSecretAccessKey, StorageUpdateRegion } from './types';
import {
StorageUpdate,
StorageUpdateCurrentBucket,
StorageUpdateAddBucket,
StorageUpdateRemoveBucket,
StorageUpdateEndpoint,
StorageUpdateAccessKeyId,
StorageUpdateSecretAccessKey,
StorageUpdateRegion,
StorageService,
StorageUpdateSetPresignedUrl,
StorageUpdateToggleService,
} from './types';

const storageAction = <T extends StorageUpdate>(
data: any
): Poke<T> => ({
const storageAction = <T extends StorageUpdate>(data: any): Poke<T> => ({
app: 'storage',
mark: 'storage-action',
json: data
json: data,
});

export const setCurrentBucket = (
bucket: string
): Poke<StorageUpdateCurrentBucket> => storageAction({
'set-current-bucket': bucket
});
): Poke<StorageUpdateCurrentBucket> =>
storageAction({
'set-current-bucket': bucket,
});

export const addBucket = (
bucket: string
): Poke<StorageUpdateAddBucket> => storageAction({
'add-bucket': bucket
});
export const addBucket = (bucket: string): Poke<StorageUpdateAddBucket> =>
storageAction({
'add-bucket': bucket,
});

export const removeBucket = (
bucket: string
): Poke<StorageUpdateRemoveBucket> => storageAction({
'remove-bucket': bucket
});
export const removeBucket = (bucket: string): Poke<StorageUpdateRemoveBucket> =>
storageAction({
'remove-bucket': bucket,
});

export const setEndpoint = (
endpoint: string
): Poke<StorageUpdateEndpoint> => storageAction({
'set-endpoint': endpoint
});
export const setEndpoint = (endpoint: string): Poke<StorageUpdateEndpoint> =>
storageAction({
'set-endpoint': endpoint,
});

export const setAccessKeyId = (
accessKeyId: string
): Poke<StorageUpdateAccessKeyId> => storageAction({
'set-access-key-id': accessKeyId
});
): Poke<StorageUpdateAccessKeyId> =>
storageAction({
'set-access-key-id': accessKeyId,
});

export const setSecretAccessKey = (
secretAccessKey: string
): Poke<StorageUpdateSecretAccessKey> => storageAction({
'set-secret-access-key': secretAccessKey
});
): Poke<StorageUpdateSecretAccessKey> =>
storageAction({
'set-secret-access-key': secretAccessKey,
});

export const setRegion = (region: string): Poke<StorageUpdateRegion> =>
storageAction({
'set-region': region,
});

export const setPresignedUrl = (
presignedUrl: string
): Poke<StorageUpdateSetPresignedUrl> =>
storageAction({
'set-presigned-url': presignedUrl,
});

export const setRegion = (
region: string
): Poke<StorageUpdateRegion> => storageAction({
'set-region': region
})
export const toggleService = (
service: StorageService
): Poke<StorageUpdateToggleService> =>
storageAction({
'toggle-service': service,
});
36 changes: 28 additions & 8 deletions ui/src/gear/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { S3Client } from '@aws-sdk/client-s3';

export type Status = 'initial' | 'idle' | 'loading' | 'success' | 'error';

export type StorageService = 'presigned-url' | 'credentials';

export interface StorageConfiguration {
buckets: Set<string>;
currentBucket: string;
region: string;
presignedUrl: string;
service: StorageService;
}

export interface StorageCredentials {
endpoint: string;
accessKeyId: string;
Expand All @@ -12,11 +22,7 @@ export interface BaseStorageState {
loaded?: boolean;
hasCredentials?: boolean;
s3: {
configuration: {
buckets: Set<string>;
currentBucket: string;
region: string;
};
configuration: StorageConfiguration;
credentials: StorageCredentials | null;
};
[ref: string]: unknown;
Expand Down Expand Up @@ -67,9 +73,13 @@ export interface FileStore {
uploadFiles: (
uploader: string,
files: FileList | null,
bucket: string
config: StorageConfiguration
) => Promise<void>;
upload: (
uploader: string,
upload: Upload,
config: StorageConfiguration
) => Promise<void>;
upload: (uploader: string, upload: Upload, bucket: string) => Promise<void>;
clear: (uploader: string) => void;
updateFile: (
uploader: string,
Expand Down Expand Up @@ -125,6 +135,14 @@ export interface StorageUpdateRegion {
setRegion: string;
}

export interface StorageUpdateToggleService {
toggleService: string;
}

export interface StorageUpdateSetPresignedUrl {
setPresignedUrl: string;
}

export declare type StorageUpdate =
| StorageUpdateCredentials
| StorageUpdateConfiguration
Expand All @@ -134,4 +152,6 @@ export declare type StorageUpdate =
| StorageUpdateEndpoint
| StorageUpdateAccessKeyId
| StorageUpdateSecretAccessKey
| StorageUpdateRegion;
| StorageUpdateRegion
| StorageUpdateToggleService
| StorageUpdateSetPresignedUrl;
Loading

0 comments on commit 4b7ce11

Please sign in to comment.