diff --git a/package-lock.json b/package-lock.json index b5dcd913e..5271d1819 100644 --- a/package-lock.json +++ b/package-lock.json @@ -115,6 +115,7 @@ "cross-fetch": "^3.1.4", "crypto-js": "^4.0.0", "decimal.js": "^10.2.1", + "ethereumjs-util": "^7.1.3", "fs": "0.0.1-security", "save-file": "^2.3.1", "web3": ">=1.3.5", diff --git a/src/@utils/provider.ts b/src/@utils/provider.ts index 4cfc0fd74..92efd727d 100644 --- a/src/@utils/provider.ts +++ b/src/@utils/provider.ts @@ -32,27 +32,22 @@ export async function getEncryptedFiles( export async function getFileInfo( url: string, - providerUrl: string, - cancelToken: CancelToken + providerUrl: string ): Promise { try { - // TODO: what was the point of this? - // if (url instanceof DID) postBody = { did: url.getDid() } - // else postBody = { url } - const postBody = { url, type: 'url' } - const response: AxiosResponse = await axios.post( - `${providerUrl}/api/services/fileinfo`, - postBody, - { cancelToken } + const response = await ProviderInstance.checkFileUrl( + url, + providerUrl, + (method: Method, path: string, body: string) => { + return fetch(path, { + method: method, + body: body, + headers: { 'Content-Type': 'application/json' } + }) + } ) - - if (!response || response.status !== 200 || !response.data) return - return response.data + return response } catch (error) { - if (axios.isCancel(error)) { - LoggerInstance.log(error.message) - } else { - LoggerInstance.error(error.message) - } + LoggerInstance.error(error.message) } } diff --git a/src/components/@shared/FormFields/FilesInput/index.tsx b/src/components/@shared/FormFields/FilesInput/index.tsx index 4a6352f74..504d9cab0 100644 --- a/src/components/@shared/FormFields/FilesInput/index.tsx +++ b/src/components/@shared/FormFields/FilesInput/index.tsx @@ -1,32 +1,25 @@ import React, { ReactElement, useState } from 'react' -import { useField } from 'formik' +import { useField, useFormikContext } from 'formik' import { toast } from 'react-toastify' import FileInfo from './Info' import UrlInput from '../URLInput' import { InputProps } from '@shared/FormInput' -import { getFileInfo } from '@utils/provider' -import { useWeb3 } from '@context/Web3' -import { getOceanConfig } from '@utils/ocean' -import { useCancelToken } from '@hooks/useCancelToken' import { initialValues } from 'src/components/Publish/_constants' +import { getFileInfo } from '@utils/provider' +import { FormPublishData } from 'src/components/Publish/_types' export default function FilesInput(props: InputProps): ReactElement { const [field, meta, helpers] = useField(props.name) const [isLoading, setIsLoading] = useState(false) - const { chainId } = useWeb3() - const newCancelToken = useCancelToken() + const { values } = useFormikContext() function loadFileInfo(url: string) { - const config = getOceanConfig(chainId || 1) + const providerUri = values.services[0].providerUrl.url async function validateUrl() { try { setIsLoading(true) - const checkedFile = await getFileInfo( - url, - config?.providerUri, - newCancelToken() - ) + const checkedFile = await getFileInfo(url, providerUri) checkedFile && helpers.setValue([{ url, ...checkedFile[0] }]) } catch (error) { toast.error('Could not fetch file info. Please check URL and try again') diff --git a/src/components/Asset/AssetActions/index.tsx b/src/components/Asset/AssetActions/index.tsx index 6c83fd734..1ef03ec34 100644 --- a/src/components/Asset/AssetActions/index.tsx +++ b/src/components/Asset/AssetActions/index.tsx @@ -66,14 +66,10 @@ export default function AssetActions({ const asset = values?.services?.[0].files?.[0].url || ddo.id const providerUrl = - values?.services[0].providerUrl || oceanConfig.providerUri + values?.services[0].providerUrl.url || oceanConfig.providerUri try { - const fileInfoResponse = await getFileInfo( - asset, - oceanConfig.providerUri, - newCancelToken() - ) + const fileInfoResponse = await getFileInfo(asset, providerUrl) fileInfoResponse && setFileMetadata(fileInfoResponse[0]) setFileIsLoading(false) } catch (error) {