Skip to content

Commit

Permalink
Merge pull request #1897 from holium/fix-s3-typing
Browse files Browse the repository at this point in the history
Fix S3 typing
  • Loading branch information
gdbroman authored Jul 12, 2023
2 parents 1ab513c + b9fb158 commit 5819379
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
40 changes: 27 additions & 13 deletions app/src/os/services/ship/ship.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import SpacesService from './spaces/spaces.service';
import TroveService from './trove.service';
import WalletService from './wallet/wallet.service';

export type S3CredentialsAndConfig = {
credentials: {
accessKeyId: string;
secretAccessKey: string;
endpoint: string;
};
configuration: {
currentBucket: string;
};
};

export class ShipService extends AbstractService<any> {
public patp: string;
private shipDB?: ShipDB;
Expand Down Expand Up @@ -271,31 +282,33 @@ export class ShipService extends AbstractService<any> {
// ----------------------------------------
// ------------------ S3 ------------------
// ----------------------------------------
public async getS3Bucket() {
const [credentials, configuration] = await Promise.all([
APIConnection.getInstance().conduit.scry({
public async getS3Bucket(): Promise<S3CredentialsAndConfig | null> {
try {
const credentials = await APIConnection.getInstance().conduit.scry({
app: 'api-store',
path: `/credentials`,
}),
APIConnection.getInstance().conduit.scry({
});
const configuration = await APIConnection.getInstance().conduit.scry({
app: 'api-store',
path: `/configuration`,
}),
]);
});

return {
credentials,
configuration,
};
return { credentials, configuration };
} catch (e) {
log.error('ship.service.ts, getS3Bucket(): error getting credentials', e);

return null;
}
}

public async uploadFile(
args: FileUploadParams
): Promise<{ Location: string; key: string }> {
return await new Promise((resolve, reject) => {
this.getS3Bucket()
.then(async (response: any) => {
.then(async (response) => {
console.log('getS3Bucket response: ', response);
if (!response) return;
// a little shim to handle people who accidentally included their bucket at the front of the credentials.endpoint
let endp = response.credentials.endpoint;
if (endp.split('.')[0] === response.configuration.currentBucket) {
Expand Down Expand Up @@ -339,8 +352,9 @@ export class ShipService extends AbstractService<any> {
public async deleteFile(args: { key: string }): Promise<any> {
return await new Promise((resolve, reject) => {
this.getS3Bucket()
.then(async (response: any) => {
.then(async (response) => {
console.log('getS3Bucket response: ', response);
if (!response) return;
// a little shim to handle people who accidentally included their bucket at the front of the credentials.endpoint
let endp = response.credentials.endpoint;
if (endp.split('.')[0] === response.configuration.currentBucket) {
Expand Down
29 changes: 13 additions & 16 deletions app/src/renderer/lib/useStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import moment from 'moment';

import { S3CredentialsAndConfig } from 'os/services/ship/ship.service';
import { ShipIPC } from 'renderer/stores/ipc';

import { S3Client, StorageAcl, StorageClient } from './S3Client';
Expand All @@ -17,18 +18,15 @@ export interface IuseStorage {

export const useStorage = ({ accept = '*' } = { accept: '*' }): IuseStorage => {
const [uploading, setUploading] = useState(false);
const [s3, setS3] = useState<any>();
const [s3, setS3] = useState<S3CredentialsAndConfig>();

const getAndSetS3 = async () => {
const fetched = await ShipIPC.getS3Bucket();
if (fetched) setS3(fetched);
};

useEffect(() => {
if (!s3) {
(ShipIPC.getS3Bucket() as Promise<any>)
.then((response: any) => {
setS3(response);
})
.catch((err: any) => {
console.error(err);
});
}
if (!s3) getAndSetS3();
}, [s3]);

const client = useRef<StorageClient | null>(null);
Expand Down Expand Up @@ -58,13 +56,12 @@ export const useStorage = ({ accept = '*' } = { accept: '*' }): IuseStorage => {
}, [s3]);

const canUpload = useMemo(() => {
if (!s3) return;
return (
(s3.credentials &&
if (!s3) return false;
return Boolean(
s3.credentials &&
s3.credentials.accessKeyId &&
s3.credentials.secretAccessKey &&
s3.configuration.currentBucket !== '') ||
false
s3.configuration.currentBucket !== ''
);
}, [s3]);

Expand Down Expand Up @@ -99,7 +96,7 @@ export const useStorage = ({ accept = '*' } = { accept: '*' }): IuseStorage => {

const uploadDefault = useCallback(
async (file: File): Promise<string> => {
if (s3.configuration.currentBucket === '') {
if (!s3 || s3.configuration.currentBucket === '') {
throw new Error('current bucket not set');
}
return await upload(file, s3.configuration.currentBucket);
Expand Down

0 comments on commit 5819379

Please sign in to comment.