1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Move download file behavior to utils.

This commit is contained in:
Pedro Gutiérrez 2019-07-11 15:54:21 +02:00 committed by Pedro Gutiérrez
parent bd411fa83f
commit a629a04dd4
2 changed files with 43 additions and 38 deletions

View File

@ -1,10 +1,7 @@
import * as fs from 'fs'
import { File } from '../ddo/MetaData' import { File } from '../ddo/MetaData'
import Account from '../ocean/Account' import Account from '../ocean/Account'
import { noZeroX } from '../utils' import { noZeroX } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
import save = require('save-file')
const apiPath = '/api/v1/brizo/services' const apiPath = '/api/v1/brizo/services'
@ -98,7 +95,7 @@ export class Brizo extends Instantiable {
consumeUrl += `&signature=${signature}` consumeUrl += `&signature=${signature}`
try { try {
await this.downloadFile(consumeUrl, destination) await this.ocean.utils.fetch.downloadFile(consumeUrl, destination, i)
} catch (e) { } catch (e) {
this.logger.error('Error consuming assets') this.logger.error('Error consuming assets')
this.logger.error(e) this.logger.error(e)
@ -136,38 +133,4 @@ export class Brizo extends Instantiable {
throw new Error('HTTP request failed') throw new Error('HTTP request failed')
} }
} }
private async downloadFile(
url: string,
destination?: string
): Promise<string> {
const response = await this.ocean.utils.fetch.get(url)
if (!response.ok) {
throw new Error('Response error.')
}
let filename
try {
filename = response.headers
.get('content-disposition')
.match(/attachment;filename=(.+)/)[1]
} catch {
throw new Error('Response is not containing file name.')
}
if (destination) {
await new Promise(async (resolve, reject) => {
fs.mkdirSync(destination, { recursive: true })
const fileStream = fs.createWriteStream(
`${destination}${filename}`
)
response.body.pipe(fileStream)
response.body.on('error', reject)
fileStream.on('finish', resolve)
})
return destination
} else {
save(await response.arrayBuffer(), filename)
}
}
} }

View File

@ -1,4 +1,7 @@
import fetch, { BodyInit, RequestInit, Response } from 'node-fetch' import fetch, { BodyInit, RequestInit, Response } from 'node-fetch'
import * as fs from 'fs'
import save = require('save-file')
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract' import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
/** /**
@ -39,6 +42,45 @@ export class WebServiceConnector extends Instantiable {
}) })
} }
public async downloadFile(
url: string,
destination?: string,
index?: number
): Promise<string> {
const response = await this.get(url)
if (!response.ok) {
throw new Error('Response error.')
}
let filename
try {
filename = response.headers
.get('content-disposition')
.match(/attachment;filename=(.+)/)[1]
} catch {
try {
filename = url.split('/').pop()
} catch {
filename = `file${index}`
}
}
if (destination) {
await new Promise(async (resolve, reject) => {
fs.mkdirSync(destination, { recursive: true })
const fileStream = fs.createWriteStream(
`${destination}${filename}`
)
response.body.pipe(fileStream)
response.body.on('error', reject)
fileStream.on('finish', resolve)
})
return destination
} else {
save(await response.arrayBuffer(), filename)
}
}
private async fetch(url: string, opts: RequestInit): Promise<Response> { private async fetch(url: string, opts: RequestInit): Promise<Response> {
const result = await fetch(url, opts) const result = await fetch(url, opts)
if (!result.ok) { if (!result.ok) {