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

Expose functions to upload track to cnode / register tracks on chain … #78

Merged
merged 6 commits into from
Sep 22, 2019
Merged
Changes from 3 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
86 changes: 86 additions & 0 deletions libs/src/api/track.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Base, Services } = require('./base')
const Utils = require('../utils')
const retry = require('async-retry')

class Tracks extends Base {
/* ------- GETTERS ------- */
Expand Down Expand Up @@ -124,6 +125,91 @@ class Tracks extends Base {
return trackId
}

/**
* Takes in a readable stream if isServer is true, or a file reference if isServer is
hareeshnagaraj marked this conversation as resolved.
Show resolved Hide resolved
* false.
* WARNING: Uploads file to creator node, but does not call contracts
* Please pair this with the addTracksToChainAndCnode
*/
async uploadTrackContentToCreatorNode (
trackFile,
coverArtFile,
metadata,
onProgress
) {
this.REQUIRES(Services.CREATOR_NODE)
this.FILE_IS_VALID(trackFile)

if (coverArtFile) this.FILE_IS_VALID(coverArtFile)

this.IS_OBJECT(metadata)

const owner = this.userStateManager.getCurrentUser()
if (!owner.user_id) {
throw new Error('No users loaded for this wallet')
}

metadata.owner_id = owner.user_id
this._validateTrackMetadata(metadata)

// Upload metadata
const { metadataMultihash, metadataFileUUID } = await retry(async (bail, num) => {
return this.creatorNode.uploadTrackContent(
trackFile,
coverArtFile,
metadata,
onProgress)
}, {
// Retry function 3x
// 1st retry delay = 500ms, 2nd = 1500ms, 3rd...nth retry = 4000 ms (capped)
minTimeout: 500,
maxTimeout: 4000,
factor: 3,
retries: 3,
onRetry: (err, i) => {
if (err) {
// eslint-disable-next-line no-console
console.log('Retry error : ', err)
}
}
})
console.error('New upload func')
return { metadataMultihash, metadataFileUUID }
}

/**
* Takes an array of [{metadataMultihash, metadataFileUUID}, {}, ]
* Adds tracks to chain for this user
* Associates tracks with user on creatorNode
*/
async addTracksToChainAndCnode (trackMultihashAndUUIDList) {
this.REQUIRES(Services.CREATOR_NODE)
const owner = this.userStateManager.getCurrentUser()
if (!owner.user_id) {
throw new Error('No users loaded for this wallet')
}

let trackIds = (await Promise.all(
trackMultihashAndUUIDList.map(async trackInfo => {
hareeshnagaraj marked this conversation as resolved.
Show resolved Hide resolved
const metadataMultihash = trackInfo.metadataMultihash
const metadataFileUUID = trackInfo.metadataFileUUID
// Write metadata to chain
const multihashDecoded = Utils.decodeMultihash(metadataMultihash)
const { txReceipt, trackId } = await this.contracts.TrackFactoryClient.addTrack(
owner.user_id,
multihashDecoded.digest,
multihashDecoded.hashFn,
multihashDecoded.size
)
// Associate the track id with the file metadata and block number
await this.creatorNode.associateTrack(trackId, metadataFileUUID, txReceipt.blockNumber)
return trackId
})
))

return trackIds
}

/**
* Updates an existing track given metadata. This function expects that all associated files
* such as track content, cover art are already on creator node.
Expand Down