Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT REVIEW] Add download track feature #150

Merged
merged 4 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 32 additions & 46 deletions libs/src/api/file.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
const { Base, Services } = require('./base')
const axios = require('axios')
const Utils = require('../utils')

const CancelToken = axios.CancelToken

// Public gateways to send requests to, ordered by precidence.
const publicGateways = [
'https://ipfs.io/ipfs/',
'https://cloudflare-ipfs.com/ipfs/'
]

// Races requests for file content
async function raceRequests (
urls,
callback
) {
const sources = []
const requests = urls.map(async (url, i) => {
const source = CancelToken.source()
sources.push(source)

// Slightly offset requests by their order, so:
// 1. We try public gateways first
// 2. We give requests the opportunity to get canceled if other's are very fast
await Utils.wait(100 * i)

return new Promise((resolve, reject) => {
axios({
method: 'get',
url,
responseType: 'blob',
cancelToken: source.token
})
.then(response => {
resolve({
blob: response,
url
})
})
.catch((thrown) => {
reject(thrown)
// no-op.
// If debugging `axios.isCancel(thrown)`
// can be used to check if the throw was from a cancel.
})
})
})
const response = await Utils.promiseFight(requests)
sources.forEach(source => {
source.cancel('Fetch already succeeded')
})
callback(response.url)
return response.blob
const downloadURL = (url) => {
if (document) {
const link = document.createElement('a')
link.href = url
link.click()
}
throw new Error('No body document found')
}

class File extends Base {
Expand All @@ -70,7 +32,31 @@ class File extends Base {
const urls = gateways.map(gateway => `${gateway}${cid}`)

try {
return raceRequests(urls, callback)
return Utils.raceRequests(urls, callback, {
method: 'get',
responseType: 'blob'
})
} catch (e) {
throw new Error(`Failed to retrieve ${cid}`)
}
}

/**
* Fetches a file from IPFS with a given CID. Follows the same pattern
* as fetchCID, but resolves with a download of the file rather than
* returning the response content.
* @param {string} cid IPFS content identifier
* @param {Array<string>} creatorNodeGateways fallback ipfs gateways from creator nodes
*/
async downloadCID (cid, creatorNodeGateways) {
const gateways = publicGateways
.concat(creatorNodeGateways)
const urls = gateways.map(gateway => `${gateway}${cid}`)

try {
return Utils.raceRequests(urls, downloadURL, {
method: 'head'
})
} catch (e) {
throw new Error(`Failed to retrieve ${cid}`)
}
Expand Down
10 changes: 10 additions & 0 deletions libs/src/api/track.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Base, Services } = require('./base')
const CreatorNode = require('../services/creatorNode')
const Utils = require('../utils')
const retry = require('async-retry')

Expand Down Expand Up @@ -69,6 +70,15 @@ class Tracks extends Base {
return this.identityService.getTrackListens(timeFrame, idsArray, startTime, endTime, limit, offset)
}

/**
* Checks if a download is available from provided creator node endpoints
* @param {string} creatorNodeEndpoints creator node endpoints
* @param {number} trackId
*/
async checkIfDownloadAvailable (trackId, creatorNodeEndpoints) {
return CreatorNode.checkIfDownloadAvailable(trackId, creatorNodeEndpoints)
}

/* ------- SETTERS ------- */

/**
Expand Down
25 changes: 25 additions & 0 deletions libs/src/services/creatorNode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ class CreatorNode {
*/
static getSecondaries (endpoints) { return endpoints ? endpoints.split(',').slice(1) : [] }

/**
* Checks if a download is available from provided creator node endpoints
* @param {string} endpoints creator node endpoints
* @param {number} trackId
*/
static async checkIfDownloadAvailable (endpoints, trackId) {
for (const endpoint in endpoints.split(',')) {
raymondjacobson marked this conversation as resolved.
Show resolved Hide resolved
raymondjacobson marked this conversation as resolved.
Show resolved Hide resolved
try {
const res = await axios({
baseURL: endpoint,
url: '/download/',
params: {
trackId
}
})
if (res.cid) return res.cid
} catch (e) {
console.log(`Unable to download ${trackId} from ${endpoint}`)
// continue
}
}
// Download is not available, clients should display "processing"
return null
}

/* -------------- */

constructor (web3Manager, creatorNodeEndpoint, isServer, userStateManager, lazyConnect) {
Expand Down
53 changes: 53 additions & 0 deletions libs/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,59 @@ class Utils {

return timings.sort((a, b) => a.millis - b.millis)
}

// Races requests for file content
/**
* Races multiple requests
* @param {*} urls
* @param {*} callback invoked with the first successful url
* @param {object} axiosConfig extra axios config for each request
*/
static async raceRequests (
urls,
callback,
axiosConfig
) {
const CancelToken = axios.CancelToken

const sources = []
const requests = urls.map(async (url, i) => {
const source = CancelToken.source()
sources.push(source)

// Slightly offset requests by their order, so:
// 1. We try public gateways first
// 2. We give requests the opportunity to get canceled if other's are very fast
await Utils.wait(100 * i)

return new Promise((resolve, reject) => {
axios({
method: 'get',
url,
cancelToken: source.token,
...axiosConfig
})
.then(response => {
resolve({
blob: response,
url
})
})
.catch((thrown) => {
reject(thrown)
// no-op.
// If debugging `axios.isCancel(thrown)`
// can be used to check if the throw was from a cancel.
})
})
})
const response = await Utils.promiseFight(requests)
sources.forEach(source => {
source.cancel('Fetch already succeeded')
})
callback(response.url)
return response.blob
}
}

module.exports = Utils