From 2aba26583035542daf60f0553927edd6e380a5ff Mon Sep 17 00:00:00 2001 From: oshinongit Date: Mon, 2 Oct 2023 14:25:52 +0200 Subject: [PATCH] feat: download and process files --- src/pipelines/encore/encore-pipeline.ts | 114 +++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/src/pipelines/encore/encore-pipeline.ts b/src/pipelines/encore/encore-pipeline.ts index ebcb94b..914f681 100644 --- a/src/pipelines/encore/encore-pipeline.ts +++ b/src/pipelines/encore/encore-pipeline.ts @@ -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 { @@ -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 { + + 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. @@ -149,4 +184,81 @@ export default class EncorePipeline implements Pipeline { logger.error(error); }); } -} \ No newline at end of file + + async fetchFile(url:string): Promise { + + 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((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 { + 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(); \ No newline at end of file