Skip to content

Commit

Permalink
feat: download and process files
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Oct 2, 2023
1 parent 9bf7b61 commit 2aba265
Showing 1 changed file with 113 additions and 1 deletion.
114 changes: 113 additions & 1 deletion src/pipelines/encore/encore-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { EncorePipelineConfiguration } from './encore-pipeline-configuration';
import { QualityAnalysisModel } from '../../models/quality-analysis-model';
import { EncoreInstance } from '../../models/encoreInstance';
import logger from '../../logger';
import { config } from 'yargs';
import fs from 'fs';
const { Readable } = require('stream');
const { finished } = require('stream/promises');

export default class EncorePipeline implements Pipeline {

Expand Down Expand Up @@ -68,6 +72,37 @@ export default class EncorePipeline implements Pipeline {
});
}

/**
* Attempts to get an Encore Instance.
* @param encoreInstance The Encore Instance to be deleted.
*/
async getEncoreInstance(encoreInstance: EncoreInstance): Promise<any> {

const url = encoreInstance.url;
const headerObj = {
'accept': 'application/json',
'x-jwt': `Bearer ${this.configuration.token}`,
};
const headers = new Headers(headerObj);

const request = new Request(url, {
method: "GET",
headers: headers,
});

let responseObj;

fetch(request).then(response => response.json())
.then(data => {
responseObj = data;
})
.catch(error => {
logger.error(error);
});
logger.info(responseObj.status)
}


/**
* Attempts to delete an Encore Instance.
* @param encoreInstance The Encore Instance to be deleted.
Expand Down Expand Up @@ -149,4 +184,81 @@ export default class EncorePipeline implements Pipeline {
logger.error(error);
});
}
}

async fetchFile(url:string): Promise<Blob> {

const headerObj = {
'accept': 'application/json',
'x-jwt': `Bearer ${this.configuration.token}`,
'Content-Type': 'application/json',
};
const headers = new Headers(headerObj);

const request = new Request(url, {
headers: headers,
});

const response: Response = await fetch(request);

if (!response.ok) {
throw new Error('Failed to fetch file stream: ' + response.statusText);
}

return new Promise<Blob>((resolve, reject) => {
const responseBody = response.body;
const responseBlob = response.blob();

if (!responseBody) {
reject(new Error('Response body is null'));
return;
}
return responseBlob;
});
}






}

async function downloadFile(url, configuration): Promise<void> {
const headerObj = {
'accept': 'application/json',
'x-jwt': `Bearer ${configuration.token}`,
'Content-Type': 'application/json',
};
const headers = new Headers(headerObj);

const request = new Request(url, {
headers: headers,
});

const stream = fs.createWriteStream('output.mp4');
const { body } = await fetch(request);
await finished(Readable.fromWeb(body).pipe(stream));
}

async function dummymain(){

const configuration: EncorePipelineConfiguration = {
apiAddress: "https://api-encore.stage.osaas.io",
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2OTUxMjkyNTAsImN1c3RvbWVyIjoib2xpdmVydGVzdCIsImVtYWlsIjoib2xpdmVydGVzdG1haWwiLCJsaW1pdCI6M30.HnFLzkl2-QQU28kNZW1SAgO6l69mslJaCwvUxITzfJY",
instanceId: "newtest",
profile: "program",
outputFolder: "/usercontent/demo",
baseName: "_demo",
inputs: ["https://testcontent.eyevinn.technology/mp4/stswe-tvplus-promo.mp4"],
duration: 10,
priority: 0,
};

//const pipeline: EncorePipeline = new EncorePipeline(configuration);
//pipeline.createEncoreInstance(configuration.apiAddress, configuration.token, configuration.instanceId, configuration.profile);
const fileUrl:string = "https://olivertest-newtest.encore.stage.osaas.io/usercontent/demo/_demo_x264_324.mp4"
downloadFile(fileUrl, configuration);

}

dummymain();

0 comments on commit 2aba265

Please sign in to comment.