mirror of
https://github.com/oceanprotocol/market.git
synced 2024-11-14 17:24:51 +01:00
kick out file API
This commit is contained in:
parent
273cd64b95
commit
8f9f3303de
72
api/file.ts
72
api/file.ts
@ -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<FileResponse> {
|
|
||||||
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`)
|
|
||||||
}
|
|
||||||
}
|
|
@ -54,7 +54,7 @@ export function toStringNoMS(date: Date): string {
|
|||||||
export async function getFileInfo(url: string): Promise<FileMetadata> {
|
export async function getFileInfo(url: string): Promise<FileMetadata> {
|
||||||
const response: AxiosResponse = await axios({
|
const response: AxiosResponse = await axios({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/api/file',
|
url: 'https://fileinfo.oceanprotocol.com',
|
||||||
data: { url }
|
data: { url }
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -67,12 +67,12 @@ export async function getFileInfo(url: string): Promise<FileMetadata> {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
contentLength,
|
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
|
url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchData(url: string): Promise<Axios> {
|
export async function fetchData(url: string): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const response = await axios(url)
|
const response = await axios(url)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user