From 9235a8de1c9cf629296fd6c0c4230a5116d56005 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Fri, 27 Sep 2024 22:02:06 +0700 Subject: [PATCH] feat: fix linkedin chunks upload --- libraries/helpers/src/utils/read.or.fetch.ts | 7 +++- .../integrations/social/linkedin.provider.ts | 32 +++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/libraries/helpers/src/utils/read.or.fetch.ts b/libraries/helpers/src/utils/read.or.fetch.ts index 10c93904..f5b2338b 100644 --- a/libraries/helpers/src/utils/read.or.fetch.ts +++ b/libraries/helpers/src/utils/read.or.fetch.ts @@ -1,8 +1,13 @@ import {readFileSync} from "fs"; +import axios from 'axios'; export const readOrFetch = async (path: string) => { if (path.indexOf('http') === 0) { - return (await fetch(path)).arrayBuffer(); + return (await axios({ + url: path, + method: 'GET', + responseType: 'arraybuffer', + })).data; } return readFileSync(path); diff --git a/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts b/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts index 177b5994..1422505b 100644 --- a/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts @@ -231,21 +231,25 @@ export class LinkedinProvider extends SocialAbstract implements SocialProvider { const sendUrlRequest = uploadInstructions?.[0]?.uploadUrl || uploadUrl; const finalOutput = video || image; - const upload = await this.fetch(sendUrlRequest, { - method: 'PUT', - headers: { - 'X-Restli-Protocol-Version': '2.0.0', - 'LinkedIn-Version': '202402', - Authorization: `Bearer ${accessToken}`, - ...(fileName.indexOf('mp4') > -1 - ? { 'Content-Type': 'application/octet-stream' } - : {}), - }, - body: picture, - }); + const etags = []; + for (let i = 0; i < picture.length; i += 1024 * 1024 * 2) { + const upload = await this.fetch(sendUrlRequest, { + method: 'PUT', + headers: { + 'X-Restli-Protocol-Version': '2.0.0', + 'LinkedIn-Version': '202402', + Authorization: `Bearer ${accessToken}`, + ...(fileName.indexOf('mp4') > -1 + ? { 'Content-Type': 'application/octet-stream' } + : {}), + }, + body: picture.slice(i, i + 1024 * 1024 * 2), + }); + + etags.push(upload.headers.get('etag')); + } if (fileName.indexOf('mp4') > -1) { - const etag = upload.headers.get('etag'); const a = await this.fetch( 'https://api.linkedin.com/rest/videos?action=finalizeUpload', { @@ -254,7 +258,7 @@ export class LinkedinProvider extends SocialAbstract implements SocialProvider { finalizeUploadRequest: { video, uploadToken: '', - uploadedPartIds: [etag], + uploadedPartIds: etags, }, }), headers: {