Skip to content

Commit

Permalink
fix: check type
Browse files Browse the repository at this point in the history
  • Loading branch information
asafgardin committed Dec 4, 2024
1 parent e3b31ac commit 670fc69
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/files/NodeFilesHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,56 @@ import { FormDataRequest } from 'types/API';

export class NodeFilesHandler extends BaseFilesHandler {
private async convertReadableStream(readableStream: ReadableStream): Promise<NodeJS.ReadableStream> {
const { Readable } = await import('stream');
const reader = readableStream.getReader();
try {
if (typeof window === 'undefined') {
const { Readable } = await import('stream');
const reader = readableStream.getReader();

return new Readable({
async read() {
const { done, value } = await reader.read();
if (done) {
this.push(null);
} else {
this.push(value);
}
},
});
return new Readable({
async read() {
const { done, value } = await reader.read();
if (done) {
this.push(null);
} else {
this.push(value);
}
},
});
} else {
throw new Error('Stream conversion is not supported in browser environment');
}
} catch (error) {
console.error('Error in convertReadableStream:', error);
throw error;
}
}

async prepareFormDataRequest(file: FilePathOrFileObject): Promise<FormDataRequest> {
console.log('Preparing form data request for Node.js');
try {
const FormData = await import('form-data').then((m) => m.default || m);
const FormData = await import('form-data').then(m => m.default || m);
console.log('Successfully imported form-data module');

const formData = new FormData();
console.log('Created new FormData instance');

if (typeof file === 'string') {
const fs = await import('fs').then((m) => m.default || m);
if (!fs.existsSync(file)) {
throw new Error(`File not found: ${file}`);
if (typeof window === 'undefined') {
const fs = await import('fs').then(m => m.default || m);
if (!fs.existsSync(file)) {
throw new Error(`File not found: ${file}`);
}
console.log(`Appending file from path: ${file}`);
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
} else {
throw new Error('File system operations are not supported in browser environment');
}
console.log(`Appending file from path: ${file}`);
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
} else if (file && typeof file === 'object') {
console.log('Processing file object:', file);
if ('buffer' in file) {
// Handle Node.js file-like object
console.log('Appending file from buffer');
formData.append('file', file.buffer, { filename: file.name, contentType: file.type });
} else if ('stream' in file && typeof file.stream === 'function') {
// Handle File object
console.log('Converting and appending file from stream');
const nodeStream = await this.convertReadableStream(file.stream());
formData.append('file', nodeStream, { filename: file.name, contentType: file.type });
Expand Down

0 comments on commit 670fc69

Please sign in to comment.