diff --git a/app/src/os/services/ship/ship.service.ts b/app/src/os/services/ship/ship.service.ts index b30517431d..ace455efbd 100644 --- a/app/src/os/services/ship/ship.service.ts +++ b/app/src/os/services/ship/ship.service.ts @@ -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 { public patp: string; private shipDB?: ShipDB; @@ -271,22 +282,23 @@ export class ShipService extends AbstractService { // ---------------------------------------- // ------------------ S3 ------------------ // ---------------------------------------- - public async getS3Bucket() { - const [credentials, configuration] = await Promise.all([ - APIConnection.getInstance().conduit.scry({ + public async getS3Bucket(): Promise { + 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( @@ -294,8 +306,9 @@ export class ShipService extends AbstractService { ): 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) { @@ -339,8 +352,9 @@ export class ShipService extends AbstractService { public async deleteFile(args: { key: string }): Promise { 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) { diff --git a/app/src/renderer/lib/useStorage.ts b/app/src/renderer/lib/useStorage.ts index 212385732e..276a7b9f57 100644 --- a/app/src/renderer/lib/useStorage.ts +++ b/app/src/renderer/lib/useStorage.ts @@ -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'; @@ -17,18 +18,15 @@ export interface IuseStorage { export const useStorage = ({ accept = '*' } = { accept: '*' }): IuseStorage => { const [uploading, setUploading] = useState(false); - const [s3, setS3] = useState(); + const [s3, setS3] = useState(); + + const getAndSetS3 = async () => { + const fetched = await ShipIPC.getS3Bucket(); + if (fetched) setS3(fetched); + }; useEffect(() => { - if (!s3) { - (ShipIPC.getS3Bucket() as Promise) - .then((response: any) => { - setS3(response); - }) - .catch((err: any) => { - console.error(err); - }); - } + if (!s3) getAndSetS3(); }, [s3]); const client = useRef(null); @@ -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]); @@ -99,7 +96,7 @@ export const useStorage = ({ accept = '*' } = { accept: '*' }): IuseStorage => { const uploadDefault = useCallback( async (file: File): Promise => { - if (s3.configuration.currentBucket === '') { + if (!s3 || s3.configuration.currentBucket === '') { throw new Error('current bucket not set'); } return await upload(file, s3.configuration.currentBucket);