Skip to content

Commit

Permalink
Merge pull request #2906 from tloncorp/tlon-token-upload
Browse files Browse the repository at this point in the history
Add tlon hosting upload method
  • Loading branch information
arthyn authored Oct 23, 2023
2 parents cc308b3 + c044c40 commit 22338ff
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 106 deletions.
24 changes: 13 additions & 11 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"@tloncorp/mock-http-api": "^1.2.0",
"@types/marked": "^4.3.0",
"@urbit/api": "^2.2.0",
"@urbit/aura": "^0.4.0",
"@urbit/aura": "^1.0.0",
"@urbit/http-api": "^3.0.0",
"@urbit/sigil-js": "^2.1.0",
"any-ascii": "^0.3.1",
Expand Down
72 changes: 62 additions & 10 deletions ui/src/state/storage/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,128 @@
/* eslint-disable no-param-reassign */
import { S3Update } from '@urbit/api';
import _ from 'lodash';
import { BaseStorageState } from './type';
import { StorageUpdate, BaseStorageState } from './type';
import { BaseState } from '../base';

export type StorageState = BaseStorageState & BaseState<BaseStorageState>;

const credentials = (json: S3Update, state: StorageState): StorageState => {
const credentials = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'credentials', false);
if (data) {
state.s3.credentials = data;
}
return state;
};

const configuration = (json: S3Update, state: StorageState): StorageState => {
const configuration = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'configuration', false);
if (data) {
state.s3.configuration = {
buckets: new Set(data.buckets),
currentBucket: data.currentBucket,
region: data.region,
presignedUrl: data.presignedUrl,
service: data.service,
};
}
return state;
};

const currentBucket = (json: S3Update, state: StorageState): StorageState => {
const currentBucket = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'setCurrentBucket', false);
if (data && state.s3) {
state.s3.configuration.currentBucket = data;
}
return state;
};

const addBucket = (json: S3Update, state: StorageState): StorageState => {
const addBucket = (json: StorageUpdate, state: StorageState): StorageState => {
const data = _.get(json, 'addBucket', false);
if (data) {
state.s3.configuration.buckets = state.s3.configuration.buckets.add(data);
}
return state;
};

const removeBucket = (json: S3Update, state: StorageState): StorageState => {
const removeBucket = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'removeBucket', false);
if (data) {
state.s3.configuration.buckets.delete(data);
}
return state;
};

const endpoint = (json: S3Update, state: StorageState): StorageState => {
const endpoint = (json: StorageUpdate, state: StorageState): StorageState => {
const data = _.get(json, 'setEndpoint', false);
if (data && state.s3.credentials) {
state.s3.credentials.endpoint = data;
}
return state;
};

const accessKeyId = (json: S3Update, state: StorageState): StorageState => {
const accessKeyId = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'setAccessKeyId', false);
if (data && state.s3.credentials) {
state.s3.credentials.accessKeyId = data;
}
return state;
};

const secretAccessKey = (json: S3Update, state: StorageState): StorageState => {
const secretAccessKey = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'setSecretAccessKey', false);
if (data && state.s3.credentials) {
state.s3.credentials.secretAccessKey = data;
}
return state;
};

const region = (json: StorageUpdate, state: StorageState): StorageState => {
const data = _.get(json, 'setRegion', false);
if (data && state.s3.configuration) {
state.s3.configuration.region = data;
}
return state;
};

const presignedUrl = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'setPresignedUrl', false);
if (data && state.s3.configuration) {
state.s3.configuration.presignedUrl = data;
}
return state;
};

const toggleService = (
json: StorageUpdate,
state: StorageState
): StorageState => {
const data = _.get(json, 'toggleService', false);
if (data && state.s3.configuration) {
state.s3.configuration.service = data;
}
return state;
};

const reduce = [
credentials,
configuration,
Expand All @@ -83,6 +132,9 @@ const reduce = [
endpoint,
accessKeyId,
secretAccessKey,
region,
presignedUrl,
toggleService,
];

export default reduce;
3 changes: 2 additions & 1 deletion ui/src/state/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ export const useStorage = createState<BaseStorageState>(
'Storage',
() => ({
loaded: false,
hasCredentials: false,
s3: {
configuration: {
buckets: new Set(),
currentBucket: '',
region: '',
presignedUrl: '',
service: 'credentials',
},
credentials: null,
},
Expand Down
101 changes: 88 additions & 13 deletions ui/src/state/storage/type.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import { S3Credentials } from '@urbit/api';
import { S3Client } from '@aws-sdk/client-s3';
import { Status } from '@/logic/status';

export interface GcpToken {
accessKey: string;
expiresIn: number;
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;
secretAccessKey: string;
}

export interface BaseStorageState {
loaded?: boolean;
hasCredentials?: boolean;
s3: {
configuration: {
buckets: Set<string>;
currentBucket: string;
region: string;
};
credentials: S3Credentials | null;
configuration: StorageConfiguration;
credentials: StorageCredentials | null;
};
[ref: string]: unknown;
}

export interface GcpToken {
accessKey: string;
expiresIn: number;
}

export interface FileStoreFile {
key: string;
file: File;
Expand Down Expand Up @@ -57,14 +68,18 @@ export interface FileStore {
client: S3Client | null;
uploaders: Record<string, Uploader>;
getUploader: (key: string) => Uploader;
createClient: (s3: S3Credentials, region: string) => void;
createClient: (s3: StorageCredentials, region: string) => void;
update: (key: string, updateFn: (uploader: Uploader) => void) => void;
uploadFiles: (
uploader: string,
files: FileList | File[] | 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;
setUploadType: (uploaderKey: string, type: Uploader['uploadType']) => void;
getUploadType: (uploaderKey: string) => Uploader['uploadType'];
Expand All @@ -82,3 +97,63 @@ export interface UploadInputProps {
multiple?: boolean;
id: string;
}

export interface StorageUpdateCredentials {
credentials: StorageCredentials;
}

export interface StorageUpdateConfiguration {
configuration: {
buckets: string[];
currentBucket: string;
};
}

export interface StorageUpdateCurrentBucket {
setCurrentBucket: string;
}

export interface StorageUpdateAddBucket {
addBucket: string;
}

export interface StorageUpdateRemoveBucket {
removeBucket: string;
}

export interface StorageUpdateEndpoint {
setEndpoint: string;
}

export interface StorageUpdateAccessKeyId {
setAccessKeyId: string;
}

export interface StorageUpdateSecretAccessKey {
setSecretAccessKey: string;
}

export interface StorageUpdateRegion {
setRegion: string;
}

export interface StorageUpdateToggleService {
toggleService: string;
}

export interface StorageUpdateSetPresignedUrl {
setPresignedUrl: string;
}

export declare type StorageUpdate =
| StorageUpdateCredentials
| StorageUpdateConfiguration
| StorageUpdateCurrentBucket
| StorageUpdateAddBucket
| StorageUpdateRemoveBucket
| StorageUpdateEndpoint
| StorageUpdateAccessKeyId
| StorageUpdateSecretAccessKey
| StorageUpdateRegion
| StorageUpdateToggleService
| StorageUpdateSetPresignedUrl;
Loading

0 comments on commit 22338ff

Please sign in to comment.