soundcloudnodejs.js
function addTrack(options) {
    return new Promise(function (resolve, reject) {
        var form = new FormData();

        form.append('format', 'json');
        if (!options.title) {
            reject('Error while addTrack track options.title is required but is null');
        } else {
            form.append('track[title]', options.title);
        }
        if (!options.description) {
            reject('Error  while addTrack track options.description is required but is null');
        } else {
            form.append('track[description]', options.description);
        }
        if (!options.genre) {
            reject('Error  while addTrack track options.genre is required but is null');
        } else {
            form.append('track[genre]', options.genre);
        }

        var exist_artwork_data = fs.existsSync(options.artwork_data);
        if (exist_artwork_data) {
            form.append('track[artwork_data]', fs.createReadStream(options.artwork_data));
        }

        if (options.tag_list) {
            form.append('track[tag_list]', options.tag_list);
        }

        form.append('track[sharing]', options.sharing);

        if (!options.oauth_token) {
            reject('Error  while addTrack track oauth_token is required but is null');
        } else {
            form.append('oauth_token', options.oauth_token);
        }


        if (!options.asset_data) {
            reject('Error  while addTrack track options.asset_data is required but is null');
        } else {

            var exist_asset_data = fs.existsSync(options.asset_data);
            if (DEBUG_MODE_ON) {
                console.log("addTrack, exist_asset_data, ", exist_asset_data);
            }

            if (exist_asset_data) {
                form.append('track[asset_data]', fs.createReadStream(options.asset_data));
            } else {
                reject('Error addTrack could not find options.asset_data --> fs.createReadStream(options.asset_data): ' + exist_asset_data);
            }
        }

        form.getLength(function (err, length) {


            fetch('https://api.soundcloud.com/tracks', {
                method: 'POST',
                body: form,
                headers: {'content-length': length}
            }).then(function (res) {
                return res.json();
            }).then(function (json) {
                if (DEBUG_MODE_ON) {
                    console.log('addTrack successful');
                }
                resolve(json);
            }).catch(function (e) {
                reject(e);
            });

        });

    });
}
function addTrack(options) {
    return new Promise(function (resolve, reject) {
        var form = new FormData();

        form.append('format', 'json');
        if (!options.title) {
            reject('Error while addTrack track options.title is required but is null');
        } else {
            form.append('track[title]', options.title);
        }
        if (!options.description) {
            reject('Error  while addTrack track options.description is required but is null');
        } else {
            form.append('track[description]', options.description);
        }
        if (!options.genre) {
            reject('Error  while addTrack track options.genre is required but is null');
        } else {
            form.append('track[genre]', options.genre);
        }

        var exist_artwork_data = fs.existsSync(options.artwork_data);
        if (exist_artwork_data) {
            form.append('track[artwork_data]', fs.createReadStream(options.artwork_data));
        }

        if (options.tag_list) {
            form.append('track[tag_list]', options.tag_list);
        }

        form.append('track[sharing]', options.sharing);

        if (!options.oauth_token) {
            reject('Error  while addTrack track oauth_token is required but is null');
        } else {
            form.append('oauth_token', options.oauth_token);
        }


        if (!options.asset_data) {
            reject('Error  while addTrack track options.asset_data is required but is null');
        } else {

            var exist_asset_data = fs.existsSync(options.asset_data);
            if (DEBUG_MODE_ON) {
                console.log("addTrack, exist_asset_data, ", exist_asset_data);
            }

            if (exist_asset_data) {
                form.append('track[asset_data]', fs.createReadStream(options.asset_data));
            } else {
                reject('Error addTrack could not find options.asset_data --> fs.createReadStream(options.asset_data): ' + exist_asset_data);
            }
        }

        form.getLength(function (err, length) {


            fetch('https://api.soundcloud.com/tracks', {
                method: 'POST',
                body: form,
                headers: {'content-length': length}
            }).then(function (res) {
                return res.json();
            }).then(function (json) {
                if (DEBUG_MODE_ON) {
                    console.log('addTrack successful');
                }
                resolve(json);
            }).catch(function (e) {
                reject(e);
            });

        });

    });
}
function addTrack ( options ) { return new Promise ( function ( resolve , reject ) { var form = new FormData ( ) ; form . append ( 'format' , 'json' ) ; if ( ! options . title ) { reject ( 'Error while addTrack track options.title is required but is null' ) ; } else { form . append ( 'track[title]' , options . title ) ; } if ( ! options . description ) { reject ( 'Error  while addTrack track options.description is required but is null' ) ; } else { form . append ( 'track[description]' , options . description ) ; } if ( ! options . genre ) { reject ( 'Error  while addTrack track options.genre is required but is null' ) ; } else { form . append ( 'track[genre]' , options . genre ) ; } var exist_artwork_data = fs . existsSync ( options . artwork_data ) ; if ( exist_artwork_data ) { form . append ( 'track[artwork_data]' , fs . createReadStream ( options . artwork_data ) ) ; } if ( options . tag_list ) { form . append ( 'track[tag_list]' , options . tag_list ) ; } form . append ( 'track[sharing]' , options . sharing ) ; if ( ! options . oauth_token ) { reject ( 'Error  while addTrack track oauth_token is required but is null' ) ; } else { form . append ( 'oauth_token' , options . oauth_token ) ; } if ( ! options . asset_data ) { reject ( 'Error  while addTrack track options.asset_data is required but is null' ) ; } else { var exist_asset_data = fs . existsSync ( options . asset_data ) ; if ( DEBUG_MODE_ON ) { console . log ( "addTrack, exist_asset_data, " , exist_asset_data ) ; } if ( exist_asset_data ) { form . append ( 'track[asset_data]' , fs . createReadStream ( options . asset_data ) ) ; } else { reject ( 'Error addTrack could not find options.asset_data --> fs.createReadStream(options.asset_data): ' + exist_asset_data ) ; } } form . getLength ( function ( err , length ) { fetch ( 'https://api.soundcloud.com/tracks' , { method : 'POST' , body : form , headers : { 'content-length' : length } } ) . then ( function ( res ) { return res . json ( ) ; } ) . then ( function ( json ) { if ( DEBUG_MODE_ON ) { console . log ( 'addTrack successful' ) ; } resolve ( json ) ; } ) . catch ( function ( e ) { reject ( e ) ; } ) ; } ) ; } ) ; }
Add a new track
@param options
@param cb
@returns {*}
Add a new track