UNPKG

sonikdrive-file-upload

Version:

Chunked file using upload session URLs

68 lines (67 loc) 2.92 kB
"use strict"; // // This file is just a stub. The real implementation is provided via package.json exports field. // // Node.js will resolve to normalizeInput.node.js, browsers to normalizeInput.browser.js. // // export * from './normalizeInput.node'; // import { Readable } from 'stream'; // // import * as fs from 'fs'; // export type SupportedFileInput = File | Blob | Buffer | Readable | string; // let isReactNative = false; // try { // isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'; // } catch (e) { // isReactNative = false; // } // export async function normalizeFileInput(input: SupportedFileInput): Promise<Buffer | Blob> { // if (typeof File !== 'undefined' && input instanceof File) return input; // if (typeof Blob !== 'undefined' && input instanceof Blob) return input; // if (Buffer.isBuffer(input)) return input; // if (typeof input === 'string') { // // if (fs.existsSync(input)) { // // return fs.readFileSync(input); // treat as file path // // } else { // // return Buffer.from(input, 'base64'); // treat as base64 string // // } // // Only attempt to use fs in Node.js environments // if (typeof process !== 'undefined' && process.versions && process.versions.node) { // try { // const fs = await import('fs/promises'); // await fs.access(input); // Check if file exists // return await fs.readFile(input); // Read file as Buffer // } catch { // return Buffer.from(input, "base64"); // Treat as base64 string // } // } else { // // In browsers, always treat as base64 string // return Buffer.from(input, "base64"); // } // // return Buffer.from(input, 'base64'); // } // // if (typeof input === 'string') { // // if (isReactNative) { // // // ✅ Use react-native-fs for mobile // // const RNFS = require('react-native-fs'); // // if (input.startsWith('file://') || (await RNFS.exists(input))) { // // const base64Content = await RNFS.readFile(input, 'base64'); // // return Buffer.from(base64Content, 'base64'); // // } // // } else { // // // ✅ Use fs in Node.js // // // const fs = await import('fs/promises'); // // // try { // // // const data = await fs.readFile(input); // // // return data; // Buffer // // // } catch { // // // return Buffer.from(input, 'base64'); // Fallback: base64 string // // // } // // } // // } // if (isReadableStream(input)) { // const chunks: Buffer[] = []; // for await (const chunk of input) chunks.push(Buffer.from(chunk)); // return Buffer.concat(chunks); // } // throw new Error('Unsupported file input type.'); // } // function isReadableStream(obj: any): obj is Readable { // return obj && typeof obj.pipe === 'function' && typeof obj._read === 'function'; // }