diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 018273e..c51247b 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -13,7 +13,6 @@ import axios, { AxiosError, AxiosHeaders, AxiosProgressEvent } from 'axios'; import ApiVideoError from './ApiVideoError'; import ProblemDetails from './model/ProblemDetails'; import { encode } from 'js-base64'; -import { Readable, Stream } from 'stream'; import AccessToken from './model/AccessToken'; export type QueryOptions = { @@ -107,25 +106,3 @@ export default class HttpClient { } } } - -export async function readableToBuffer(readable: Readable): Promise { - const writableStream = new Stream.Writable({ - defaultEncoding: 'utf-8', - }); - const data: Buffer[] = []; - writableStream._write = (chunk, encoding, next) => { - data.push(Buffer.from(chunk, encoding)); - next(); - }; - - return new Promise((resolve, reject) => { - Stream.pipeline(readable, writableStream, async (err) => { - if (err) { - reject(err); - } - - const buffer = Buffer.concat(data); - resolve(buffer); - }); - }); -} diff --git a/src/api/AnalyticsApi.ts b/src/api/AnalyticsApi.ts index 4ce5a5a..b920854 100644 --- a/src/api/AnalyticsApi.ts +++ b/src/api/AnalyticsApi.ts @@ -9,7 +9,6 @@ * Do not edit the class manually. */ -import { URLSearchParams } from 'url'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; import AnalyticsPlaysResponse from '../model/AnalyticsPlaysResponse'; diff --git a/src/api/CaptionsApi.ts b/src/api/CaptionsApi.ts index 0c66629..2faf670 100644 --- a/src/api/CaptionsApi.ts +++ b/src/api/CaptionsApi.ts @@ -9,17 +9,13 @@ * Do not edit the class manually. */ -import path from 'path'; -import { createReadStream } from 'fs'; -import { URLSearchParams } from 'url'; import FormData from 'form-data'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; import Caption from '../model/Caption'; import CaptionsListResponse from '../model/CaptionsListResponse'; import CaptionsUpdatePayload from '../model/CaptionsUpdatePayload'; -import { Readable } from 'stream'; -import { readableToBuffer } from '../HttpClient'; +import { createBufferFromPartialData } from '../utils'; /** * no description @@ -41,7 +37,7 @@ export default class CaptionsApi { public async upload( videoId: string, language: string, - file: string | Readable | Buffer + file: Buffer | Blob | ArrayBuffer ): Promise { const queryParams: QueryOptions = {}; queryParams.headers = {}; @@ -55,15 +51,8 @@ export default class CaptionsApi { 'Required parameter language was null or undefined when calling upload.' ); } - let fileName = 'file'; - let fileBuffer = file; - if (typeof file === 'string') { - fileName = path.basename(file); - fileBuffer = createReadStream(file); - } - if (file instanceof Readable) { - fileBuffer = await readableToBuffer(file); - } + const fileName = 'file'; + const fileBuffer = createBufferFromPartialData(file); // Path Params const localVarPath = '/videos/{videoId}/captions/{language}' diff --git a/src/api/ChaptersApi.ts b/src/api/ChaptersApi.ts index 5f2b0d1..0ba196a 100644 --- a/src/api/ChaptersApi.ts +++ b/src/api/ChaptersApi.ts @@ -9,16 +9,12 @@ * Do not edit the class manually. */ -import path from 'path'; -import { createReadStream } from 'fs'; -import { URLSearchParams } from 'url'; import FormData from 'form-data'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; import Chapter from '../model/Chapter'; import ChaptersListResponse from '../model/ChaptersListResponse'; -import { Readable } from 'stream'; -import { readableToBuffer } from '../HttpClient'; +import { createBufferFromPartialData } from '../utils'; /** * no description @@ -40,7 +36,7 @@ export default class ChaptersApi { public async upload( videoId: string, language: string, - file: string | Readable | Buffer + file: Buffer | Blob | ArrayBuffer ): Promise { const queryParams: QueryOptions = {}; queryParams.headers = {}; @@ -54,15 +50,8 @@ export default class ChaptersApi { 'Required parameter language was null or undefined when calling upload.' ); } - let fileName = 'file'; - let fileBuffer = file; - if (typeof file === 'string') { - fileName = path.basename(file); - fileBuffer = createReadStream(file); - } - if (file instanceof Readable) { - fileBuffer = await readableToBuffer(file); - } + const fileName = 'file'; + const fileBuffer = createBufferFromPartialData(file); // Path Params const localVarPath = '/videos/{videoId}/chapters/{language}' diff --git a/src/api/LiveStreamsApi.ts b/src/api/LiveStreamsApi.ts index 1aca782..2343bb6 100644 --- a/src/api/LiveStreamsApi.ts +++ b/src/api/LiveStreamsApi.ts @@ -9,9 +9,6 @@ * Do not edit the class manually. */ -import path from 'path'; -import { createReadStream } from 'fs'; -import { URLSearchParams } from 'url'; import FormData from 'form-data'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; @@ -19,8 +16,7 @@ import LiveStream from '../model/LiveStream'; import LiveStreamCreationPayload from '../model/LiveStreamCreationPayload'; import LiveStreamListResponse from '../model/LiveStreamListResponse'; import LiveStreamUpdatePayload from '../model/LiveStreamUpdatePayload'; -import { Readable } from 'stream'; -import { readableToBuffer } from '../HttpClient'; +import { createBufferFromPartialData } from '../utils'; /** * no description @@ -328,7 +324,7 @@ export default class LiveStreamsApi { */ public async uploadThumbnail( liveStreamId: string, - file: string | Readable | Buffer + file: Buffer | Blob | ArrayBuffer ): Promise { const queryParams: QueryOptions = {}; queryParams.headers = {}; @@ -337,15 +333,8 @@ export default class LiveStreamsApi { 'Required parameter liveStreamId was null or undefined when calling uploadThumbnail.' ); } - let fileName = 'file'; - let fileBuffer = file; - if (typeof file === 'string') { - fileName = path.basename(file); - fileBuffer = createReadStream(file); - } - if (file instanceof Readable) { - fileBuffer = await readableToBuffer(file); - } + const fileName = 'file'; + const fileBuffer = createBufferFromPartialData(file); // Path Params const localVarPath = '/live-streams/{liveStreamId}/thumbnail' diff --git a/src/api/PlayerThemesApi.ts b/src/api/PlayerThemesApi.ts index 91738ad..eff1aa8 100644 --- a/src/api/PlayerThemesApi.ts +++ b/src/api/PlayerThemesApi.ts @@ -9,9 +9,6 @@ * Do not edit the class manually. */ -import path from 'path'; -import { createReadStream } from 'fs'; -import { URLSearchParams } from 'url'; import FormData from 'form-data'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; @@ -19,8 +16,7 @@ import PlayerTheme from '../model/PlayerTheme'; import PlayerThemeCreationPayload from '../model/PlayerThemeCreationPayload'; import PlayerThemeUpdatePayload from '../model/PlayerThemeUpdatePayload'; import PlayerThemesListResponse from '../model/PlayerThemesListResponse'; -import { Readable } from 'stream'; -import { readableToBuffer } from '../HttpClient'; +import { createBufferFromPartialData } from '../utils'; /** * no description @@ -302,7 +298,7 @@ export default class PlayerThemesApi { */ public async uploadLogo( playerId: string, - file: string | Readable | Buffer, + file: Buffer | Blob | ArrayBuffer, link?: string ): Promise { const queryParams: QueryOptions = {}; @@ -312,15 +308,8 @@ export default class PlayerThemesApi { 'Required parameter playerId was null or undefined when calling uploadLogo.' ); } - let fileName = 'file'; - let fileBuffer = file; - if (typeof file === 'string') { - fileName = path.basename(file); - fileBuffer = createReadStream(file); - } - if (file instanceof Readable) { - fileBuffer = await readableToBuffer(file); - } + const fileName = 'file'; + const fileBuffer = createBufferFromPartialData(file); // Path Params const localVarPath = '/players/{playerId}/logo' diff --git a/src/api/UploadTokensApi.ts b/src/api/UploadTokensApi.ts index 2918bd7..ecf5f93 100644 --- a/src/api/UploadTokensApi.ts +++ b/src/api/UploadTokensApi.ts @@ -9,7 +9,6 @@ * Do not edit the class manually. */ -import { URLSearchParams } from 'url'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; import TokenCreationPayload from '../model/TokenCreationPayload'; diff --git a/src/api/VideosApi.ts b/src/api/VideosApi.ts index 5206408..dee9fd8 100644 --- a/src/api/VideosApi.ts +++ b/src/api/VideosApi.ts @@ -9,9 +9,6 @@ * Do not edit the class manually. */ -import path from 'path'; -import { existsSync, statSync, createReadStream } from 'fs'; -import { URLSearchParams } from 'url'; import FormData from 'form-data'; import ObjectSerializer from '../ObjectSerializer'; import HttpClient, { QueryOptions } from '../HttpClient'; @@ -23,8 +20,7 @@ import VideoThumbnailPickPayload from '../model/VideoThumbnailPickPayload'; import VideoUpdatePayload from '../model/VideoUpdatePayload'; import VideosListResponse from '../model/VideosListResponse'; import UploadProgressEvent from '../model/UploadProgressEvent'; -import { Readable } from 'stream'; -import { readableToBuffer } from '../HttpClient'; +import { createBufferFromPartialData, getFileSize } from '../utils'; /** * no description @@ -103,21 +99,21 @@ export default class VideosApi { } uploadPart( - file: string, + file: Buffer | Blob | ArrayBuffer, progressListener?: (event: UploadProgressEvent) => void ) { return this.upload(file, false, progressListener); } uploadLastPart( - file: string, + file: Buffer | Blob | ArrayBuffer, progressListener?: (event: UploadProgressEvent) => void ) { return this.upload(file, true, progressListener); } upload( - file: string, + file: Buffer | Blob | ArrayBuffer, isLast: boolean, progressListener?: (event: UploadProgressEvent) => void ) { @@ -130,11 +126,7 @@ export default class VideosApi { ); } - if (!existsSync(file)) { - throw new Error(`${file} must be a readable source file`); - } - - const length = statSync(file).size; + const length = getFileSize(file); if (length <= 0) { throw new Error(`${file} is empty`); } @@ -147,8 +139,8 @@ export default class VideosApi { const formData = new FormData(); - const filename = path.basename(file); - formData.append(filename, createReadStream(file), filename); + const filename = 'file'; + formData.append(filename, createBufferFromPartialData(file), filename); queryParams.body = formData; queryParams.headers['Content-Range'] = `part ${this.currentPart}/${ isLast ? this.currentPart : '*' @@ -212,7 +204,7 @@ The latter allows you to split a video source into X chunks and send those chunk */ public async upload( videoId: string, - file: string, + file: Buffer | Blob | ArrayBuffer, progressListener?: (event: UploadProgressEvent) => void ): Promise