Fileinfo replacement (#352)

* replaces market fileinfo with provider file info

* removed test for replaced getFileInfo method

* updated toast messages
- if uploaded file when no web3 wallet is connected
- if file is not valid

* used provider api to check file'

* moved get fileinfo logic to provider.ts

* cancel fileinfo req on unmount

* reduced fileinfo method complexity

* canceltoken passed to fileinfo issue
This commit is contained in:
Bogdan Fazakas 2021-02-04 16:45:21 +02:00 committed by GitHub
parent 6bdf5b44e1
commit c72dfb6ced
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 51 deletions

View File

@ -1,14 +1,44 @@
import React, { ReactElement, useState } from 'react'
import React, { ReactElement, useState, useEffect } from 'react'
import axios from 'axios'
import { useField } from 'formik'
import { toast } from 'react-toastify'
import FileInfo from './Info'
import FileInput from './Input'
import { getFileInfo } from '../../../../utils'
import { useOcean } from '@oceanprotocol/react'
import { InputProps } from '../../../atoms/Input'
import { fileinfo } from '../../../../utils/provider'
export default function FilesInput(props: InputProps): ReactElement {
const [field, meta, helpers] = useField(props.name)
const [isLoading, setIsLoading] = useState(false)
const [fileUrl, setFileUrl] = useState<string>()
const { config } = useOcean()
useEffect(() => {
const source = axios.CancelToken.source()
async function validateUrl() {
try {
setIsLoading(true)
const checkedFile = await fileinfo(
fileUrl,
config.providerUri,
source.token
)
checkedFile && helpers.setValue([checkedFile])
} catch (error) {
toast.error('Could not fetch file info. Please check URL and try again')
console.error(error.message)
} finally {
setIsLoading(false)
}
}
fileUrl && validateUrl()
return () => {
source.cancel()
}
}, [fileUrl])
async function handleButtonClick(e: React.SyntheticEvent, url: string) {
// hack so the onBlur-triggered validation does not show,
@ -18,16 +48,7 @@ export default function FilesInput(props: InputProps): ReactElement {
// File example 'https://oceanprotocol.com/tech-whitepaper.pdf'
e.preventDefault()
try {
setIsLoading(true)
const newFileInfo = await getFileInfo(url)
newFileInfo && helpers.setValue([newFileInfo])
} catch (error) {
toast.error('Could not fetch file info. Please check URL and try again')
console.error(error.message)
} finally {
setIsLoading(false)
}
setFileUrl(url)
}
return (

View File

@ -1,6 +1,4 @@
import axios, { AxiosResponse } from 'axios'
import { toast } from 'react-toastify'
import { File as FileMetadata } from '@oceanprotocol/lib'
export function updateQueryStringParameter(
uri: string,
@ -42,27 +40,6 @@ export function toStringNoMS(date: Date): string {
return date.toISOString().replace(/\.[0-9]{3}Z/, 'Z')
}
export async function getFileInfo(url: string): Promise<FileMetadata> {
const response: AxiosResponse = await axios({
method: 'POST',
url: 'https://fileinfo.oceanprotocol.com',
data: { url }
})
if (response.status > 299 || !response.data) {
toast.error('Could not connect to File API')
return
}
const { contentLength, contentType } = response.data.result
return {
contentLength,
contentType: contentType || '', // need to do that cause lib-js File interface requires contentType
url
}
}
export async function fetchData(url: string): Promise<AxiosResponse['data']> {
try {
const response = await axios(url)

38
src/utils/provider.ts Normal file
View File

@ -0,0 +1,38 @@
import axios, { CancelToken, AxiosResponse } from 'axios'
import { toast } from 'react-toastify'
import { File as FileMetadata, Logger } from '@oceanprotocol/lib'
export async function fileinfo(
url: string,
providerUri: string,
cancelToken: CancelToken
): Promise<FileMetadata> {
try {
const response: AxiosResponse = await axios.post(
`${providerUri}/api/v1/services/fileinfo`,
{
url,
cancelToken
}
)
if (!response || response.status !== 200 || !response.data) {
toast.error('Could not connect to File API')
return
}
if (!response.data[0] || !response.data[0].valid) {
toast.error('Could not fetch file info. Please check URL and try again')
return
}
const { contentLength, contentType } = response.data[0]
return {
contentLength,
contentType: contentType || '', // need to do that cause lib-js File interface requires contentType
url
}
} catch (error) {
Logger.error(error.message)
}
}

View File

@ -3,8 +3,7 @@ import axios, { AxiosResponse } from 'axios'
import {
toStringNoMS,
updateQueryStringParameter,
isDid,
getFileInfo
isDid
} from '../../../src/utils'
jest.mock('axios')
@ -28,20 +27,6 @@ describe('toStringNoMS()', () => {
})
})
describe('getFileInfo()', () => {
it('Success on existing file', async () => {
;(axios as any).mockResolvedValue({
data: {
status: 200,
result: { contentLength: '10000', contentType: 'application/pdf' }
}
} as AxiosResponse)
const fileInfo = await getFileInfo('https://demo.com')
expect(fileInfo.contentType).toBe('application/pdf')
})
})
describe('isDid()', () => {
it('checks correct DID', () => {
expect(