From 08e67e959f80d9f6cba6a282b3e2dc075b1218e2 Mon Sep 17 00:00:00 2001 From: emadehsan Date: Thu, 17 May 2018 20:24:54 +0500 Subject: [PATCH] surrond all calls to methods in try catch --- index.js | 226 ++++++++++++++++++++++++++++-------------------------- runner.js | 5 ++ 2 files changed, 122 insertions(+), 109 deletions(-) diff --git a/index.js b/index.js index 6ec9a1b..47befab 100644 --- a/index.js +++ b/index.js @@ -56,119 +56,127 @@ function delay(time) { async function transcode (filename, filepath) { setupVariables(); - /* - * Retrieve Access Token - */ - let tokenResp = await REST.getAADAccessToken(VARS.AzureADSTSEndpoint, VARS.ClientID, VARS.ClientSecret); - VARS.AccessToken = tokenResp.access_token; - console.log('Fetched AccessToken!'); - - /* - * Access Policy with write permissions. Fetch the policy if one exists with the same name - * or create it - */ - let newPolicyName = 'TravUploadPolicy'; - let aPolicy = await REST.fetchOrCreateAccessPolicy(VARS.RESTAPIEndpoint, VARS.AccessToken, newPolicyName, REST.WRITE_POLICY); - VARS.UploadAccessPolicyId = aPolicy.Id; - console.log('Fetched Access Policy: ', VARS.UploadAccessPolicyId); - - /* - * Prefix assets with a name and suffix with current time - */ - let assetName = 'TravAsset_' + (new Date()).getTime(); - let asset = await REST.createAsset(VARS.RESTAPIEndpoint, VARS.AccessToken, assetName); - console.log('Asset created: ', asset.Id, asset.Name); - VARS.LastAssetId = asset.Id; - - /* - * SAS Locator for uploading - */ - const locator = await REST.fetchLocator(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.UploadAccessPolicyId, VARS.LastAssetId, REST.WRITE_LOCATOR); - console.log('Fetched locator: ', locator.Id); - - /* - * Upload URL - */ - VARS.UploadURL = locator.BaseUri + '/' + filename + locator.ContentAccessComponent; - console.log('UploadURL: ', VARS.UploadURL); - - console.log('Uploading file: ', filepath, ' ...'); - let isUploaded = await REST.uploadFile(VARS.UploadURL, filepath); - - if (!isUploaded) { - console.log('File Upload ERRORED'); - return; - } + try { + /* + * Retrieve Access Token + */ + let tokenResp = await REST.getAADAccessToken(VARS.AzureADSTSEndpoint, VARS.ClientID, VARS.ClientSecret); + VARS.AccessToken = tokenResp.access_token; + console.log('Fetched AccessToken!'); + + /* + * Access Policy with write permissions. Fetch the policy if one exists with the same name + * or create it + */ + let newPolicyName = 'TravUploadPolicy'; + let aPolicy = await REST.fetchOrCreateAccessPolicy(VARS.RESTAPIEndpoint, VARS.AccessToken, newPolicyName, REST.WRITE_POLICY); + VARS.UploadAccessPolicyId = aPolicy.Id; + console.log('Fetched Access Policy: ', VARS.UploadAccessPolicyId); + + /* + * Prefix assets with a name and suffix with current time + */ + let assetName = 'TravAsset_' + (new Date()).getTime(); + let asset = await REST.createAsset(VARS.RESTAPIEndpoint, VARS.AccessToken, assetName); + console.log('Asset created: ', asset.Id, asset.Name); + VARS.LastAssetId = asset.Id; + + /* + * SAS Locator for uploading + */ + const locator = await REST.fetchLocator(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.UploadAccessPolicyId, VARS.LastAssetId, REST.WRITE_LOCATOR); + console.log('Fetched locator: ', locator.Id); + + /* + * Upload URL + */ + VARS.UploadURL = locator.BaseUri + '/' + filename + locator.ContentAccessComponent; + console.log('UploadURL: ', VARS.UploadURL); + + console.log('Uploading file: ', filepath, ' ...'); + let isUploaded = await REST.uploadFile(VARS.UploadURL, filepath); + + if (!isUploaded) { + console.log('File Upload ERRORED'); + return null; + } - /* - * create file info - */ - let fileInfoCreated = await REST.createFileInfos(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.LastAssetId); - console.log('Created file infos: ', fileInfoCreated); - - /* - * no need to request media processor each time. We already know the Id of Standard Encoding Media Processor - * nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56 - */ - // let mediaProcessor = await REST.getMediaProcessor(VARS.RESTAPIEndpoint, VARS.AccessToken); - // console.log('Media Processor: ', mediaProcessor); - - let encodingJob = await REST.createJob(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.LastAssetId, assetName, VARS.MediaProcessorId); - - let jobId = encodingJob.d.Id; - let outputMediaAsset = encodingJob.d.OutputMediaAssets; - /* - * Format: of OutputMediaAssets: - OutputMediaAssets: { - __deferred: { - uri: '/OutputMediaAssets' + console.log('File uploaded!'); + + /* + * create file info + */ + let fileInfoCreated = await REST.createFileInfos(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.LastAssetId); + console.log('Created file infos: ', fileInfoCreated); + + /* + * no need to request media processor each time. We already know the Id of Standard Encoding Media Processor + * nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56 + */ + // let mediaProcessor = await REST.getMediaProcessor(VARS.RESTAPIEndpoint, VARS.AccessToken); + // console.log('Media Processor: ', mediaProcessor); + + let encodingJob = await REST.createJob(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.LastAssetId, assetName, VARS.MediaProcessorId); + + let jobId = encodingJob.d.Id; + let outputMediaAsset = encodingJob.d.OutputMediaAssets; + /* + * Format: of OutputMediaAssets: + OutputMediaAssets: { + __deferred: { + uri: '/OutputMediaAssets' + } } - } - */ - let outputAssetUrl = outputMediaAsset.__deferred.uri; - - console.log('Encoding job started: ', jobId); - - /* - * Actively tracking job state - */ - let isDone = false; - do { - console.log('Waiting for few sec before checking job state...'); - await delay(5000); - let jobState = await REST.monitorJob(VARS.RESTAPIEndpoint, VARS.AccessToken, jobId); - - if (jobState.d.State == 3) { - console.log('Job done.'); - isDone = true; + */ + let outputAssetUrl = outputMediaAsset.__deferred.uri; + + console.log('Encoding job started: ', jobId); + + /* + * Actively tracking job state + */ + let isDone = false; + do { + console.log('Waiting for few sec before checking job state...'); + await delay(5000); + let jobState = await REST.monitorJob(VARS.RESTAPIEndpoint, VARS.AccessToken, jobId); + + if (jobState.d.State == 3) { + console.log('Job done.'); + isDone = true; + } + } while(!isDone); + + /* + * Read Access Policy + */ + let readPolicyName = 'TravReadPolicy'; + let readPolicy = await REST.fetchOrCreateAccessPolicy(VARS.RESTAPIEndpoint, VARS.AccessToken, readPolicyName, REST.READ_POLICY); + VARS.ReadAccessPolicyId = readPolicy.Id; + console.log('Read Access Policy: ', readPolicy.Name, readPolicy.Id); + + let outputAsset = await REST.getOutputAsset(outputAssetUrl, VARS.AccessToken); + if (outputAsset.value && outputAsset.value.length > 0) { + outputAsset = outputAsset.value[0]; + console.log('Output Asset: ', outputAsset.Id); } - } while(!isDone); - - /* - * Read Access Policy - */ - let readPolicyName = 'TravReadPolicy'; - let readPolicy = await REST.fetchOrCreateAccessPolicy(VARS.RESTAPIEndpoint, VARS.AccessToken, readPolicyName, REST.READ_POLICY); - VARS.ReadAccessPolicyId = readPolicy.Id; - console.log('Read Access Policy: ', readPolicy.Name, readPolicy.Id); - - let outputAsset = await REST.getOutputAsset(outputAssetUrl, VARS.AccessToken); - if (outputAsset.value && outputAsset.value.length > 0) { - outputAsset = outputAsset.value[0]; - console.log('Output Asset: ', outputAsset.Id); - } - /* - * Streaming locator - */ - const streamingLocator = await REST.fetchLocator(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.ReadAccessPolicyId, outputAsset.Id, REST.READ_LOCATOR); - console.log('Streaming locator created: ', streamingLocator.Name, streamingLocator.Id); - console.log(streamingLocator); - - /* - * return streaming path for DASH - */ - return streamingLocator.Path; + /* + * Streaming locator + */ + const streamingLocator = await REST.fetchLocator(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.ReadAccessPolicyId, outputAsset.Id, REST.READ_LOCATOR); + console.log('Streaming locator created: ', streamingLocator.Name, streamingLocator.Id); + console.log(streamingLocator); + + /* + * return streaming path for DASH + */ + return streamingLocator.Path; + } catch (e) { + console.log('ERROR: ', e); + + return null; + } } module.exports = { diff --git a/runner.js b/runner.js index 60e517f..8a4c8d8 100644 --- a/runner.js +++ b/runner.js @@ -7,6 +7,11 @@ async function run() { let filepath = './media/sample.mp4'; let streamingUrl = await GaleCrater.transcode(filename, filepath); + + if (streamingUrl == null) { + console.log('Operation could NOT be completed...'); + return null; + } filename = filename.replace('.mp4', ''); let dashUrl = streamingUrl + filename + '.ism/Manifest(format=mpd-time-csf)';