π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
fix: address AI review findings on streamed media uploads
Nevo David1b121d0cf10e
diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.tsindex 69d1bb92f..543c53eda 100644--- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts+++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts@@ -6,6 +6,7 @@ import { readOrFetch } from '@gitroom/helpers/utils/read.or.fetch';import { getSsrfSafeDispatcher } from '@gitroom/nestjs-libraries/dtos/webhooks/ssrf.safe.dispatcher';import sharp from 'sharp';import { createReadStream, statSync } from 'fs';+import { Readable } from 'stream';export type ValidityMedia = {path: string;@@ -207,7 +208,8 @@ export abstract class SocialAbstract {protected async mediaChunk(path: string,start: number,- end: number+ end: number,+ identifier = ''): Promise<Buffer> {if (path.indexOf('http') === 0) {const response = await fetch(path, {@@ -219,7 +221,7 @@ export abstract class SocialAbstract {// upload corrupted chunks.if (response.status !== 206) {throw new BadBody(- '',+ identifier,'{}',Buffer.from('{}'),`Media server did not honor the range request (status ${response.status})`@@ -237,6 +239,33 @@ export abstract class SocialAbstract {});}+ // Opens the media as a Node stream. The media path is user-influenced, so+ // remote URLs go through the same SSRF-safe dispatcher as every other+ // outbound request - never fetch these with plain axios/fetch.+ protected async mediaStream(+ path: string,+ identifier = ''+ ): Promise<Readable> {+ if (path.indexOf('http') !== 0) {+ return createReadStream(path);+ }++ const response = await fetch(path, {+ dispatcher: getSsrfSafeDispatcher(),+ } as any);++ if (!response.ok || !response.body) {+ throw new BadBody(+ identifier,+ '{}',+ Buffer.from('{}'),+ 'Could not read the media for upload'+ );+ }++ return Readable.fromWeb(response.body as any);+ }+// Streamed request bodies can't be replayed by this.fetch's retry, so// providers wrap streamed uploads in a factory that rebuilds the request// (re-opening its source streams) on every attempt. Errors carrying adiff --git a/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts b/libraries/nestjs-libraries/src/integrations/social/discord.provider.tsindex 4781ac69e..ab9d6a18e 100644--- a/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts@@ -165,9 +165,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {let index = 0;for (const item of media || []) {const fileSize = await this.mediaSize(item.path, this.identifier);- const { data: stream } = await axios.get(item.path, {- responseType: 'stream',- });+ const stream = await this.mediaStream(item.path, this.identifier);form.append(`files[${index}]`, stream, {filename: item.path.split('/').pop(),knownLength: fileSize,diff --git a/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts b/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.tsindex b50bebb48..0727ba7c4 100644--- a/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts@@ -333,7 +333,8 @@ export class LinkedinProvider extends SocialAbstract implements SocialProvider {: await this.mediaChunk(picture.path,i,- Math.min(i + chunkSize, fileSizeBytes) - 1+ Math.min(i + chunkSize, fileSizeBytes) - 1,+ this.identifier);const upload = await this.fetch(diff --git a/libraries/nestjs-libraries/src/integrations/social/reddit.provider.ts b/libraries/nestjs-libraries/src/integrations/social/reddit.provider.tsindex 474159833..147028e0f 100644--- a/libraries/nestjs-libraries/src/integrations/social/reddit.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/reddit.provider.ts@@ -181,9 +181,7 @@ export class RedditProvider extends SocialAbstract implements SocialProvider {// Stream the file into Reddit's S3 form instead of buffering it in memory.// S3 requires the exact part length, which comes from a HEAD request.const fileSize = await this.mediaSize(path, this.identifier);- const { data } = await axios.get(path, {- responseType: 'stream',- });+ const stream = await this.mediaStream(path, this.identifier);const upload = (fields as { name: string; value: string }[]).reduce((acc, value) => {@@ -193,7 +191,7 @@ export class RedditProvider extends SocialAbstract implements SocialProvider {new FormDataUpload());- upload.append('file', data, {+ upload.append('file', stream, {filename: path.split('/').pop(),contentType: (mimeType as string) || 'application/octet-stream',knownLength: fileSize,diff --git a/libraries/nestjs-libraries/src/integrations/social/skool.provider.ts b/libraries/nestjs-libraries/src/integrations/social/skool.provider.tsindex 7c31bef9d..2abc60a79 100644--- a/libraries/nestjs-libraries/src/integrations/social/skool.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/skool.provider.ts@@ -256,7 +256,7 @@ export class SkoolProvider extends SocialAbstract implements SocialProvider {if (!fileResponse.ok || !fileResponse.body) {throw new Error(`Failed to fetch media: ${fileResponse.statusText}`);}- await fetch(createFileResponse.write_url, {+ const uploadResponse = await fetch(createFileResponse.write_url, {method: 'PUT',headers: {'Content-Type': createFileResponse.content_type,@@ -267,6 +267,16 @@ export class SkoolProvider extends SocialAbstract implements SocialProvider {// Required by undici when streaming a request body.duplex: 'half',} as any);+ // A rejected PUT would otherwise publish the post with an empty+ // attachment - the file id exists but no bytes were stored.+ if (!uploadResponse.ok) {+ throw new BadBody(+ this.identifier,+ await uploadResponse.text().catch(() => '{}'),+ '{}',+ 'Failed to upload the media file'+ );+ }fileIds.push(createFileResponse.file.id);}diff --git a/libraries/nestjs-libraries/src/integrations/social/tumblr.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tumblr.provider.tsindex 3fd76e05f..593a4814d 100644--- a/libraries/nestjs-libraries/src/integrations/social/tumblr.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/tumblr.provider.ts@@ -524,10 +524,8 @@ export class TumblrProvider extends SocialAbstract implements SocialProvider {const mediaUrl = this.getMediaUrl(item.path);const mimeType = this.getMimeType(item.path);const fileSize = await this.mediaSize(mediaUrl, this.identifier);- const { data } = await axios.get(mediaUrl, {- responseType: 'stream',- });- formData.append(`media-${index}`, data, {+ const stream = await this.mediaStream(mediaUrl, this.identifier);+ formData.append(`media-${index}`, stream, {filename: item.path.split('/').pop() || `media-${index}`,contentType: mimeType,knownLength: fileSize,diff --git a/libraries/nestjs-libraries/src/integrations/social/whop.provider.ts b/libraries/nestjs-libraries/src/integrations/social/whop.provider.tsindex 23397c755..e453694f2 100644--- a/libraries/nestjs-libraries/src/integrations/social/whop.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/whop.provider.ts@@ -276,7 +276,7 @@ export class WhopProvider extends SocialAbstract implements SocialProvider {if (!fileResponse.ok || !fileResponse.body) {throw new Error(`Failed to fetch media: ${fileResponse.statusText}`);}- await fetch(createFileResponse.upload_url, {+ const uploadResponse = await fetch(createFileResponse.upload_url, {method: 'PUT',headers: {// Whop's own upload_headers win if they ever include a length.@@ -287,6 +287,16 @@ export class WhopProvider extends SocialAbstract implements SocialProvider {// Required by undici when streaming a request body.duplex: 'half',} as any);+ // A rejected PUT leaves the file pending forever - fail fast instead+ // of burning the ~9 minute status poll below.+ if (!uploadResponse.ok) {+ throw new BadBody(+ this.identifier,+ await uploadResponse.text().catch(() => '{}'),+ '{}',+ 'Failed to upload the media file'+ );+ }let uploadStatus = 'pending';let attempts = 0;diff --git a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts b/libraries/nestjs-libraries/src/integrations/social/x.provider.tsindex 5a537ed9b..c4c5e5641 100644--- a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts@@ -450,7 +450,7 @@ export class XProvider extends SocialAbstract implements SocialProvider {`media/upload/${mediaId}/append`,{segment_index: i,- media: await this.mediaChunk(path, start, end),+ media: await this.mediaChunk(path, start, end, this.identifier),},{ forceBodyMode: 'form-data' });