Skip to content

Commit

Permalink
Fix legacy images (#1023)
Browse files Browse the repository at this point in the history
* Fix legacy images

* 1.0.11

* Fix lint
  • Loading branch information
raymondjacobson committed Oct 31, 2020
1 parent 4681999 commit 84d0617
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@audius/libs",
"version": "1.0.10",
"version": "1.0.11",
"description": "",
"main": "src/index.js",
"browser": {
Expand Down
24 changes: 22 additions & 2 deletions libs/src/api/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ if (urlJoin && urlJoin.default) urlJoin = urlJoin.default
const { Base, Services } = require('./base')
const { raceRequests } = require('../utils/network')
const retry = require('async-retry')
const FETCH_CID_TIMEOUT_MS = 20 /* sec */ * 1000 /* millis */

// Public gateways to send requests to, ordered by precidence.
const publicGateways = [
Expand Down Expand Up @@ -48,9 +47,30 @@ class File extends Base {
const { response } = await raceRequests(urls, callback, {
method: 'get',
responseType: 'blob'
}, FETCH_CID_TIMEOUT_MS)
}, /* timeout */ null)
if (!response) throw new Error(`Could not fetch ${cid}`)
return response
} catch (e) {
// TODO: Remove this fallback logic when no more users/tracks/playlists
// contain "legacy" image formats (no dir cid)
if (cid.includes('/')) { // dirCID -- an image
console.debug(`Attempted to fetch image ${cid} via legacy method`)
// Try legacy image format
// Lop off anything like /480x480.jpg in the CID
const legacyUrls = gateways.map(gateway => urlJoin(gateway, cid.split('/')[0]))
try {
const { response } = await raceRequests(legacyUrls, callback, {
method: 'get',
responseType: 'blob'
}, /* timeout */ null)
if (!response) throw new Error(`Could not fetch ${cid} via legacy method`)
return response
} catch (e) {
throw new Error(`Failed to retrieve ${cid} by legacy method`)
}
}

// Throw so we can retry
throw new Error(`Failed to retrieve ${cid}`)
}
}, {
Expand Down
4 changes: 3 additions & 1 deletion libs/src/utils/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ async function raceRequests (
})
})
})
requests.push(Utils.wait(timeout))
if (timeout !== null) {
requests.push(Utils.wait(timeout))
}
let response
let errored
try {
Expand Down

0 comments on commit 84d0617

Please sign in to comment.