diff --git a/api/file.ts b/api/file.ts deleted file mode 100644 index db6e5789d..000000000 --- a/api/file.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { NowRequest, NowResponse } from '@now/node' -import axios, { AxiosResponse } from 'axios' -import { IncomingHttpHeaders } from 'http' - -interface ResponseResult { - contentLength?: string - contentType?: string -} - -export interface FileResponse { - status: string - message?: string - result?: ResponseResult -} - -function successResult(headers: IncomingHttpHeaders): FileResponse { - const contentType = - headers['content-type'] && headers['content-type'].split(';')[0] - let contentLength = headers['content-length'] && headers['content-length'] - - // sometimes servers send content-range header, - // try to use it if content-length is not present - if (headers['content-range'] && !headers['content-length']) { - const size = headers['content-range'].split('/')[1] - contentLength = size - } - - const result: ResponseResult = { - contentLength, - contentType - } - - return { status: 'success', result } -} - -async function checkUrl(url: string): Promise { - if (!url) { - return { status: 'error', message: 'missing url' } - } - - try { - const response: AxiosResponse = await axios({ - method: 'HEAD', - url, - headers: { Range: 'bytes=0-' } - }) - - const { headers, status } = response - const successResponses = - status.toString().startsWith('2') || status.toString().startsWith('416') - - if (!response && !successResponses) { - return { status: 'error', message: 'Unknown Error' } - } - - const result = successResult(headers) - return result - } catch (error) { - return { status: 'error', message: error.message } - } -} - -export default async (req: NowRequest, res: NowResponse) => { - switch (req.method) { - case 'POST': - res.status(200).json(await checkUrl(req.body.url)) - break - default: - res.setHeader('Allow', ['POST']) - res.status(405).end(`Method ${req.method} Not Allowed`) - } -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 5080fd72c..c61e7d2e4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -54,7 +54,7 @@ export function toStringNoMS(date: Date): string { export async function getFileInfo(url: string): Promise { const response: AxiosResponse = await axios({ method: 'POST', - url: '/api/file', + url: 'https://fileinfo.oceanprotocol.com', data: { url } }) @@ -67,12 +67,12 @@ export async function getFileInfo(url: string): Promise { return { contentLength, - contentType: contentType || '', // need to do that cause squid.js File interface requires contentType + contentType: contentType || '', // need to do that cause lib-js File interface requires contentType url } } -export async function fetchData(url: string): Promise { +export async function fetchData(url: string): Promise { try { const response = await axios(url)